Completed
Push — master ( 505b3b...86fb92 )
by SignpostMarv
01:46
created

TestObjectRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 89.13%

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 41
cts 46
cp 0.8913
rs 10
c 0
b 0
f 0
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A DaftObjectDatabaseTable() 0 3 1
C __construct() 0 70 12
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
17
class TestObjectRepository extends AbstractDaftObjectEasyDBRepository
18
{
19 6
    protected function __construct(string $type, EasyDB $db)
20
    {
21 6
        parent::__construct($type, $db);
22
        $query =
23
            'CREATE TABLE ' .
24 6
            $db->escapeIdentifier($this->DaftObjectDatabaseTable()) .
25 6
            ' (';
26
27 6
        $queryParts = [];
28
29 6
        $ref = new ReflectionClass($type);
30 6
        $nullables = $type::DaftObjectNullableProperties();
31
32 6
        foreach ($type::DaftObjectProperties() as $i => $prop) {
33 6
            $methodName = 'Get' . ucfirst($prop);
34 6
            if ($ref->hasMethod($methodName) === true) {
35 6
                $refReturn = $ref->getMethod($methodName)->getReturnType();
36
37
                if (
38 6
                    ($refReturn instanceof ReflectionType) &&
39 6
                    $refReturn->isBuiltin()
40
                ) {
41 6
                    $queryPart = $db->escapeIdentifier($prop);
42 6
                    switch ($refReturn->__toString()) {
43 6
                        case 'string':
44 6
                            $queryPart .= ' VARCHAR(255)';
45 6
                        break;
46 6
                        case 'float':
47 6
                            $queryPart .= ' REAL';
48 6
                        break;
49 6
                        case 'int':
50 6
                        case 'bool':
51 6
                            $queryPart .= ' INTEGER';
52 6
                        break;
53
                        default:
54
                            throw new RuntimeException(
55
                                sprintf(
56
                                    'Unsupported data type! (%s)',
57
                                    $refReturn->__toString()
58
                                )
59
                            );
60
                    }
61 6
                    if (in_array($prop, $nullables, true) === false) {
62 6
                        $queryPart .= ' NOT NULL';
63
                    }
64
65 6
                    $queryParts[] = $queryPart;
66
                } else {
67
                    throw new RuntimeException('Only supports builtins');
68
                }
69
            }
70
        }
71
72 6
        $primaryKeyCols = [];
73 6
        foreach ($type::DaftObjectIdProperties() as $col) {
74 6
            $primaryKeyCols[] = $db->escapeIdentifier($col);
75
        }
76
77 6
        if (count($primaryKeyCols) > 0) {
78 6
            $queryParts[] =
79
                'PRIMARY KEY (' .
80 6
                implode(',', $primaryKeyCols) .
81 6
                ')';
82
        }
83
84
        $query .=
85 6
            implode(',', $queryParts) .
86 6
            ');';
87
88 6
        $db->safeQuery($query);
89 6
    }
90
91 6
    protected function DaftObjectDatabaseTable() : string
92
    {
93 6
        return preg_replace('/[^a-z]+/', '_', mb_strtolower($this->type));
94
    }
95
}
96