QueryGenerator::generate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 23
loc 23
ccs 0
cts 13
cp 0
rs 9.0856
cc 3
eloc 11
nc 3
nop 1
crap 12
1
<?php
2
3
/*
4
 * This file is part of the "RocketORM" package.
5
 *
6
 * https://github.com/RocketORM/ORM
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rocket\ORM\Generator\Model\Query;
13
14
use Rocket\ORM\Generator\Generator;
15
use Rocket\ORM\Generator\Schema\Schema;
16
use Rocket\ORM\Rocket;
17
18
/**
19
 * @author Sylvain Lorinet <[email protected]>
20
 */
21
class QueryGenerator extends Generator
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $modelNamespace;
27
28
    /**
29
     * @var \Twig_Environment
30
     */
31
    protected $twig;
32
33
34
    /**
35
     * @param string $modelNamespace
36
     * @param array  $templateDirs
37
     */
38
    public function __construct($modelNamespace = '\\Rocket\\ORM\\Model\\Query\\Query', array $templateDirs = [])
39
    {
40
        $class = new \ReflectionClass($modelNamespace);
41
        if (!$class->implementsInterface('\\Rocket\\ORM\\Model\\Query\\QueryInterface')) {
42
            throw new \InvalidArgumentException('The table map model must implement Rocket\ORM\Model\Query\QueryInterface');
43
        }
44
45
        $this->modelNamespace = $modelNamespace;
46
47
        $loader = new \Twig_Loader_Filesystem(array_merge($templateDirs, [
48
            __DIR__ . '/../../Resources/Skeletons/Model/Query'
49
        ]));
50
        $loader->addPath(__DIR__ . '/../../Resources/Skeletons/Model/Query/Driver/SQLite', 'sqlite');
51
52
        $this->twig = new \Twig_Environment($loader, [
53
            'cache'            => false,
54
            'strict_variables' => true
55
        ]);
56
    }
57
58
    /**
59
     * @param Schema $schema
60
     *
61
     * @return void
62
     */
63 View Code Duplication
    public function generate(Schema $schema)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        // First generate base
66
        $this->generateBase($schema);
67
68
        $outputDirectory = $schema->absoluteDirectory;
69
        $this->createDirectory($outputDirectory);
70
71
        foreach ($schema->getTables() as $table) {
72
            $outputFile = $outputDirectory . DIRECTORY_SEPARATOR . $table->phpName . 'Query.php';
73
74
            // Check if file already exists, do not override the file
75
            if (is_file($outputFile)) {
76
                continue;
77
            }
78
79
            $template = $this->twig->render('query.php.twig', [
80
                'table'  => $table
81
            ]);
82
83
            file_put_contents($outputFile, $template);
84
        }
85
    }
86
87
    /**
88
     * @param Schema $schema
89
     */
90 View Code Duplication
    protected function generateBase(Schema $schema)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $outputDirectory = $schema->absoluteDirectory . DIRECTORY_SEPARATOR . 'Base';
93
        $this->createDirectory($outputDirectory);
94
95
        // Allow overriding template for a given driver
96
        $driver = Rocket::getConnectionDriver($schema->connection);
97
98
        foreach ($schema->getTables() as $table) {
99
            $template = $this->twig->resolveTemplate([
100
                '@' . $driver . '/base_query.php.twig',
101
                'base_query.php.twig'
102
            ])->render([
103
                'table'  => $table,
104
                'driver' => $driver
105
            ]);
106
107
            file_put_contents($outputDirectory . DIRECTORY_SEPARATOR . 'Base' . $table->phpName . 'Query.php', $template);
108
        }
109
    }
110
}
111