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

MySQLGatewayBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 24
rs 9.8333
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\Gateway;
11
12
use FlexPHP\Generator\Domain\Builders\AbstractBuilder;
13
use FlexPHP\Schema\Constants\Keyword;
14
15
final class MySQLGatewayBuilder extends AbstractBuilder
16
{
17
    public function __construct(string $entity, array $actions, array $properties)
18
    {
19
        $entity = $this->getPascalCase($this->getSingularize($entity));
20
        $name = $this->getSnakeCase($this->getPluralize($entity));
21
        $item = $this->getCamelCase($this->getSingularize($entity));
22
        $actions = \array_reduce($actions, function (array $result, string $action) {
23
            $result[] = $this->getCamelCase($action);
24
25
            return $result;
26
        }, []);
27
28
        $properties = \array_reduce($properties, function ($result, $property) {
29
            $result[$this->getCamelCase($property[Keyword::NAME])] = $property;
30
31
            return $result;
32
        }, []);
33
34
        $dbtypes = \array_reduce($properties, function ($result, $property) {
35
            $result[$this->getCamelCase($property[Keyword::NAME])] = $this->getDbType($property[Keyword::DATATYPE]);
36
37
            return $result;
38
        }, []);
39
40
        parent::__construct(\compact('entity', 'item', 'name', 'actions', 'properties', 'dbtypes'));
41
    }
42
43
    protected function getFileTemplate(): string
44
    {
45
        return 'MySQLGateway.php.twig';
46
    }
47
48
    protected function getPathTemplate(): string
49
    {
50
        return \sprintf('%1$s/FlexPHP/Gateway', parent::getPathTemplate());
51
    }
52
53
    private function getDbType(string $dataType): string
54
    {
55
        $dbTypes = [
56
            'smallint' => 'INTEGER',
57
            'integer' => 'INTEGER',
58
            'float' => 'INTEGER',
59
            'double' => 'INTEGER',
60
            'bool' => 'BOOLEAN',
61
            'boolean' => 'BOOLEAN',
62
        ];
63
64
        if (!empty($dbTypes[$dataType])) {
65
            return $dbTypes[$dataType];
66
        }
67
68
        return 'STRING';
69
    }
70
}
71