Completed
Push — master ( d5827e...097023 )
by
unknown
15:13
created

FileCollectionRepository::findAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Core\Resource;
17
18
use TYPO3\CMS\Core\Database\ConnectionPool;
19
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
20
use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
21
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
24
25
/**
26
 * Repository for accessing file collections stored in the database
27
 */
28
class FileCollectionRepository
29
{
30
    /**
31
     * @var string
32
     */
33
    protected $table = 'sys_file_collection';
34
35
    /**
36
     * @var string
37
     */
38
    protected $typeField = 'type';
39
40
    /**
41
     * Finds a record collection by uid.
42
     *
43
     * @param int $uid The uid to be looked up
44
     * @return Collection\AbstractFileCollection|null
45
     * @throws Exception\ResourceDoesNotExistException
46
     */
47
    public function findByUid($uid)
48
    {
49
        $object = null;
50
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
51
52
        if ($this->getEnvironmentMode() === 'FE') {
53
            $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
54
        } else {
55
            $queryBuilder->getRestrictions()
56
                ->removeAll()
57
                ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
58
        }
59
60
        $data = $queryBuilder->select('*')
61
            ->from($this->table)
62
            ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)))
63
            ->execute()
64
            ->fetch();
65
        if (is_array($data)) {
66
            $object = $this->createDomainObject($data);
67
        }
68
        if ($object === null) {
69
            throw new ResourceDoesNotExistException('Could not find row with uid "' . $uid . '" in table "' . $this->table . '"', 1314354066);
70
        }
71
        return $object;
72
    }
73
74
    /**
75
     * Finds record collection by type.
76
     *
77
     * @param string $type Type to be looked up
78
     * @return Collection\AbstractFileCollection[]|null
79
     */
80
    public function findByType($type)
81
    {
82
        $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
83
            ->getQueryBuilderForTable($this->table)
84
            ->expr();
85
86
        return $this->queryMultipleRecords([
87
            $expressionBuilder->eq($this->typeField, $expressionBuilder->literal($type))
88
        ]);
89
    }
90
91
    /**
92
     * Queries for multiple records for the given conditions.
93
     *
94
     * @param array $conditions Conditions concatenated with AND for query
95
     * @return Collection\AbstractFileCollection[]|null
96
     */
97
    protected function queryMultipleRecords(array $conditions = [])
98
    {
99
        $result = null;
100
101
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
102
        $queryBuilder->getRestrictions()
103
            ->removeAll()
104
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
105
106
        $queryBuilder->select('*')
107
            ->from($this->table);
108
109
        if (!empty($conditions)) {
110
            $queryBuilder->where(...$conditions);
111
        }
112
113
        $data = $queryBuilder->execute()->fetchAll();
114
        if (!empty($data)) {
115
            $result = $this->createMultipleDomainObjects($data);
116
        }
117
118
        return $result;
119
    }
120
121
    /**
122
     * Creates multiple record collection domain objects.
123
     *
124
     * @param array $data Array of multiple database records to be reconstituted
125
     * @return \TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
126
     */
127
    protected function createMultipleDomainObjects(array $data)
128
    {
129
        $collections = [];
130
        foreach ($data as $collection) {
131
            $collections[] = $this->createDomainObject($collection);
132
        }
133
        return $collections;
134
    }
135
136
    /**
137
     * Function to return the current TYPO3_MODE (FE/BE) based on $GLOBALS[TSFE].
138
     * This function can be mocked in unit tests to be able to test frontend behaviour.
139
     *
140
     * @return string
141
     */
142
    protected function getEnvironmentMode(): string
143
    {
144
        return ($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController ? 'FE' : 'BE';
145
    }
146
147
    /**
148
     * Creates a record collection domain object.
149
     *
150
     * @param array $record Database record to be reconstituted
151
     *
152
     * @return Collection\AbstractFileCollection
153
     */
154
    protected function createDomainObject(array $record)
155
    {
156
        return $this->getFileFactory()->createCollectionObject($record);
157
    }
158
159
    /**
160
     * Gets the file factory.
161
     *
162
     * @return ResourceFactory
163
     */
164
    protected function getFileFactory()
165
    {
166
        return GeneralUtility::makeInstance(ResourceFactory::class);
167
    }
168
169
    /**
170
     * Finds all record collections.
171
     *
172
     * @return Collection\AbstractFileCollection[]|null
173
     */
174
    public function findAll()
175
    {
176
        return $this->queryMultipleRecords();
177
    }
178
}
179