Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
55 / 55 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
CopyService | |
100.00% |
55 / 55 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
copy | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
1 | |||
copyCSSFilesFrom | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
2 | |||
assertHasRequiredParameters | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 |
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) 2022 (original work) Open Assessment Technologies SA. |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoMediaManager\model\sharedStimulus\service; |
24 | |
25 | use oat\generis\model\data\Ontology; |
26 | use oat\taoMediaManager\model\fileManagement\FileManagement; |
27 | use oat\taoMediaManager\model\fileManagement\FileSourceUnserializer; |
28 | use oat\taoMediaManager\model\sharedStimulus\CopyCommand; |
29 | use oat\taoMediaManager\model\sharedStimulus\css\dto\ListStylesheets as ListStylesheetsDTO; |
30 | use oat\taoMediaManager\model\sharedStimulus\css\repository\StylesheetRepository; |
31 | use oat\taoMediaManager\model\sharedStimulus\css\service\ListStylesheetsService; |
32 | use oat\taoMediaManager\model\sharedStimulus\dto\SharedStimulusInstanceData; |
33 | use oat\taoMediaManager\model\sharedStimulus\SharedStimulus; |
34 | use InvalidArgumentException; |
35 | use oat\taoMediaManager\model\TaoMediaOntology; |
36 | |
37 | class CopyService |
38 | { |
39 | private const NAMESPACE_TEMP_FILES = 'MediaManagerCopyService'; |
40 | |
41 | /** @var Ontology */ |
42 | private $ontology; |
43 | |
44 | /** @var StoreService */ |
45 | private $sharedStimulusStoreService; |
46 | |
47 | /** @var ListStylesheetsService */ |
48 | private $listStylesheetsService; |
49 | |
50 | /** @var StylesheetRepository */ |
51 | private $stylesheetRepository; |
52 | |
53 | /** @var FileSourceUnserializer */ |
54 | private $fileSourceUnserializer; |
55 | |
56 | /** @var FileManagement */ |
57 | private $fileManagement; |
58 | |
59 | /** @var TempFileWriter */ |
60 | private $tempFileWriter; |
61 | |
62 | public function __construct( |
63 | Ontology $ontology, |
64 | StoreService $sharedStimulusStoreService, |
65 | ListStylesheetsService $listStylesheetsService, |
66 | StylesheetRepository $stylesheetRepository, |
67 | FileSourceUnserializer $fileSourceUnserializer, |
68 | FileManagement $fileManagement, |
69 | TempFileWriter $tempFileWriter |
70 | ) { |
71 | $this->ontology = $ontology; |
72 | $this->sharedStimulusStoreService = $sharedStimulusStoreService; |
73 | $this->listStylesheetsService = $listStylesheetsService; |
74 | $this->stylesheetRepository = $stylesheetRepository; |
75 | $this->fileSourceUnserializer = $fileSourceUnserializer; |
76 | $this->fileManagement = $fileManagement; |
77 | $this->tempFileWriter = $tempFileWriter; |
78 | } |
79 | |
80 | public function copy(CopyCommand $command): SharedStimulus |
81 | { |
82 | $this->assertHasRequiredParameters($command); |
83 | |
84 | $source = SharedStimulusInstanceData::fromResource( |
85 | $this->ontology->getResource($command->getSourceUri()), |
86 | $command->getLanguage() |
87 | ); |
88 | |
89 | $srcXmlPath = $this->fileSourceUnserializer->unserialize($source->link); |
90 | $stimulusFilename = basename($source->link); |
91 | $dirname = $this->sharedStimulusStoreService->storeStream( |
92 | $this->fileManagement->getFileStream($srcXmlPath)->detach(), |
93 | $stimulusFilename, |
94 | $this->copyCSSFilesFrom($source) |
95 | ); |
96 | |
97 | $target = $this->ontology->getResource($command->getDestinationUri()); |
98 | |
99 | $target->setPropertyValue( |
100 | $target->getProperty(TaoMediaOntology::PROPERTY_LINK), |
101 | $dirname . DIRECTORY_SEPARATOR . $stimulusFilename |
102 | ); |
103 | |
104 | return new SharedStimulus( |
105 | $source->resourceUri, |
106 | $target->getUri(), |
107 | $command->getLanguage() |
108 | ); |
109 | } |
110 | |
111 | private function copyCSSFilesFrom(SharedStimulusInstanceData $source): array |
112 | { |
113 | $cssPath = $this->stylesheetRepository->getPath($source->resourceUri); |
114 | $cssFiles = $this->listStylesheetsService->getList( |
115 | new ListStylesheetsDTO($source->resourceUri) |
116 | ); |
117 | |
118 | $newCssFiles = []; |
119 | |
120 | foreach ($cssFiles['children'] as $child) { |
121 | $newCssFiles[] = $this->tempFileWriter->writeFile( |
122 | self::NAMESPACE_TEMP_FILES, |
123 | $child['name'], |
124 | $this->stylesheetRepository->read( |
125 | implode( |
126 | DIRECTORY_SEPARATOR, |
127 | [$cssPath , StoreService::CSS_DIR_NAME, $child['name']] |
128 | ) |
129 | ) |
130 | ); |
131 | } |
132 | |
133 | return $newCssFiles; |
134 | } |
135 | |
136 | private function assertHasRequiredParameters(CopyCommand $command): void |
137 | { |
138 | if ( |
139 | '' === trim($command->getSourceUri()) |
140 | || '' === trim($command->getDestinationUri()) |
141 | || '' === trim($command->getLanguage()) |
142 | ) { |
143 | throw new InvalidArgumentException( |
144 | sprintf( |
145 | 'Argument of type %s is missing a required parameter', |
146 | CopyCommand::class |
147 | ) |
148 | ); |
149 | } |
150 | } |
151 | } |