9
9
namespace Xibo\Factory;
11
12
use Xibo\Entity\RequiredFile;
12
13
use Xibo\Exception\NotFoundException;
13
use Xibo\Service\LogServiceInterface;
14
use Xibo\Service\SanitizerServiceInterface;
15
use Xibo\Storage\StorageServiceInterface;
15
use Xibo\Helper\Sanitize;
16
use Xibo\Storage\PDOConnect;
18
* Class RequiredFileFactory
19
* @package Xibo\Factory
21
18
class RequiredFileFactory extends BaseFactory
23
private $statement = null;
27
* @param StorageServiceInterface $store
28
* @param LogServiceInterface $log
29
* @param SanitizerServiceInterface $sanitizerService
31
public function __construct($store, $log, $sanitizerService)
33
$this->setCommonDependencies($store, $log, $sanitizerService);
37
* @return RequiredFile
39
public function createEmpty()
41
return new RequiredFile($this->getStore(), $this->getLog());
45
* @param array $params
46
* @return RequiredFile[]
48
private function query($params)
52
if ($this->statement === null) {
53
$this->statement = $this->getStore()->getConnection()->prepare('
56
WHERE `displayId` = :displayId
58
AND `itemId` = :itemId
62
$this->statement->execute($params);
64
foreach ($this->statement->fetchAll(\PDO::FETCH_ASSOC) as $item) {
65
$files[] = $this->createEmpty()->hydrate($item);
72
* @param int $displayId
73
* @param int $layoutId
74
* @return RequiredFile
75
* @throws NotFoundException
77
public function getByDisplayAndLayout($displayId, $layoutId)
79
$result = $this->query(['displayId' => $displayId, 'type' => 'L', 'itemId' => $layoutId]);
81
if (count($result) <= 0)
82
throw new NotFoundException(__('Required file not found for Display and Layout Combination'));
88
* @param int $displayId
90
* @return RequiredFile
91
* @throws NotFoundException
93
public function getByDisplayAndMedia($displayId, $mediaId)
95
$result = $this->query(['displayId' => $displayId, 'type' => 'M', 'itemId' => $mediaId]);
97
if (count($result) <= 0)
98
throw new NotFoundException(__('Required file not found for Display and Media Combination'));
104
* @param int $displayId
105
* @param int $widgetId
106
* @return RequiredFile
107
* @throws NotFoundException
109
public function getByDisplayAndWidget($displayId, $widgetId)
111
$result = $this->query(['displayId' => $displayId, 'type' => 'W', 'itemId' => $widgetId]);
113
if (count($result) <= 0)
114
throw new NotFoundException(__('Required file not found for Display and Layout Widget'));
21
* @param string $nonce
22
* @return RequiredFile
23
* @throws NotFoundException
25
public static function getByNonce($nonce)
27
$nonce = RequiredFileFactory::query(null, ['nonce' => $nonce]);
29
if (count($nonce) <= 0)
30
throw new NotFoundException();
36
* @param int $displayId
37
* @param int $layoutId
38
* @return RequiredFile
39
* @throws NotFoundException
41
public static function getByDisplayAndLayout($displayId, $layoutId)
43
$files = RequiredFileFactory::query(null, ['displayId' => $displayId, 'layoutId' => $layoutId]);
45
if (count($files) <= 0)
46
throw new NotFoundException();
52
* @param int $displayId
54
* @return RequiredFile
55
* @throws NotFoundException
57
public static function getByDisplayAndMedia($displayId, $mediaId)
59
$files = RequiredFileFactory::query(null, ['displayId' => $displayId, 'mediaId' => $mediaId]);
61
if (count($files) <= 0)
62
throw new NotFoundException();
68
* @param int $displayId
69
* @param int $layoutId
70
* @param int $regionId
72
* @return RequiredFile
73
* @throws NotFoundException
75
public static function getByDisplayAndResource($displayId, $layoutId, $regionId, $mediaId)
77
$files = RequiredFileFactory::query(null, ['displayId' => $displayId, 'layoutId' => $layoutId, 'regionId' => $regionId, 'mediaId' => $mediaId]);
79
if (count($files) <= 0)
80
throw new NotFoundException();
120
86
* Create for layout
121
87
* @param $displayId
122
89
* @param $layoutId
125
92
* @return RequiredFile
127
public function createForLayout($displayId, $layoutId, $size, $path)
94
public static function createForLayout($displayId, $requestKey, $layoutId, $size, $path)
130
$requiredFile = $this->getByDisplayAndLayout($displayId, $layoutId);
97
$nonce = RequiredFileFactory::getByDisplayAndLayout($displayId, $layoutId);
132
99
catch (NotFoundException $e) {
133
$requiredFile = $this->createEmpty();
100
$nonce = new RequiredFile();
136
$requiredFile->displayId = $displayId;
137
$requiredFile->type = 'L';
138
$requiredFile->itemId = $layoutId;
139
$requiredFile->size = $size;
140
$requiredFile->path = $path;
141
return $requiredFile;
103
$nonce->displayId = $displayId;
104
$nonce->requestKey = $requestKey;
105
$nonce->layoutId = $layoutId;
106
$nonce->size = $size;
107
$nonce->storedAs = $path;
145
112
* Create for Get Resource
146
113
* @param $displayId
148
118
* @return RequiredFile
150
public function createForGetResource($displayId, $widgetId)
120
public static function createForGetResource($displayId, $requestKey, $layoutId, $regionId, $mediaId)
153
$requiredFile = $this->getByDisplayAndWidget($displayId, $widgetId);
123
$nonce = RequiredFileFactory::getByDisplayAndResource($displayId, $layoutId, $regionId, $mediaId);
155
125
catch (NotFoundException $e) {
156
$requiredFile = $this->createEmpty();
126
$nonce = new RequiredFile();
159
$requiredFile->displayId = $displayId;
160
$requiredFile->type = 'W';
161
$requiredFile->itemId = $widgetId;
162
return $requiredFile;
129
$nonce->displayId = $displayId;
130
$nonce->requestKey = $requestKey;
131
$nonce->layoutId = $layoutId;
132
$nonce->regionId = $regionId;
133
$nonce->mediaId = $mediaId;
166
138
* Create for Media
167
139
* @param $displayId
168
141
* @param $mediaId
171
144
* @return RequiredFile
173
public function createForMedia($displayId, $mediaId, $size, $path)
146
public static function createForMedia($displayId, $requestKey, $mediaId, $size, $path)
176
$requiredFile = $this->getByDisplayAndMedia($displayId, $mediaId);
149
$nonce = RequiredFileFactory::getByDisplayAndMedia($displayId, $mediaId);
178
151
catch (NotFoundException $e) {
179
$requiredFile = $this->createEmpty();
182
$requiredFile->displayId = $displayId;
183
$requiredFile->type = 'M';
184
$requiredFile->itemId = $mediaId;
185
$requiredFile->size = $size;
186
$requiredFile->path = $path;
187
return $requiredFile;
152
$nonce = new RequiredFile();
155
$nonce->displayId = $displayId;
156
$nonce->requestKey = $requestKey;
157
$nonce->mediaId = $mediaId;
158
$nonce->size = $size;
159
$nonce->storedAs = $path;
163
public static function query($sortOrder = null, $filterBy = null)
169
`requiredfile`.requestKey,
170
`requiredfile`.nonce,
171
`requiredfile`.expiry,
172
`requiredfile`.lastUsed,
173
`requiredfile`.displayId,
175
`requiredfile`.storedAs,
176
`requiredfile`.layoutId,
177
`requiredfile`.regionId,
178
`requiredfile`.mediaId,
179
`requiredfile`.bytesRequested,
180
`requiredfile`.complete
185
if (Sanitize::getString('nonce', $filterBy) !== null) {
186
$sql .= ' AND requiredfile.nonce = :nonce';
187
$params['nonce'] = Sanitize::getString('nonce', $filterBy);
190
if (Sanitize::getInt('displayId', $filterBy) !== null) {
191
$sql .= ' AND requiredfile.displayId = :displayId';
192
$params['displayId'] = Sanitize::getInt('displayId', $filterBy);
195
if (Sanitize::getInt('layoutId', $filterBy) !== null) {
196
$sql .= ' AND requiredfile.layoutId = :layoutId';
197
$params['layoutId'] = Sanitize::getInt('layoutId', $filterBy);
200
if (Sanitize::getInt('regionId', $filterBy) !== null) {
201
$sql .= ' AND requiredfile.regionId = :regionId';
202
$params['regionId'] = Sanitize::getInt('regionId', $filterBy);
205
if (Sanitize::getInt('mediaId', $filterBy) !== null) {
206
$sql .= ' AND requiredfile.mediaId = :mediaId';
207
$params['mediaId'] = Sanitize::getInt('mediaId', $filterBy);
211
if (is_array($sortOrder))
212
$sql .= 'ORDER BY ' . implode(',', $sortOrder);
214
Log::sql($sql, $params);
216
foreach (PDOConnect::select($sql, $params) as $row) {
217
$entries[] = (new RequiredFile())->hydrate($row, ['intProperties' => ['expires', 'lastUsed', 'size']]);
b'\\ No newline at end of file'