Completed
Push — master ( 805de4...131e3d )
by Vitaly
02:35
created

Writer::write()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 8.439
cc 5
eloc 10
nc 8
nop 0
1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 25.03.16 at 12:04
6
 */
7
namespace samsoncms\api\generator;
8
9
use samsonframework\orm\DatabaseInterface;
10
use samsonphp\generator\Generator;
11
12
/**
13
 * Generator classes file writer.
14
 *
15
 * @package samsoncms\api\generator
16
 */
17
class Writer
18
{
19
    /** @var \samsoncms\api\generator\analyzer\Generic[string] Collection of entity analyzers */
20
    protected $analyzers = [];
21
22
    /** @var \samsoncms\api\generator\Generic[string] Collection of entity generators */
23
    protected $generators = [];
24
25
    /** @var Generator Code generator */
26
    protected $codeGenerator;
27
28
    /** @var string Path to generated entities */
29
    protected $path;
30
31
    /**
32
     * Writer constructor.
33
     *
34
     * @param DatabaseInterface $db
35
     * @param Generator         $codeGenerator
36
     * @param string            $namespace
37
     * @param array             $analyzers Collection of analyzer class names
38
     * @param string            $path Path to generated entities
39
     *
40
     * @throws \Exception
41
     */
42
    public function __construct(DatabaseInterface $db, Generator $codeGenerator, $namespace, array $analyzers, $path)
43
    {
44
        $this->codeGenerator = $codeGenerator;
45
        $this->path = $path;
46
        $this->namespace = $namespace;
0 ignored issues
show
Bug introduced by
The property namespace does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
48
        // Create analyzer instances
49
        foreach ($analyzers as $analyzerClass => $generators) {
50
            if (class_exists($analyzerClass)) {
51
                $this->analyzers[$analyzerClass] = new $analyzerClass($db);
52
53
                // Validate generator classes
54
                foreach ($generators as $generator) {
55
                    if (class_exists($generator)) {
56
                        $this->generators[$analyzerClass][] = $generator;
57
                    } else {
58
                        throw new \Exception('Entity generator class[' . $generator . '] not found');
59
                    }
60
                }
61
            } else {
62
                throw new \Exception('Entity analyzer class['.$analyzerClass.'] not found');
63
            }
64
        }
65
    }
66
67
    /**
68
     * Analyze, generate and write class files.
69
     */
70
    public function write()
71
    {
72
        // Create module cache folder if not exists
73
        if (!file_exists($this->path)) {
74
            @mkdir($path, 0777, true);
0 ignored issues
show
Bug introduced by
The variable $path does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
75
        }
76
77
        foreach ($this->analyzers as $analyzerClass => $analyzer) {
78
            // Analyze database structure and get entities metadata
79
            foreach ($analyzer->analyze() as $metadata) {
80
                // Iterate all generators for analyzer
81
                foreach ($this->generators[$analyzerClass] as $generator) {
82
                    /** @var Generic $generator Create class generator */
83
                    $generator = new $generator($this->codeGenerator->defNamespace($this->namespace), $metadata);
84
85
                    // Create entity generated class names
86
                    $file = $this->path . $generator->className . '.php';
87
88
                    // Create entity query class files
89
                    file_put_contents($file, '<?php' . $generator->generate());
90
91
                    // Require files
92
                    require($file);
93
                }
94
            }
95
        }
96
    }
97
}
98
//[PHPCOMPRESSOR(remove,end)]
99