RealCollection   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createUses() 0 9 1
A createDefinition() 0 12 1
A createConstructor() 0 16 1
1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 22.03.16 at 15:46
6
 */
7
namespace samsonframework\orm\generator;
8
9
use samsonframework\orm\generator\metadata\RealMetadata;
10
use samsonphp\generator\Generator;
11
12
/**
13
 * Real entity collection class generator.
14
 *
15
 * @package samsonframework\orm\generator
16
 */
17
class RealCollection extends Generic
18
{
19
    /**
20
     * Query constructor.
21
     *
22
     * @param Generator $generator
23
     * @param           $metadata
24
     */
25
    public function __construct(Generator $generator, $metadata)
26
    {
27
        parent::__construct($generator, $metadata);
28
29
        $this->parentClass = $this->className . 'Query';
30
        $this->className .= 'Collection';
31
    }
32
33
    /**
34
     * Class uses generation part.
35
     *
36
     * @param RealMetadata $metadata Entity metadata
37
     */
38
    protected function createUses($metadata)
39
    {
40
        $this->generator
41
            ->newLine('use samsonframework\core\ViewInterface;')
42
            ->newLine('use samsonframework\orm\QueryInterface;')
43
            ->newLine('use samsonframework\orm\ArgumentInterface;')
44
            ->newLine('use samson\activerecord\dbQuery;')
45
            ->newLine();
46
    }
47
48
    /**
49
     * Class definition generation part.
50
     *
51
     * @param RealMetadata $metadata Entity metadata
52
     */
53
    protected function createDefinition($metadata)
54
    {
55
        $this->generator
56
            ->multiComment(array(
57
                'Class for rendering and querying and fetching "' . $metadata->entity . '" instances from database',
58
                '@method ' . $metadata->entity . ' first();',
59
                '@method ' . $metadata->entity . '[] find();',
60
            ))
61
            ->defClass($this->className, $this->parentClass)
62
            ->newLine('use \\' . \samsonframework\orm\Renderable::class . ';')
63
            ->newLine();
64
    }
65
66
    /**
67
     * Class constructor generation part.
68
     *
69
     * @param RealMetadata $metadata Entity metadata
70
     */
71
    protected function createConstructor($metadata)
72
    {
73
        $class = "\n\t" . '/**';
74
        $class .= "\n\t" . ' * @param ViewInterface $renderer Rendering instance';
75
        $class .= "\n\t" . ' * @param QueryInterface $query Database query instance';
76
        $class .= "\n\t" . ' */';
77
        $class .= "\n\t" . 'public function __construct(ViewInterface $renderer, QueryInterface $query = null)';
78
        $class .= "\n\t" . '{';
79
        $class .= "\n\t\t" . '// TODO: This should be removed!';
80
        $class .= "\n\t\t" . '$this->renderer = $renderer;';
81
        $class .= "\n\t\t" . '$container = $GLOBALS[\'__core\']->getContainer();';
82
        $class .= "\n\t\t" . 'parent::__construct($query ?? $container->get("query"));';
83
        $class .= "\n\t" . '}';
84
85
        $this->generator->text($class);
86
    }
87
}
88
//[PHPCOMPRESSOR(remove,end)]
89