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

JavascriptBuilder::getNotBlameFkRelations()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 9.9666
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