Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
CommandFactory
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 makeCreateCommandByRequest
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 makePatchCommand
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 makeCopyCommand
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDirectory
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getSerializer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoMediaManager\model\sharedStimulus\factory;
24
25use oat\generis\model\fileReference\FileReferenceSerializer;
26use oat\oatbox\filesystem\Directory;
27use oat\oatbox\filesystem\FileSystemService;
28use oat\oatbox\service\ConfigurableService;
29use oat\oatbox\user\User;
30use oat\taoMediaManager\model\sharedStimulus\CopyCommand;
31use oat\taoMediaManager\model\sharedStimulus\CreateCommand;
32use oat\taoMediaManager\model\sharedStimulus\PatchCommand;
33use oat\taoMediaManager\model\TaoMediaOntology;
34use Psr\Http\Message\ServerRequestInterface;
35
36class CommandFactory extends ConfigurableService
37{
38    public const DEFAULT_DIRECTORY = 'sharedStimulusUploads';
39
40    /** @var Directory */
41    private $directory;
42
43    public function makeCreateCommandByRequest(ServerRequestInterface $request): CreateCommand
44    {
45        $parsedBody = json_decode((string)$request->getBody(), true);
46
47        return new CreateCommand(
48            $parsedBody['classId'] ?? $parsedBody['classUri'] ?? TaoMediaOntology::CLASS_URI_MEDIA_ROOT,
49            $parsedBody['name'] ?? null,
50            $parsedBody['languageId'] ?? $parsedBody['languageUri'] ?? null
51        );
52    }
53
54    public function makePatchCommand(string $id, string $body, User $user): PatchCommand
55    {
56        $name = hash('md5', $id);
57        $file = $this->getDirectory()->getFile($name);
58        $file->put($body);
59
60        return new PatchCommand(
61            $id,
62            $this->getSerializer()->serialize($file),
63            $user->getIdentifier()
64        );
65    }
66
67    public function makeCopyCommand(
68        string $sourceUri,
69        string $destinationUri,
70        string $language
71    ): CopyCommand {
72        return new CopyCommand($sourceUri, $destinationUri, $language);
73    }
74
75    private function getDirectory(): Directory
76    {
77        if (is_null($this->directory)) {
78            /** @var FileSystemService $fileSystemService */
79            $fileSystemService = $this->getServiceLocator()->get(FileSystemService::SERVICE_ID);
80            $this->directory = $fileSystemService->getDirectory(self::DEFAULT_DIRECTORY);
81        }
82        return $this->directory;
83    }
84
85    private function getSerializer(): FileReferenceSerializer
86    {
87        return $this->getServiceLocator()->get(FileReferenceSerializer::SERVICE_ID);
88    }
89}