Completed
Push — master ( a99a7b...23898a )
by Fabien
51:24
created

DataService::getRecord()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Fab\Vidi\Service;
4
5
/*
6
 * This file is part of the Ecodev/Speciality. 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 $matches
79
     */
80
    public function delete(string $tableName, array $matches): void
81
    {
82
        $connection = $this->getConnection($tableName);
83
        $connection->delete(
84
            $tableName,
85
            $matches
86
        );
87
    }
88
89
    /**
90
     * @param array $demand
91
     * @param QueryBuilder $queryBuilder
92
     * @return void
93
     */
94
    protected function addDemandConstraints(array $demand, $queryBuilder): void
95
    {
96
        $expressions = [];
97
        foreach ($demand as $fieldName => $value) {
98
            if (is_numeric($value)) {
99
                $expressions[] = $queryBuilder->expr()->eq(
100
                    $fieldName,
101
                    $value
102
                );
103
            } elseif (is_string($value)) {
104
                $expressions[] = $queryBuilder->expr()->eq(
105
                    $fieldName,
106
                    $queryBuilder->expr()->literal($value)
107
                );
108
            } elseif (is_array($value)) {
109
                $expressions[] = $queryBuilder->expr()->in(
110
                    $fieldName,
111
                    $value
112
                );
113
            }
114
        }
115
        foreach ($expressions as $expression) {
116
            $queryBuilder->andWhere($expression);
117
        }
118
    }
119
120
    /**
121
     * @return object|DeletedRestriction
122
     */
123
    protected function getDeletedRestriction(): DeletedRestriction
124
    {
125
        return GeneralUtility::makeInstance(DeletedRestriction::class);
126
    }
127
128
    /**
129
     * @return object|HiddenRestriction
130
     */
131
    protected function getHiddenRestriction(): HiddenRestriction
132
    {
133
        return GeneralUtility::makeInstance(HiddenRestriction::class);
134
    }
135
136
    /**
137
     * @param string $tableName
138
     * @return object|Connection
139
     */
140
    protected function getConnection($tableName): Connection
141
    {
142
        /** @var ConnectionPool $connectionPool */
143
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
144
        return $connectionPool->getConnectionForTable($tableName);
145
    }
146
147
    /**
148
     * @param string $tableName
149
     * @return object|QueryBuilder
150
     */
151
    protected function getQueryBuilder($tableName): QueryBuilder
152
    {
153
        /** @var ConnectionPool $connectionPool */
154
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
155
        return $connectionPool->getQueryBuilderForTable($tableName);
156
    }
157
}
158