TableMapGenerator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 48
ccs 0
cts 18
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A generate() 0 13 2
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\TableMap;
13
14
use Rocket\ORM\Generator\Generator;
15
use Rocket\ORM\Generator\Schema\Schema;
16
17
/**
18
 * @author Sylvain Lorinet <[email protected]>
19
 */
20
class TableMapGenerator extends Generator
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $modelNamespace;
26
27
    /**
28
     * @var \Twig_Environment
29
     */
30
    protected $twig;
31
32
33
    /**
34
     * @param string $modelNamespace
35
     * @param array  $templateDirs
36
     */
37
    public function __construct($modelNamespace = '\\Rocket\\ORM\\Model\\Map\\TableMap', array $templateDirs = [])
38
    {
39
        $class = new \ReflectionClass($modelNamespace);
40
        if (!$class->implementsInterface('\\Rocket\\ORM\\Model\\Map\\TableMapInterface')) {
41
            throw new \InvalidArgumentException('The table map model must implement Rocket\ORM\Model\Map\TableMapInterface');
42
        }
43
44
        $this->modelNamespace = $modelNamespace;
45
        $this->twig           = new \Twig_Environment(new \Twig_Loader_Filesystem(array_merge($templateDirs, [__DIR__ . '/../../Resources/Skeletons/Model/Map'])), [
46
            'cache'            => false,
47
            'strict_variables' => true
48
        ]);
49
    }
50
51
    /**
52
     * @param Schema $schema
53
     */
54
    public function generate(Schema $schema)
55
    {
56
        $outputDirectory = $schema->absoluteDirectory . DIRECTORY_SEPARATOR . 'TableMap';
57
        $this->createDirectory($outputDirectory);
58
59
        foreach ($schema->getTables() as $table) {
60
            $template = $this->twig->render('table_map.php.twig', [
61
                'table'  => $table
62
            ]);
63
64
            file_put_contents($outputDirectory . DIRECTORY_SEPARATOR . $table->phpName . 'TableMap.php', $template);
65
        }
66
    }
67
}
68