Completed
Push — develop ( 8dd5aa...fee724 )
by Freddie
14:52
created

JavascriptBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getNotBlameFkRelations() 0 14 3
A getPathTemplate() 0 3 1
A getFileTemplate() 0 3 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Generator\Domain\Builders\Javascript;
11
12
use FlexPHP\Generator\Domain\Builders\AbstractBuilder;
13
use FlexPHP\Schema\SchemaAttributeInterface;
14
use FlexPHP\Schema\SchemaInterface;
15
16
final class JavascriptBuilder extends AbstractBuilder
17
{
18
    public function __construct(SchemaInterface $schema)
19
    {
20
        $route = $this->getInflector()->route($schema->name());
21
        $form = $this->getInflector()->form($schema->name());
22
        $fkRels = $this->getNotBlameFkRelations($schema);
23
24
        parent::__construct(\compact('route', 'form', 'fkRels'));
25
    }
26
27
    protected function getFileTemplate(): string
28
    {
29
        return 'Javascript.js.twig';
30
    }
31
32
    protected function getPathTemplate(): string
33
    {
34
        return \sprintf('%1$s/FlexPHP/Javascript', parent::getPathTemplate());
35
    }
36
37
    private function getNotBlameFkRelations(SchemaInterface $schema): array
38
    {
39
        $properties = \array_map(function (SchemaAttributeInterface $property) {
40
            if ($property->isBlameBy()) {
41
                return $property->name();
42
            }
43
        }, $schema->attributes());
44
45
        $properties = \array_filter($properties);
46
        $fkRels = $this->getFkRelations($schema->fkRelations());
47
48
        return \array_filter($fkRels, function (array $fkRel) use ($properties) {
49
            if (!\in_array($fkRel['pk'], $properties)) {
50
                return $fkRel;
51
            }
52
        });
53
    }
54
}
55