Test Failed
Push — master ( d3f519...f62cab )
by SignpostMarv
02:44
created

TestObjectRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 13
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
                /**
43
                * @var ReflectionType $refReturn
44
                */
45
                $refReturn = $ref->getMethod($methodName)->getReturnType();
46
                $queryPart =
47
                    $db->escapeIdentifier($prop) .
48
                    static::QueryPartTypeFromRefReturn($refReturn);
49
                if (false === in_array($prop, $nullables, true)) {
50
                    $queryPart .= ' NOT NULL';
51
                }
52
53
                $queryParts[] = $queryPart;
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]+/', '_', (string) mb_strtolower($this->type));
79
    }
80
81
    protected static function QueryPartTypeFromRefReturn(ReflectionType $refReturn) : string
82
    {
83
        if ($refReturn->isBuiltin()) {
84
            switch ($refReturn->__toString()) {
85
                case 'string':
86
                    return ' VARCHAR(255)';
87
                case 'float':
88
                    return ' REAL';
89
                case 'int':
90
                case 'bool':
91
                    return ' INTEGER';
92
                default:
93
                    throw new RuntimeException(
94
                        sprintf(
95
                            'Unsupported data type! (%s)',
96
                            $refReturn->__toString()
97
                        )
98
                    );
99
            }
100
        }
101
102
        throw new RuntimeException('Only supports builtins');
103
    }
104
}
105