Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (warning)
50.00%
10 / 20
CRAP
53.70% covered (warning)
53.70%
29 / 54
Reverse
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (warning)
50.00%
10 / 20
75.49
53.70% covered (warning)
53.70%
29 / 54
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 createSingleXMLFile
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
8 / 8
 createXMLFileForEveryTable
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 7
 getTargetFile
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 4
 createAndInitializeXMLEntity
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 getXMLWrite
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getDatabaseMetaData
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getDefaultNamespace
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setDefaultNamespace
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getConnection
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
 setConnectionName
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setConnection
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getDatabaseName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setDatabaseName
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getEntityXMLFileSuffix
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setEntityXMLFileSuffix
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getTargetPath
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setTargetPath
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 setClassNamingStrategy
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setAttributeNamingStrategy
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
<?php
declare(strict_types = 1);
namespace Siesta\Main;
use Siesta\Contract\AttributeNamingStrategy;
use Siesta\Database\Connection;
use Siesta\Database\ConnectionFactory;
use Siesta\Database\MetaData\DatabaseMetaData;
use Siesta\Database\MetaData\TableMetaData;
use Siesta\NamingStrategy\NamingStrategy;
use Siesta\NamingStrategy\NamingStrategyRegistry;
use Siesta\Util\File;
use Siesta\XML\XMLEntity;
use Siesta\XML\XMLWrite;
/**
 * @author Gregor Müller
 */
class Reverse
{
    /**
     * @var string
     */
    protected $entityXMLFileSuffix;
    /**
     * @var string
     */
    protected $defaultNamespace;
    /**
     * @var string
     */
    protected $targetPath;
    /**
     * @var Connection
     */
    protected $connection;
    /**
     * @var string
     */
    protected $databaseName;
    /**
     * @var AttributeNamingStrategy
     */
    protected $attributeNamingStrategy;
    /**
     * Reverse constructor.
     */
    public function __construct()
    {
        $this->entityXMLFileSuffix = ".reverse.xml";
    }
    /**
     * @param string $targetFileName
     */
    public function createSingleXMLFile(string $targetFileName)
    {
        $xmlWrite = $this->getXMLWrite();
        $databaseMetaData = $this->getDatabaseMetaData();
        foreach ($databaseMetaData->getTableList() as $tableMetaData) {
            $this->createAndInitializeXMLEntity($tableMetaData, $xmlWrite);
        }
        $targetFile = new File($targetFileName);
        $targetFile->createDirForFile();
        $xmlWrite->saveToFile($targetFile);
    }
    /**
     * @param string $targetDirectory
     */
    public function createXMLFileForEveryTable(string $targetDirectory)
    {
        $databaseMetaData = $this->getDatabaseMetaData();
        foreach ($databaseMetaData->getTableList() as $tableMetaData) {
            $xmlWrite = $this->getXMLWrite();
            $this->createAndInitializeXMLEntity($tableMetaData, $xmlWrite);
            $targetFile = $this->getTargetFile($targetDirectory, $tableMetaData);
            $xmlWrite->saveToFile($targetFile);
        }
    }
    /**
     * @param string $targetDirectory
     * @param TableMetaData $tableMetaData
     *
     * @return File
     */
    protected function getTargetFile(string $targetDirectory, TableMetaData $tableMetaData) : File
    {
        $filePath = $targetDirectory . "/" . $tableMetaData->getName() . $this->getEntityXMLFileSuffix();
        $targetFile = new File($filePath);
        $targetFile->createDirForFile();
        return $targetFile;
    }
    /**
     * @param TableMetaData $tableMetaData
     * @param XMLWrite $xmlWrite
     *
     * @return XMLEntity
     */
    protected function createAndInitializeXMLEntity(TableMetaData $tableMetaData, XMLWrite $xmlWrite)
    {
        $xmlEntity = new XMLEntity();
        $xmlEntity->setNamespaceName($this->getDefaultNamespace());
        $xmlEntity->setTargetPath($this->getTargetPath());
        $xmlEntity->fromTable($tableMetaData);
        $xmlEntity->toXML($xmlWrite);
        return $xmlEntity;
    }
    /**
     * @return XMLWrite
     */
    protected function getXMLWrite()
    {
        $document = new \DOMDocument("1.0");
        return new XMLWrite($document, null, "entityList");
    }
    /**
     * @return DatabaseMetaData
     */
    public function getDatabaseMetaData() : DatabaseMetaData
    {
        $connection = $this->getConnection();
        return $connection->getDatabaseMetaData($this->getDatabaseName());
    }
    /**
     * @return string
     */
    public function getDefaultNamespace()
    {
        return $this->defaultNamespace;
    }
    /**
     * @param string $defaultNamespace
     */
    public function setDefaultNamespace(string $defaultNamespace = "")
    {
        $this->defaultNamespace = $defaultNamespace;
    }
    /**
     * @return Connection
     */
    public function getConnection() : Connection
    {
        if ($this->connection !== null) {
            return $this->connection;
        }
        return ConnectionFactory::getConnection();
    }
    /**
     * @param string $connectionName
     */
    public function setConnectionName(string $connectionName = null)
    {
        $this->connection = ConnectionFactory::getConnection($connectionName);
    }
    /**
     * @param Connection $connection
     */
    public function setConnection($connection)
    {
        $this->connection = $connection;
    }
    /**
     * @return string
     */
    public function getDatabaseName()
    {
        return $this->databaseName;
    }
    /**
     * @param string $databaseName
     */
    public function setDatabaseName($databaseName)
    {
        $this->databaseName = $databaseName;
    }
    /**
     * @return string
     */
    public function getEntityXMLFileSuffix()
    {
        return $this->entityXMLFileSuffix;
    }
    /**
     * @param string $entityXMLFileSuffix
     */
    public function setEntityXMLFileSuffix(string $entityXMLFileSuffix = null)
    {
        $this->entityXMLFileSuffix = $entityXMLFileSuffix;
    }
    /**
     * @return string
     */
    public function getTargetPath()
    {
        return $this->targetPath;
    }
    /**
     * @param string $targetPath
     */
    public function setTargetPath(string $targetPath)
    {
        $this->targetPath = $targetPath;
    }
    /**
     * @param NamingStrategy $namingStrategy
     */
    public function setClassNamingStrategy(NamingStrategy $namingStrategy)
    {
        NamingStrategyRegistry::setClassNamingStrategy($namingStrategy);
    }
    /**
     * @param NamingStrategy $attributeNamingStrategy
     */
    public function setAttributeNamingStrategy(NamingStrategy $attributeNamingStrategy)
    {
        NamingStrategyRegistry::setAttributeNamingStrategy($attributeNamingStrategy);
    }
}