Passed
Push — master ( 6616aa...06e1ac )
by SignpostMarv
02:06
created

TestObjectRepository::QueryPartFromRefReturn()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 8
nop 4
dl 0
loc 37
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject\EasyDB;
10
11
use ParagonIE\EasyDB\EasyDB;
12
use ReflectionClass;
13
use ReflectionType;
14
use RuntimeException;
15
use SignpostMarv\DaftObject\AbstractDaftObjectEasyDBRepository;
16
use SignpostMarv\DaftObject\DefinesOwnIdPropertiesInterface;
17
18
class TestObjectRepository extends AbstractDaftObjectEasyDBRepository
19
{
20
    protected function __construct(string $type, EasyDB $db)
21
    {
22
        parent::__construct($type, $db);
23
24
        /**
25
        * @var DefinesOwnIdPropertiesInterface $type
26
        */
27
        $type = $type;
28
29
        $query =
30
            'CREATE TABLE ' .
31
            $db->escapeIdentifier($this->DaftObjectDatabaseTable()) .
32
            ' (';
33
34
        $queryParts = [];
35
36
        $ref = new ReflectionClass($type);
37
        $nullables = $type::DaftObjectNullableProperties();
38
39
        foreach ($type::DaftObjectProperties() as $i => $prop) {
40
            $methodName = 'Get' . ucfirst($prop);
41
            if (true === $ref->hasMethod($methodName)) {
42
                $refReturn = $ref->getMethod($methodName)->getReturnType();
43
                if (
44
                    ($refReturn instanceof ReflectionType) &&
45
                    true
46
                ) {
47
                    $queryParts[] = static::QueryPartFromRefReturn(
48
                        $db,
49
                        $refReturn,
50
                        $prop,
51
                        $nullables
52
                    );
53
                }
54
            }
55
        }
56
57
        $primaryKeyCols = [];
58
        foreach ($type::DaftObjectIdProperties() as $col) {
59
            $primaryKeyCols[] = $db->escapeIdentifier($col);
60
        }
61
62
        if (count($primaryKeyCols) > 0) {
63
            $queryParts[] =
64
                'PRIMARY KEY (' .
65
                implode(',', $primaryKeyCols) .
66
                ')';
67
        }
68
69
        $query .=
70
            implode(',', $queryParts) .
71
            ');';
72
73
        $db->safeQuery($query);
74
    }
75
76
    protected function DaftObjectDatabaseTable() : string
77
    {
78
        return preg_replace('/[^a-z]+/', '_', mb_strtolower($this->type));
79
    }
80
81
    protected static function QueryPartFromRefReturn(
82
        EasyDB $db,
83
        ReflectionType $refReturn,
84
        string $prop,
85
        array $nullables
86
    ) : string {
87
        if (
88
            $refReturn->isBuiltin()
89
        ) {
90
            $queryPart = $db->escapeIdentifier($prop);
91
            switch ($refReturn->__toString()) {
92
                case 'string':
93
                    $queryPart .= ' VARCHAR(255)';
94
                break;
95
                case 'float':
96
                    $queryPart .= ' REAL';
97
                break;
98
                case 'int':
99
                case 'bool':
100
                    $queryPart .= ' INTEGER';
101
                break;
102
                default:
103
                    throw new RuntimeException(
104
                        sprintf(
105
                            'Unsupported data type! (%s)',
106
                            $refReturn->__toString()
107
                        )
108
                    );
109
            }
110
            if (false === in_array($prop, $nullables, true)) {
111
                $queryPart .= ' NOT NULL';
112
            }
113
114
            return $queryPart;
115
        }
116
117
        throw new RuntimeException('Only supports builtins');
118
    }
119
}
120