Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.44% |
57 / 61 |
|
77.78% |
7 / 9 |
CRAP | |
0.00% |
0 / 1 |
| QtiItemAssetCompiler | |
93.44% |
57 / 61 |
|
77.78% |
7 / 9 |
20.11 | |
0.00% |
0 / 1 |
| extractAndCopyAssetFiles | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
7 | |||
| replaceWithExternalSource | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| isBlacklisted | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| resolve | |
72.73% |
8 / 11 |
|
0.00% |
0 / 1 |
5.51 | |||
| getReplacementName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| copyAssetFileToPublicDirectory | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| getQtiItemAssetReplacer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getXIncludeAdditionalAssetInjector | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isInlineAssetAndShouldNotBeReplaced | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 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) 2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\taoQtiItem\model\compile\QtiAssetCompiler; |
| 25 | |
| 26 | use InvalidArgumentException; |
| 27 | use oat\oatbox\config\ConfigurationService; |
| 28 | use oat\oatbox\filesystem\Directory; |
| 29 | use oat\tao\model\media\sourceStrategy\HttpSource; |
| 30 | use oat\tao\model\media\sourceStrategy\InlineSource; |
| 31 | use oat\taoItems\model\media\ItemMediaResolver; |
| 32 | use oat\taoQtiItem\model\compile\QtiAssetReplacer\QtiItemAssetReplacer; |
| 33 | use oat\taoQtiItem\model\compile\QtiItemCompilerAssetBlacklist; |
| 34 | use oat\taoQtiItem\model\pack\QtiAssetPacker\PackedAsset; |
| 35 | use oat\taoQtiItem\model\qti\AssetParser; |
| 36 | use oat\taoQtiItem\model\qti\exception\XIncludeException; |
| 37 | use oat\taoQtiItem\model\qti\Item; |
| 38 | use oat\taoQtiItem\model\qti\XIncludeLoader; |
| 39 | |
| 40 | class QtiItemAssetCompiler extends ConfigurationService |
| 41 | { |
| 42 | /** |
| 43 | * @param Item $qtiItem |
| 44 | * @param Directory $publicDirectory |
| 45 | * @param ItemMediaResolver $resolver |
| 46 | * @return PackedAsset[] |
| 47 | * @throws XIncludeException |
| 48 | * @throws \common_Exception |
| 49 | */ |
| 50 | public function extractAndCopyAssetFiles( |
| 51 | Item $qtiItem, |
| 52 | Directory $publicDirectory, |
| 53 | ItemMediaResolver $resolver |
| 54 | ): array { |
| 55 | $assetParser = new AssetParser($qtiItem, $publicDirectory); |
| 56 | $assetParser->setGetSharedLibraries(false); |
| 57 | $assetParser->setGetXinclude(true); |
| 58 | |
| 59 | $xincludeLoader = new XIncludeLoader($qtiItem, $resolver); |
| 60 | $xincludeLoader->load(); |
| 61 | |
| 62 | $packedAssets = []; |
| 63 | foreach ($assetParser->extract() as $type => $assets) { |
| 64 | foreach ($assets as $key => $assetUrl) { |
| 65 | if ($this->isBlacklisted($assetUrl)) { |
| 66 | continue; |
| 67 | } |
| 68 | $packedAsset = $this->resolve($resolver, $assetUrl, $type); |
| 69 | $replacement = $this->getReplacementName($packedAsset); |
| 70 | $packedAsset->setReplacedBy($replacement); |
| 71 | |
| 72 | if ($this->isInlineAssetAndShouldNotBeReplaced($packedAsset)) { |
| 73 | continue; |
| 74 | } |
| 75 | $this->getXIncludeAdditionalAssetInjector()->injectNonRDFXincludeRelatedAssets( |
| 76 | $qtiItem, |
| 77 | $publicDirectory, |
| 78 | $packedAsset |
| 79 | ); |
| 80 | |
| 81 | if ($type != 'xinclude') { |
| 82 | if ($this->getQtiItemAssetReplacer()->shouldBeReplaced($packedAsset)) { |
| 83 | $packedAsset = $this->replaceWithExternalSource($packedAsset, $qtiItem); |
| 84 | } else { |
| 85 | $this->copyAssetFileToPublicDirectory($publicDirectory, $packedAsset); |
| 86 | } |
| 87 | } |
| 88 | $packedAssets[$assetUrl] = $packedAsset; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return $packedAssets; |
| 93 | } |
| 94 | |
| 95 | private function replaceWithExternalSource(PackedAsset $packedAsset, Item $qtiItem): PackedAsset |
| 96 | { |
| 97 | $qtiItemAssetReplacer = $this->getQtiItemAssetReplacer(); |
| 98 | return $qtiItemAssetReplacer->replace( |
| 99 | $packedAsset, |
| 100 | $qtiItem->getIdentifier() |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | private function isBlacklisted(string $assetUrl): bool |
| 105 | { |
| 106 | return $this->getServiceLocator() |
| 107 | ->get(QtiItemCompilerAssetBlacklist::SERVICE_ID) |
| 108 | ->isBlacklisted($assetUrl); |
| 109 | } |
| 110 | |
| 111 | private function resolve(ItemMediaResolver $resolver, string $assetUrl, string $type): PackedAsset |
| 112 | { |
| 113 | $mediaAsset = $resolver->resolve($assetUrl); |
| 114 | $mediaSource = $mediaAsset->getMediaSource(); |
| 115 | |
| 116 | if ($mediaSource instanceof HttpSource || $mediaSource instanceof InlineSource) { |
| 117 | return new PackedAsset($type, $mediaAsset, $assetUrl); |
| 118 | } |
| 119 | |
| 120 | $fileInfo = $mediaSource->getFileInfo($mediaAsset->getMediaIdentifier()); |
| 121 | if (isset($fileInfo['link'])) { |
| 122 | $link = $fileInfo['link']; |
| 123 | } elseif (isset($fileInfo['filePath'])) { |
| 124 | $link = $fileInfo['filePath']; |
| 125 | } else { |
| 126 | throw new InvalidArgumentException(sprintf('Item asset %s cannot be resolved.', $assetUrl)); |
| 127 | } |
| 128 | |
| 129 | return new PackedAsset($type, $mediaAsset, $link); |
| 130 | } |
| 131 | |
| 132 | private function getReplacementName(PackedAsset $packedAsset): string |
| 133 | { |
| 134 | $link = $packedAsset->getMediaAsset()->getMediaIdentifier(); |
| 135 | return uniqid() . '_' . $packedAsset->getMediaAsset()->getMediaSource()->getBaseName($link); |
| 136 | } |
| 137 | |
| 138 | private function copyAssetFileToPublicDirectory(Directory $publicDirectory, PackedAsset $packedAsset): bool |
| 139 | { |
| 140 | $mediaAsset = $packedAsset->getMediaAsset(); |
| 141 | $mediaSource = $mediaAsset->getMediaSource(); |
| 142 | |
| 143 | $content = $mediaSource->getFileStream($mediaAsset->getMediaIdentifier()); |
| 144 | |
| 145 | $this->logInfo(sprintf( |
| 146 | 'Copying %s reference %s to file %s', |
| 147 | $packedAsset->getType(), |
| 148 | $mediaAsset->getMediaIdentifier(), |
| 149 | $packedAsset->getReplacedBy() |
| 150 | )); |
| 151 | |
| 152 | return $publicDirectory->getFile($packedAsset->getReplacedBy())->write($content); |
| 153 | } |
| 154 | |
| 155 | private function getQtiItemAssetReplacer(): QtiItemAssetReplacer |
| 156 | { |
| 157 | return $this->getServiceLocator()->get(QtiItemAssetReplacer::SERVICE_ID); |
| 158 | } |
| 159 | |
| 160 | private function getXIncludeAdditionalAssetInjector(): XIncludeAdditionalAssetInjector |
| 161 | { |
| 162 | return $this->getServiceLocator()->get(XIncludeAdditionalAssetInjector::class); |
| 163 | } |
| 164 | |
| 165 | private function isInlineAssetAndShouldNotBeReplaced(PackedAsset $packedAsset): bool |
| 166 | { |
| 167 | return $packedAsset->getMediaAsset()->getMediaSource() instanceof InlineSource |
| 168 | && !$this->getQtiItemAssetReplacer()->shouldBeReplaced($packedAsset); |
| 169 | } |
| 170 | } |