Completed
Push — master ( dfc64f...43826b )
by Fabien
51:13
created

DataService   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 0
dl 0
loc 154
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecord() 0 14 2
A getRecords() 0 12 1
A insert() 0 9 1
A update() 0 9 1
A delete() 0 8 1
B addDemandConstraints() 0 25 6
A getDeletedRestriction() 0 4 1
A getHiddenRestriction() 0 4 1
A getConnection() 0 6 1
A getQueryBuilder() 0 6 1
1
<?php
2
3
namespace Fab\Vidi\Service;
4
5
/*
6
 * This file is part of the Fab/Vidi. project.
7
 */
8
9
use TYPO3\CMS\Core\Database\Connection;
10
use TYPO3\CMS\Core\Database\ConnectionPool;
11
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
12
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
13
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
14
use TYPO3\CMS\Core\SingletonInterface;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
17
/**
18
 * Class DataService
19
 */
20
class DataService implements SingletonInterface
21
{
22
23
    /**
24
     * @param string $tableName
25
     * @param array $demand
26
     * @return array
27
     */
28
    public function getRecord(string $tableName, array $demand = []): array
29
    {
30
        /** @var QueryBuilder $queryBuilder */
31
        $queryBuilder = $this->getQueryBuilder($tableName);
32
        $queryBuilder
33
            ->select('*')
34
            ->from($tableName);
35
36
        $this->addDemandConstraints($demand, $queryBuilder);
37
        $record = $queryBuilder->execute()->fetch();
38
        return is_array($record)
39
            ? $record
40
            : [];
41
    }
42
43
    /**
44
     * @param string $tableName
45
     * @param array $demand
46
     * @return array
47
     */
48
    public function getRecords(string $tableName, array $demand = []): array
49
    {
50
        /** @var QueryBuilder $queryBuilder */
51
        $queryBuilder = $this->getQueryBuilder($tableName);
52
        $queryBuilder
53
            ->select('*')
54
            ->from($tableName);
55
56
        $this->addDemandConstraints($demand, $queryBuilder);
57
58
        return $queryBuilder->execute()->fetchAll();
59
    }
60
61
    /**
62
     * @param string $tableName
63
     * @param array $values
64
     * @return int
65
     */
66
    public function insert(string $tableName, array $values): int
67
    {
68
        $connection = $this->getConnection($tableName);
69
        $connection->insert(
70
            $tableName,
71
            $values
72
        );
73
        return (int)$connection->lastInsertId();
74
    }
75
76
    /**
77
     * @param string $tableName
78
     * @param array $values
79
     * @param array $identifiers
80
     * @return void
81
     */
82
    public function update(string $tableName, array $values, array $identifiers): void
83
    {
84
        $connection = $this->getConnection($tableName);
85
        $connection->update(
86
            $tableName,
87
            $values,
88
            $identifiers
89
        );
90
    }
91
92
    /**
93
     * @param string $tableName
94
     * @param array $identifiers
95
     */
96
    public function delete(string $tableName, array $identifiers): void
97
    {
98
        $connection = $this->getConnection($tableName);
99
        $connection->delete(
100
            $tableName,
101
            $identifiers
102
        );
103
    }
104
105
    /**
106
     * @param array $demand
107
     * @param QueryBuilder $queryBuilder
108
     * @return void
109
     */
110
    protected function addDemandConstraints(array $demand, $queryBuilder): void
111
    {
112
        $expressions = [];
113
        foreach ($demand as $fieldName => $value) {
114
            if (is_numeric($value)) {
115
                $expressions[] = $queryBuilder->expr()->eq(
116
                    $fieldName,
117
                    $value
118
                );
119
            } elseif (is_string($value)) {
120
                $expressions[] = $queryBuilder->expr()->eq(
121
                    $fieldName,
122
                    $queryBuilder->expr()->literal($value)
123
                );
124
            } elseif (is_array($value)) {
125
                $expressions[] = $queryBuilder->expr()->in(
126
                    $fieldName,
127
                    $value
128
                );
129
            }
130
        }
131
        foreach ($expressions as $expression) {
132
            $queryBuilder->andWhere($expression);
133
        }
134
    }
135
136
    /**
137
     * @return object|DeletedRestriction
138
     */
139
    protected function getDeletedRestriction(): DeletedRestriction
140
    {
141
        return GeneralUtility::makeInstance(DeletedRestriction::class);
142
    }
143
144
    /**
145
     * @return object|HiddenRestriction
146
     */
147
    protected function getHiddenRestriction(): HiddenRestriction
148
    {
149
        return GeneralUtility::makeInstance(HiddenRestriction::class);
150
    }
151
152
    /**
153
     * @param string $tableName
154
     * @return object|Connection
155
     */
156
    protected function getConnection($tableName): Connection
157
    {
158
        /** @var ConnectionPool $connectionPool */
159
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
160
        return $connectionPool->getConnectionForTable($tableName);
161
    }
162
163
    /**
164
     * @param string $tableName
165
     * @return object|QueryBuilder
166
     */
167
    protected function getQueryBuilder($tableName): QueryBuilder
168
    {
169
        /** @var ConnectionPool $connectionPool */
170
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
171
        return $connectionPool->getQueryBuilderForTable($tableName);
172
    }
173
}
174