Completed
Push — master ( 894f69...39feae )
by SignpostMarv
02:28
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 =
43
                    $db->escapeIdentifier($prop) .
44
                    static::QueryPartTypeFromRefReturn(
45
                    $ref->getMethod($methodName)->getReturnType()
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 QueryPartTypeFromRefReturn(
80
        ? ReflectionType $refReturn
81
    ) : string {
82
        if (
83
            ! is_null($refReturn) &&
84
            $refReturn->isBuiltin()
85
        ) {
86
            switch ($refReturn->__toString()) {
87
                case 'string':
88
                    return ' VARCHAR(255)';
89
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
90
                case 'float':
91
                    return ' REAL';
92
                break;
93
                case 'int':
94
                case 'bool':
95
                    return ' INTEGER';
96
                break;
97
                default:
98
                    throw new RuntimeException(
99
                        sprintf(
100
                            'Unsupported data type! (%s)',
101
                            $refReturn->__toString()
102
                        )
103
                    );
104
            }
105
        }
106
107
        throw new RuntimeException('Only supports builtins');
108
    }
109
}
110