Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.63% |
40 / 49 |
|
58.33% |
7 / 12 |
CRAP | |
0.00% |
0 / 1 |
ItemPack | |
81.63% |
40 / 49 |
|
58.33% |
7 / 12 |
31.52 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setAssets | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
setAsset | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
getAssets | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
JsonSerialize | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
getAssetEncoders | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setAssetEncoders | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
isNestedResourcesInclusion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setNestedResourcesInclusion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAssetKey | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
6.22 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2015-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace oat\taoItems\model\pack; |
25 | |
26 | use InvalidArgumentException; |
27 | use JsonSerializable; |
28 | use LogicException; |
29 | use oat\tao\helpers\Base64; |
30 | use oat\tao\model\media\MediaAsset; |
31 | use oat\tao\model\media\sourceStrategy\HttpSource; |
32 | use oat\taoMediaManager\model\MediaSource; |
33 | use tao_models_classes_FileNotFoundException as FileNotFoundException; |
34 | use tao_models_classes_service_StorageDirectory as StorageDirectory; |
35 | |
36 | /** |
37 | * The Item Pack represents the item package data produced by the compilation. |
38 | * |
39 | * @package taoItems |
40 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
41 | */ |
42 | class ItemPack implements JsonSerializable |
43 | { |
44 | /** |
45 | * The supported assets types |
46 | * @var string[] |
47 | */ |
48 | private static $assetTypes = [ |
49 | 'html', |
50 | 'document', |
51 | 'js', |
52 | 'css', |
53 | 'font', |
54 | 'img', |
55 | 'audio', |
56 | 'video', |
57 | 'xinclude', |
58 | 'apip', |
59 | 'pdf' |
60 | ]; |
61 | |
62 | /** |
63 | * The item type |
64 | * @var string |
65 | */ |
66 | private $type; |
67 | |
68 | /** |
69 | * The item data as arrays. Can be anything, just be careful of cyclic refs. |
70 | * @var array |
71 | */ |
72 | private $data = []; |
73 | |
74 | /** |
75 | * The item's required assets by type |
76 | * @var array |
77 | */ |
78 | private $assets = []; |
79 | |
80 | /** |
81 | * Determines what type of assets should be packed as well as packer |
82 | * @example array('css'=>'base64') |
83 | * @var array |
84 | */ |
85 | protected $assetEncoders = [ |
86 | 'html' => 'none', |
87 | 'document' => 'none', |
88 | 'js' => 'none', |
89 | 'css' => 'none', |
90 | 'font' => 'none', |
91 | 'img' => 'none', |
92 | 'audio' => 'none', |
93 | 'video' => 'none', |
94 | 'xinclude' => 'none', |
95 | 'apip' => 'none', |
96 | 'pdf' => 'none', |
97 | ]; |
98 | |
99 | /** |
100 | * Should be @import or url() processed |
101 | * @var bool |
102 | */ |
103 | protected $nestedResourcesInclusion = true; |
104 | |
105 | /** |
106 | * Creates an ItemPack with the required data. |
107 | * |
108 | * @param string $type the item type |
109 | * @param array $data the item data |
110 | * |
111 | * @throw InvalidArgumentException |
112 | */ |
113 | public function __construct($type, $data) |
114 | { |
115 | if (empty($type)) { |
116 | throw new InvalidArgumentException('Please provide and item type'); |
117 | } |
118 | if (!is_array($data)) { |
119 | throw new InvalidArgumentException('Please provide the item data as an array'); |
120 | } |
121 | $this->type = $type; |
122 | $this->data = $data; |
123 | } |
124 | |
125 | /** |
126 | * Get the item type |
127 | * @return string the type |
128 | */ |
129 | public function getType(): string |
130 | { |
131 | return $this->type; |
132 | } |
133 | |
134 | /** |
135 | * Get the item data |
136 | * @return array the data |
137 | */ |
138 | public function getData(): array |
139 | { |
140 | return $this->data; |
141 | } |
142 | |
143 | /** |
144 | * Set item's assets of a given type to the pack. |
145 | * |
146 | * @param string $type |
147 | * @param $assets |
148 | * @param StorageDirectory|null $publicDirectory |
149 | * @param bool $skipBinaries |
150 | * |
151 | * @throws ExceptionMissingEncoder |
152 | * @throws FileNotFoundException |
153 | */ |
154 | public function setAssets( |
155 | string $type, |
156 | $assets, |
157 | ?StorageDirectory $publicDirectory = null, |
158 | bool $skipBinaries = false |
159 | ): void { |
160 | if (!is_array($assets)) { |
161 | throw new InvalidArgumentException('Assets should be an array, "' . gettype($assets) . '" given'); |
162 | } |
163 | |
164 | foreach ($assets as $asset) { |
165 | $this->setAsset($type, $asset, $publicDirectory, $skipBinaries); |
166 | } |
167 | } |
168 | |
169 | /** |
170 | * @param string $type |
171 | * @param $asset |
172 | * @param StorageDirectory|null $publicDirectory |
173 | * @param bool $skipBinaries |
174 | * |
175 | * @throws ExceptionMissingEncoder |
176 | * @throws FileNotFoundException |
177 | */ |
178 | public function setAsset( |
179 | string $type, |
180 | $asset, |
181 | ?StorageDirectory $publicDirectory = null, |
182 | bool $skipBinaries = false |
183 | ): void { |
184 | if (!in_array($type, self::$assetTypes, true)) { |
185 | throw new InvalidArgumentException(sprintf( |
186 | 'Unknown asset type "%s", it should be either %s', |
187 | $type, |
188 | implode(', ', self::$assetTypes) |
189 | )); |
190 | } |
191 | |
192 | $encoder = EncoderService::singleton()->get($this->assetEncoders[$type], $publicDirectory); |
193 | $assetKey = $this->getAssetKey($asset); |
194 | if ($skipBinaries && Base64::isEncodedImage($asset->getMediaIdentifier())) { |
195 | return; |
196 | } |
197 | $this->assets[$type][$assetKey] = $encoder->encode($asset); |
198 | } |
199 | |
200 | /** |
201 | * Get item's assets of a given type. |
202 | * |
203 | * @param string $type the assets type, one of those who are supported |
204 | * @return string[] the list of assets' URL to load |
205 | */ |
206 | public function getAssets(string $type): array |
207 | { |
208 | if (!array_key_exists($type, $this->assets)) { |
209 | return []; |
210 | } |
211 | return $this->assets[$type]; |
212 | } |
213 | |
214 | /** |
215 | * How to serialize the pack in JSON. |
216 | * |
217 | * phpcs:disable PSR1.Methods.CamelCapsMethodName |
218 | */ |
219 | public function JsonSerialize() |
220 | { |
221 | return [ |
222 | 'type' => $this->type, |
223 | 'data' => $this->data, |
224 | 'assets' => $this->assets |
225 | ]; |
226 | } |
227 | // phpcs:enable PSR1.Methods.CamelCapsMethodName |
228 | |
229 | /** |
230 | * @return array |
231 | */ |
232 | public function getAssetEncoders(): array |
233 | { |
234 | return $this->assetEncoders; |
235 | } |
236 | |
237 | /** |
238 | * @param array $assetEncoders |
239 | */ |
240 | public function setAssetEncoders(array $assetEncoders): void |
241 | { |
242 | foreach ($assetEncoders as $type => $encoder) { |
243 | if ($encoder == '') { |
244 | $this->assetEncoders[$type] = 'none'; |
245 | } else { |
246 | $this->assetEncoders[$type] = $encoder; |
247 | } |
248 | } |
249 | } |
250 | |
251 | /** |
252 | * @return boolean |
253 | */ |
254 | public function isNestedResourcesInclusion(): bool |
255 | { |
256 | return $this->nestedResourcesInclusion; |
257 | } |
258 | |
259 | /** |
260 | * @param boolean $nestedResourcesInclusion |
261 | */ |
262 | public function setNestedResourcesInclusion(bool $nestedResourcesInclusion): void |
263 | { |
264 | $this->nestedResourcesInclusion = (bool)$nestedResourcesInclusion; |
265 | } |
266 | |
267 | /** |
268 | * @param string|MediaAsset $asset |
269 | * |
270 | * @throws FileNotFoundException |
271 | * |
272 | * @return string |
273 | */ |
274 | private function getAssetKey($asset): string |
275 | { |
276 | if (!$asset instanceof MediaAsset) { |
277 | if (!is_string($asset)) { |
278 | throw new LogicException('Item pack can only pack assets as string url or MediaAsset'); |
279 | } |
280 | return $asset; |
281 | } |
282 | |
283 | $mediaSource = $asset->getMediaSource(); |
284 | $mediaIdentifier = $asset->getMediaIdentifier(); |
285 | |
286 | if ( |
287 | $mediaSource instanceof MediaSource |
288 | || $mediaSource instanceof HttpSource |
289 | || Base64::isEncodedImage($mediaIdentifier) |
290 | ) { |
291 | return $mediaIdentifier; |
292 | } |
293 | |
294 | return $mediaSource->getBaseName($mediaIdentifier); |
295 | } |
296 | } |