MySQLGatewayBuilder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 71
c 1
b 0
f 0
dl 0
loc 108
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDbType() 0 33 2
A getOperator() 0 15 2
A getFileTemplate() 0 3 1
A __construct() 0 43 1
A getPathTemplate() 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\Gateway;
11
12
use FlexPHP\Generator\Domain\Builders\AbstractBuilder;
13
use FlexPHP\Schema\Constants\Operator;
14
use FlexPHP\Schema\SchemaAttributeInterface;
15
use FlexPHP\Schema\SchemaInterface;
16
17
final class MySQLGatewayBuilder extends AbstractBuilder
18
{
19
    public function __construct(SchemaInterface $schema, array $actions)
20
    {
21
        $table = $schema->name();
22
        $entity = $this->getInflector()->entity($schema->name());
23
        $item = $this->getInflector()->item($schema->name());
24
        $actions = \array_reduce($actions, function (array $result, string $action) {
25
            $result[] = $this->getInflector()->camelAction($action);
26
27
            return $result;
28
        }, []);
29
30
        $dbTypes = [];
31
        $operators = [];
32
        $pkName = $this->getInflector()->camelProperty($schema->pkName());
33
        $fkFns = $this->getFkFunctions($schema->fkRelations());
34
        $fkRels = $this->getFkRelations($schema->fkRelations());
35
        $properties = \array_reduce(
36
            $schema->attributes(),
37
            function (array $result, SchemaAttributeInterface $property) use (&$dbTypes, &$operators) {
38
                $camelName = $this->getInflector()->camelProperty($property->name());
39
40
                $result[$camelName] = $property;
41
                $dbTypes[$camelName] = $this->getDbType($property->dataType());
42
                $operators[$camelName] = $this->getOperator($property->filter());
43
44
                return $result;
45
            },
46
            []
47
        );
48
        $header = self::getHeaderFile();
49
50
        parent::__construct(\compact(
51
            'header',
52
            'entity',
53
            'item',
54
            'table',
55
            'actions',
56
            'properties',
57
            'dbTypes',
58
            'pkName',
59
            'fkFns',
60
            'fkRels',
61
            'operators'
62
        ));
63
    }
64
65
    protected function getFileTemplate(): string
66
    {
67
        return 'MySQLGateway.php.twig';
68
    }
69
70
    protected function getPathTemplate(): string
71
    {
72
        return \sprintf('%1$s/FlexPHP/Gateway', parent::getPathTemplate());
73
    }
74
75
    private function getDbType(string $dataType): string
76
    {
77
        $dbTypes = [
78
            'array' => 'ARRAY',
79
            'bigint' => 'BIGINT',
80
            'binary' => 'BINARY',
81
            'blob' => 'BLOB',
82
            'boolean' => 'BOOLEAN',
83
            'date' => 'DATE_MUTABLE',
84
            'date_immutable' => 'DATE_IMMUTABLE',
85
            'dateinterval' => 'DATEINTERVAL',
86
            'datetime' => 'DATETIME_MUTABLE',
87
            'datetime_immutable' => 'DATETIME_IMMUTABLE',
88
            'datetimetz' => 'DATETIMETZ_MUTABLE',
89
            'datetimetz_immutable' => 'DATETIMETZ_IMMUTABLE',
90
            'decimal' => 'DECIMAL',
91
            'float' => 'FLOAT',
92
            'guid' => 'GUID',
93
            'integer' => 'INTEGER',
94
            'json' => 'JSON',
95
            'object' => 'OBJECT',
96
            'simple_array' => 'SIMPLE_ARRAY',
97
            'smallint' => 'SMALLINT',
98
            'text' => 'TEXT',
99
            'time' => 'TIME_MUTABLE',
100
            'time_immutable' => 'TIME_IMMUTABLE',
101
        ];
102
103
        if (!empty($dbTypes[$dataType])) {
104
            return $dbTypes[$dataType];
105
        }
106
107
        return 'STRING';
108
    }
109
110
    private function getOperator(?string $operator): string
111
    {
112
        $operators = [
113
            Operator::EQUALS => 'OP_EQUALS',
114
            Operator::STARTS => 'OP_START',
115
            Operator::ENDS => 'OP_END',
116
            Operator::CONTAINS => 'OP_CONTAINS',
117
            Operator::EXPLODE => 'OP_SEARCH',
118
        ];
119
120
        if (!empty($operators[$operator])) {
121
            return $operators[$operator];
122
        }
123
124
        return '';
125
    }
126
}
127