Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeliveryAssemblyService
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 11
342
0.00% covered (danger)
0.00%
0 / 1
 getRootClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFileStorage
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 createAssemblyFromServiceCall
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllAssemblies
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 deleteInstance
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 deleteResource
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 deleteDeliveryRuntime
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 deleteDeliveryDirectory
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 getRuntime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCompilationDate
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getOrigin
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 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) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\taoDeliveryRdf\model;
23
24use core_kernel_classes_Resource;
25use core_kernel_classes_Property;
26use oat\taoDeliveryRdf\model\event\DeliveryRemovedEvent;
27use tao_models_classes_service_ServiceCall;
28use oat\taoDelivery\model\RuntimeService;
29use oat\tao\model\OntologyClassService;
30
31/**
32 * Service to manage the authoring of deliveries
33 *
34 * @access public
35 * @author Joel Bout, <joel@taotesting.com>
36 * @package taoDelivery
37 */
38class DeliveryAssemblyService extends OntologyClassService
39{
40    public const PROPERTY_ORIGIN = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#AssembledDeliveryOrigin';
41
42    public const CLASS_URI = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#AssembledDelivery';
43
44    // phpcs:disable Generic.Files.LineLength
45    public const PROPERTY_DELIVERY_TIME = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#AssembledDeliveryCompilationTime';
46    // phpcs:enable Generic.Files.LineLength
47
48    public const PROPERTY_DELIVERY_RUNTIME = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#AssembledDeliveryRuntime';
49
50    // phpcs:disable Generic.Files.LineLength
51    public const PROPERTY_DELIVERY_DIRECTORY = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#AssembledDeliveryCompilationDirectory';
52    // phpcs:enable Generic.Files.LineLength
53
54    public const PROPERTY_DELIVERY_GUEST_ACCESS = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#GuestAccess';
55
56    public const PROPERTY_DELIVERY_DISPLAY_ORDER_PROP = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#DisplayOrder';
57
58    public const PROPERTY_START = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#PeriodStart';
59
60    public const PROPERTY_END = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#PeriodEnd';
61
62    public const PROPERTY_ASSESSMENT_PROJECT_ID = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#AssessmentProjectId';
63
64    /**
65     * @var \tao_models_classes_service_FileStorage
66     */
67    protected $storageService;
68
69    /**
70     * (non-PHPdoc)
71     *
72     * @see \tao_models_classes_ClassService::getRootClass()
73     */
74    public function getRootClass()
75    {
76        return $this->getClass(self::CLASS_URI);
77    }
78
79    /**
80     * Return the file storage
81     *
82     * @return \tao_models_classes_service_FileStorage
83     */
84    protected function getFileStorage()
85    {
86        if (!$this->storageService) {
87            $this->storageService = $this
88                ->getServiceManager()
89                ->get(\tao_models_classes_service_FileStorage::SERVICE_ID);
90        }
91        return $this->storageService;
92    }
93
94    /**
95     * @deprecated please use DeliveryFactory
96     *
97     * @param core_kernel_classes_Class $deliveryClass
98     * @param tao_models_classes_service_ServiceCall $serviceCall
99     * @param array $properties
100     * @return \core_kernel_classes_Resource
101     */
102    public function createAssemblyFromServiceCall(
103        core_kernel_classes_Class $deliveryClass,
104        tao_models_classes_service_ServiceCall $serviceCall,
105        $properties = []
106    ) {
107        throw new \common_exception_Error("Call to deprecated " . __FUNCTION__);
108    }
109
110    /**
111     * Returns all assemblies marked as active
112     *
113     * @return core_kernel_classes_Resource[]
114     */
115    public function getAllAssemblies()
116    {
117        return $this->getRootClass()->getInstances(true);
118    }
119
120    /**
121     * Delete delivery by deleting runtime, directories & ontology record
122     * @param core_kernel_classes_Resource $assembly
123     * @return bool
124     */
125    public function deleteInstance(core_kernel_classes_Resource $assembly)
126    {
127        if ($this->deleteDeliveryRuntime($assembly) === false) {
128            \common_Logger::i('Fail to delete runtimes assembly, process aborted');
129        }
130
131        if ($this->deleteDeliveryDirectory($assembly) === false) {
132            \common_Logger::i('Fail to delete directories assembly, process aborted');
133        }
134
135        return $assembly->delete();
136    }
137
138    public function deleteResource(core_kernel_classes_Resource $resource)
139    {
140        $result = parent::deleteResource($resource);
141
142        $this->getEventManager()->trigger(new DeliveryRemovedEvent($resource->getUri()));
143
144        return $result;
145    }
146
147
148    /**
149     * Delete a runtime of a delivery
150     *
151     * @param core_kernel_classes_Resource $assembly
152     * @return bool
153     * @throws \core_kernel_classes_EmptyProperty
154     * @throws \core_kernel_classes_MultiplePropertyValuesException
155     */
156    protected function deleteDeliveryRuntime(core_kernel_classes_Resource $assembly)
157    {
158        /** @var GroupAssignment $deliveryAssignement */
159        $deliveryAssignement = $this->getServiceManager()->get(GroupAssignment::SERVICE_ID);
160        $deliveryAssignement->onDelete($assembly);
161        /** @var core_kernel_classes_Resource $runtimeResource */
162        $runtimeResource = $assembly->getUniquePropertyValue(
163            new core_kernel_classes_Property(self::PROPERTY_DELIVERY_RUNTIME)
164        );
165        if ($runtimeResource instanceof core_kernel_classes_Resource) {
166            return $runtimeResource->delete();
167        }
168        return true;
169    }
170
171    /**
172     * Delete directories related to a delivery, don't remove if dir is used by another delivery
173     *
174     * @param core_kernel_classes_Resource $assembly
175     * @return bool
176     */
177    public function deleteDeliveryDirectory(core_kernel_classes_Resource $assembly)
178    {
179        $success = true;
180        $deleted = 0;
181        $directories = $assembly->getPropertyValues(
182            new core_kernel_classes_Property(self::PROPERTY_DELIVERY_DIRECTORY)
183        );
184
185        foreach ($directories as $directory) {
186            $instances = $this->getRootClass()->getInstances(true, [self::PROPERTY_DELIVERY_DIRECTORY => $directory]);
187            unset($instances[$assembly->getUri()]);
188            if (empty($instances)) {
189                $success = $this->getFileStorage()->deleteDirectoryById($directory) ? $success : false;
190                $deleted++;
191            }
192        }
193        \common_Logger::i('(' . (int) $deleted . ') deletions for delivery assembly: ' . $assembly->getUri());
194        return $success;
195    }
196
197    /**
198     * Gets the service call to run this assembly
199     *
200     * @param core_kernel_classes_Resource $assembly
201     * @return tao_models_classes_service_ServiceCall
202     */
203    public function getRuntime(core_kernel_classes_Resource $assembly)
204    {
205        return $this->getServiceLocator()->get(RuntimeService::SERVICE_ID)->getRuntime($assembly->getUri());
206    }
207
208    /**
209     * Returns the date of the compilation of an assembly as a timestamp
210     *
211     * @param core_kernel_classes_Resource $assembly
212     * @return string
213     */
214    public function getCompilationDate(core_kernel_classes_Resource $assembly)
215    {
216        return (string)$assembly->getUniquePropertyValue(
217            new core_kernel_classes_Property(self::PROPERTY_DELIVERY_TIME)
218        );
219    }
220
221    /**
222     * @param core_kernel_classes_Resource $assembly
223     * @return core_kernel_classes_Resource
224     * @throws \common_Exception
225     * @throws \core_kernel_classes_EmptyProperty
226     */
227    public function getOrigin(core_kernel_classes_Resource $assembly)
228    {
229        return $assembly->getUniquePropertyValue(new core_kernel_classes_Property(self::PROPERTY_ORIGIN));
230    }
231}