Completed
Push — develop ( 3d0c4c...e7cba6 )
by Freddie
13:19 queued 12s
created

FactoryBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPathTemplate() 0 3 1
A __construct() 0 7 1
A getFileTemplate() 0 3 1
A getSetterWithCasting() 0 11 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\Factory;
11
12
use FlexPHP\Generator\Domain\Builders\AbstractBuilder;
13
use FlexPHP\Generator\Domain\Builders\Entity\TypeHintTrait;
14
use FlexPHP\Schema\Constants\Keyword;
15
16
final class FactoryBuilder extends AbstractBuilder
17
{
18
    use TypeHintTrait;
19
20
    public function __construct(string $entity, array $properties)
21
    {
22
        $entity = $this->getPascalCase($this->getSingularize($entity));
23
        $item = $this->getCamelCase($this->getSingularize($entity));
24
        $setters = $this->getSetterWithCasting($properties);
25
26
        parent::__construct(\compact('entity', 'item', 'setters'));
27
    }
28
29
    protected function getFileTemplate(): string
30
    {
31
        return 'Factory.php.twig';
32
    }
33
34
    protected function getPathTemplate(): string
35
    {
36
        return \sprintf('%1$s/FlexPHP/Factory', parent::getPathTemplate());
37
    }
38
39
    private function getSetterWithCasting(array $properties): array
40
    {
41
        return \array_reduce($properties, function (array $result, array $attributes): array {
42
            $result[] = [
43
                'pascal' => $this->getPascalCase($attributes[Keyword::NAME]),
44
                'camel' => $this->getCamelCase($attributes[Keyword::NAME]),
45
                'typehint' => $this->guessTypeHint($attributes[Keyword::DATATYPE]),
46
            ];
47
48
            return $result;
49
        }, []);
50
    }
51
}
52