Completed
Push — master ( edcd2a...894f69 )
by SignpostMarv
02:15
created

TestObjectRepository::QueryPartFromRefReturn()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 22
nc 4
nop 3
dl 0
loc 34
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
                $queryPart = static::QueryPartFromRefReturn(
43
                    $db,
44
                    $ref->getMethod($methodName)->getReturnType(),
45
                    $prop
46
                );
47
                if (false === in_array($prop, $nullables, true)) {
48
                    $queryPart .= ' NOT NULL';
49
                }
50
51
                $queryParts[] = $queryPart;
52
            }
53
        }
54
55
        $primaryKeyCols = [];
56
        foreach ($type::DaftObjectIdProperties() as $col) {
57
            $primaryKeyCols[] = $db->escapeIdentifier($col);
58
        }
59
60
        if (count($primaryKeyCols) > 0) {
61
            $queryParts[] =
62
                'PRIMARY KEY (' .
63
                implode(',', $primaryKeyCols) .
64
                ')';
65
        }
66
67
        $query .=
68
            implode(',', $queryParts) .
69
            ');';
70
71
        $db->safeQuery($query);
72
    }
73
74
    protected function DaftObjectDatabaseTable() : string
75
    {
76
        return preg_replace('/[^a-z]+/', '_', mb_strtolower($this->type));
77
    }
78
79
    protected static function QueryPartFromRefReturn(
80
        EasyDB $db,
81
        ? ReflectionType $refReturn,
82
        string $prop
83
    ) : string {
84
        if (
85
            ! is_null($refReturn) &&
86
            $refReturn->isBuiltin()
87
        ) {
88
            $queryPart = $db->escapeIdentifier($prop);
89
            switch ($refReturn->__toString()) {
90
                case 'string':
91
                    $queryPart .= ' VARCHAR(255)';
92
                break;
93
                case 'float':
94
                    $queryPart .= ' REAL';
95
                break;
96
                case 'int':
97
                case 'bool':
98
                    $queryPart .= ' INTEGER';
99
                break;
100
                default:
101
                    throw new RuntimeException(
102
                        sprintf(
103
                            'Unsupported data type! (%s)',
104
                            $refReturn->__toString()
105
                        )
106
                    );
107
            }
108
109
            return $queryPart;
110
        }
111
112
        throw new RuntimeException('Only supports builtins');
113
    }
114
}
115