Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
46.67% covered (warning)
46.67%
7 / 15
CRAP
70.00% covered (success)
70.00%
35 / 50
Siesta
0.00% covered (danger)
0.00%
0 / 1
46.67% covered (warning)
46.67%
7 / 15
24.80
70.00% covered (success)
70.00%
35 / 50
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
8 / 8
 setLogger
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 addFile
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 addFileType
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setGenericGeneratorConfig
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getConnection
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
 setConnection
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setup
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 prepareModel
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 generateClasses
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 migrateDirect
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 migrateToFile
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 checkValidationError
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
 setTableNamingStrategy
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setColumnNamingStrategy
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 Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Siesta\Config\GenericConfigLoader;
use Siesta\Database\Connection;
use Siesta\Database\ConnectionFactory;
use Siesta\Exception\InvalidConfigurationException;
use Siesta\Generator\MainGenerator;
use Siesta\Migration\Migrator;
use Siesta\Model\DataModel;
use Siesta\Model\ValidationLogger;
use Siesta\NamingStrategy\NamingStrategy;
use Siesta\NamingStrategy\NamingStrategyRegistry;
use Siesta\Util\File;
use Siesta\Validator\Validator;
use Siesta\XML\XMLDirectoryScanner;
/**
 * @author Gregor Müller
 */
class Siesta implements LoggerAwareInterface
{
    /**
     * @var ValidationLogger
     */
    protected $validationLogger;
    /**
     * @var XMLDirectoryScanner
     */
    protected $directoryScanner;
    /**
     * @var DataModel
     */
    protected $dataModel;
    /**
     * @var GenericConfigLoader
     */
    protected $genericConfigLoader;
    /**
     * @var Validator
     */
    protected $validator;
    /**
     * @var MainGenerator
     */
    protected $mainGenerator;
    /**
     * @var Migrator
     */
    protected $migrator;
    /**
     * @var Connection
     */
    protected $connection;
    /**
     * Siesta constructor.
     */
    public function __construct()
    {
        $this->validationLogger = new ValidationLogger();
        $this->directoryScanner = new XMLDirectoryScanner($this->validationLogger);
        $this->dataModel = new DataModel();
        $this->genericConfigLoader = new GenericConfigLoader();
        $this->validator = new Validator();
        $this->mainGenerator = new MainGenerator();
        $this->migrator = new Migrator();
    }
    /**
     * @param LoggerInterface $logger
     */
    public function setLogger(LoggerInterface $logger)
    {
        $this->validationLogger->setLogger($logger);
        $this->migrator->setLogger($logger);
    }
    /**
     * @param File $file
     */
    public function addFile(File $file)
    {
        $this->directoryScanner->addFile($file);
    }
    /**
     * @param string $extension
     * @param string|null $baseDir
     */
    public function addFileType(string $extension, string $baseDir = null)
    {
        $this->directoryScanner->addFileExtension($extension, $baseDir);
    }
    /**
     * @param File $file
     */
    public function setGenericGeneratorConfig(File $file)
    {
        $this->genericConfigLoader->setConfigFile($file);
    }
    /**
     * @return Connection
     */
    public function getConnection()
    {
        if ($this->connection !== null) {
            return $this->connection;
        }
        return ConnectionFactory::getConnection();
    }
    /**
     * @param Connection $connection
     */
    public function setConnection(Connection $connection)
    {
        $this->connection = $connection;
    }
    /**
     * @throws InvalidConfigurationException
     */
    protected function setup()
    {
        $genericConfig = $this->genericConfigLoader->loadAndValidate($this->validationLogger);
        $this->checkValidationError();
        $this->validator->setup($genericConfig);
        $this->mainGenerator->setup($genericConfig);
    }
    /**
     * @throws InvalidConfigurationException
     */
    protected function prepareModel()
    {
        $xmlEntityList = $this->directoryScanner->getXmlEntityList();
        $this->dataModel->addXMLEntityList($xmlEntityList);
        $this->dataModel->update();
        $this->validator->validateDataModel($this->dataModel, $this->validationLogger);
        $this->checkValidationError();
    }
    /**
     * @param string $baseDir
     * @param bool $dropUnusedTables
     *
     * @throws InvalidConfigurationException
     */
    protected function generateClasses(string $baseDir, bool $dropUnusedTables = true)
    {
        $this->setup();
        $this->prepareModel();
        $this->mainGenerator->generate($this->dataModel, $baseDir);
    }
    /**
     * @param string $baseDir
     * @param bool $dropUnusedTables
     *
     * @throws InvalidConfigurationException
     */
    public function migrateDirect(string $baseDir, bool $dropUnusedTables = true)
    {
        $this->generateClasses($baseDir, $dropUnusedTables);
        $this->migrator->migrateDirect($this->dataModel, $this->getConnection(), $dropUnusedTables);
    }
    /**
     * @param string $baseDir
     * @param File $targetFile
     * @param bool $dropUnusedTables
     *
     * @throws InvalidConfigurationException
     */
    public function migrateToFile(string $baseDir, File $targetFile, bool $dropUnusedTables = true)
    {
        $this->generateClasses($baseDir, $dropUnusedTables);
        $this->migrator->migrateToSQLFile($this->dataModel, $this->getConnection(), $targetFile, $dropUnusedTables);
    }
    /**
     * @throws InvalidConfigurationException
     */
    protected function checkValidationError()
    {
        if (!$this->validationLogger->hasError()) {
            return;
        }
        throw new InvalidConfigurationException();
    }
    /**
     * @param NamingStrategy $strategy
     */
    public function setTableNamingStrategy(NamingStrategy $strategy)
    {
        NamingStrategyRegistry::setTableNamingStrategy($strategy);
    }
    /**
     * @param NamingStrategy $strategy
     */
    public function setColumnNamingStrategy(NamingStrategy $strategy)
    {
        NamingStrategyRegistry::setColumnNamingStrategy($strategy);
    }
}