Completed
Push — master ( 43826b...9b05ea )
by Fabien
52:38
created

DataService::getQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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 View Code Duplication
    public function getRecords(string $tableName, array $demand = []): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 $demand
64
     * @return int
65
     */
66 View Code Duplication
    public function count(string $tableName, array $demand = []): int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        /** @var QueryBuilder $queryBuilder */
69
        $queryBuilder = $this->getQueryBuilder($tableName);
70
        $queryBuilder
71
            ->count('*')
72
            ->from($tableName);
73
74
        $this->addDemandConstraints($demand, $queryBuilder);
75
76
        return (int)$queryBuilder->execute()->fetchColumn(0);
77
    }
78
79
    /**
80
     * @param string $tableName
81
     * @param array $values
82
     * @return int
83
     */
84
    public function insert(string $tableName, array $values): int
85
    {
86
        $connection = $this->getConnection($tableName);
87
        $connection->insert(
88
            $tableName,
89
            $values
90
        );
91
        return (int)$connection->lastInsertId();
92
    }
93
94
    /**
95
     * @param string $tableName
96
     * @param array $values
97
     * @param array $identifiers
98
     * @return void
99
     */
100
    public function update(string $tableName, array $values, array $identifiers): void
101
    {
102
        $connection = $this->getConnection($tableName);
103
        $connection->update(
104
            $tableName,
105
            $values,
106
            $identifiers
107
        );
108
    }
109
110
    /**
111
     * @param string $tableName
112
     * @param array $identifiers
113
     */
114
    public function delete(string $tableName, array $identifiers): void
115
    {
116
        $connection = $this->getConnection($tableName);
117
        $connection->delete(
118
            $tableName,
119
            $identifiers
120
        );
121
    }
122
123
    /**
124
     * @param array $demand
125
     * @param QueryBuilder $queryBuilder
126
     * @return void
127
     */
128
    protected function addDemandConstraints(array $demand, $queryBuilder): void
129
    {
130
        $expressions = [];
131
        foreach ($demand as $fieldName => $value) {
132
            if (is_numeric($value)) {
133
                $expressions[] = $queryBuilder->expr()->eq(
134
                    $fieldName,
135
                    $value
136
                );
137
            } elseif (is_string($value)) {
138
                $expressions[] = $queryBuilder->expr()->eq(
139
                    $fieldName,
140
                    $queryBuilder->expr()->literal($value)
141
                );
142
            } elseif (is_array($value)) {
143
                $expressions[] = $queryBuilder->expr()->in(
144
                    $fieldName,
145
                    $value
146
                );
147
            }
148
        }
149
        foreach ($expressions as $expression) {
150
            $queryBuilder->andWhere($expression);
151
        }
152
    }
153
154
    /**
155
     * @return object|DeletedRestriction
156
     */
157
    protected function getDeletedRestriction(): DeletedRestriction
158
    {
159
        return GeneralUtility::makeInstance(DeletedRestriction::class);
160
    }
161
162
    /**
163
     * @return object|HiddenRestriction
164
     */
165
    protected function getHiddenRestriction(): HiddenRestriction
166
    {
167
        return GeneralUtility::makeInstance(HiddenRestriction::class);
168
    }
169
170
    /**
171
     * @param string $tableName
172
     * @return object|Connection
173
     */
174
    protected function getConnection($tableName): Connection
175
    {
176
        /** @var ConnectionPool $connectionPool */
177
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
178
        return $connectionPool->getConnectionForTable($tableName);
179
    }
180
181
    /**
182
     * @param string $tableName
183
     * @return object|QueryBuilder
184
     */
185
    protected function getQueryBuilder($tableName): QueryBuilder
186
    {
187
        /** @var ConnectionPool $connectionPool */
188
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
189
        return $connectionPool->getQueryBuilderForTable($tableName);
190
    }
191
}
192