JavascriptBuilder::getNotBlameFkRelations()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 1
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
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
        $fkRels = $this->getFkRelations($schema->fkRelations());
40
        $properties = \array_filter(\array_map(function (SchemaAttributeInterface $property) {
41
            if ($property->isBlameBy()) {
42
                return $property->name();
43
            }
44
        }, $schema->attributes()));
45
46
        // @phpstan-ignore-next-line
47
        return \array_filter($fkRels, function (array $fkRel) use ($properties) {
48
            if (!\in_array($fkRel['pk'], $properties)) {
49
                return $fkRel;
50
            }
51
        });
52
    }
53
}
54