Completed
Push — master ( c84407...ff395e )
by Fabien
51:43
created

DataService::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\SingletonInterface;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
15
/**
16
 * Class DataService
17
 */
18
class DataService implements SingletonInterface
19
{
20
21
    /**
22
     * @param string $tableName
23
     * @param array $demand
24
     * @return array
25
     */
26
    public function getRecord(string $tableName, array $demand = []): array
27
    {
28
        /** @var QueryBuilder $queryBuilder */
29
        $queryBuilder = $this->getQueryBuilder($tableName);
30
        $queryBuilder
31
            ->select('*')
32
            ->from($tableName);
33
34
        $this->addDemandConstraints($demand, $queryBuilder);
35
        $record = $queryBuilder->execute()->fetch();
36
        return is_array($record)
37
            ? $record
38
            : [];
39
    }
40
41
    /**
42
     * @param string $tableName
43
     * @param array $demand
44
     * @return array
45
     */
46 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...
47
    {
48
        /** @var QueryBuilder $queryBuilder */
49
        $queryBuilder = $this->getQueryBuilder($tableName);
50
        $queryBuilder
51
            ->select('*')
52
            ->from($tableName);
53
54
        $this->addDemandConstraints($demand, $queryBuilder);
55
56
        return $queryBuilder->execute()->fetchAll();
57
    }
58
59
    /**
60
     * @param string $tableName
61
     * @param array $demand
62
     * @return int
63
     */
64 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...
65
    {
66
        /** @var QueryBuilder $queryBuilder */
67
        $queryBuilder = $this->getQueryBuilder($tableName);
68
        $queryBuilder
69
            ->count('*')
70
            ->from($tableName);
71
72
        $this->addDemandConstraints($demand, $queryBuilder);
73
74
        return (int)$queryBuilder->execute()->fetchColumn(0);
75
    }
76
77
    /**
78
     * @param string $tableName
79
     * @param array $values
80
     * @return int
81
     */
82
    public function insert(string $tableName, array $values): int
83
    {
84
        $connection = $this->getConnection($tableName);
85
        $connection->insert(
86
            $tableName,
87
            $values
88
        );
89
        return (int)$connection->lastInsertId();
90
    }
91
92
    /**
93
     * @param string $tableName
94
     * @param array $values
95
     * @param array $identifiers
96
     * @return void
97
     */
98
    public function update(string $tableName, array $values, array $identifiers): void
99
    {
100
        $connection = $this->getConnection($tableName);
101
        $connection->update(
102
            $tableName,
103
            $values,
104
            $identifiers
105
        );
106
    }
107
108
    /**
109
     * @param string $tableName
110
     * @param array $identifiers
111
     */
112
    public function delete(string $tableName, array $identifiers): void
113
    {
114
        $connection = $this->getConnection($tableName);
115
        $connection->delete(
116
            $tableName,
117
            $identifiers
118
        );
119
    }
120
121
    /**
122
     * @param array $demand
123
     * @param QueryBuilder $queryBuilder
124
     * @return void
125
     */
126
    protected function addDemandConstraints(array $demand, $queryBuilder): void
127
    {
128
        $expressions = [];
129
        foreach ($demand as $fieldName => $value) {
130
            if (is_numeric($value)) {
131
                $expressions[] = $queryBuilder->expr()->eq(
132
                    $fieldName,
133
                    $value
134
                );
135
            } elseif (is_string($value)) {
136
                $expressions[] = $queryBuilder->expr()->eq(
137
                    $fieldName,
138
                    $queryBuilder->expr()->literal($value)
139
                );
140
            } elseif (is_array($value)) {
141
                $expressions[] = $queryBuilder->expr()->in(
142
                    $fieldName,
143
                    $value
144
                );
145
            }
146
        }
147
        foreach ($expressions as $expression) {
148
            $queryBuilder->andWhere($expression);
149
        }
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