Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
27.59% covered (danger)
27.59%
16 / 58
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
OntologyService
27.59% covered (danger)
27.59%
16 / 58
14.29% covered (danger)
14.29%
1 / 7
56.95
0.00% covered (danger)
0.00%
0 / 1
 getExecutionsByDelivery
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 getDeliveryExecutionsByStatus
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 getUserExecutions
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 initDeliveryExecution
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 spawnDeliveryExecution
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
2.00
 getDeliveryExecution
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 deleteDeliveryExecutionData
0.00% covered (danger)
0.00%
0 / 2
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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\taoDelivery\model\execution;
23
24use common_Logger;
25use core_kernel_classes_Class;
26use core_kernel_classes_Resource;
27use oat\generis\model\data\event\ResourceCreated;
28use oat\generis\model\OntologyAwareTrait;
29use oat\generis\model\OntologyRdfs;
30use oat\oatbox\event\EventManagerAwareTrait;
31use oat\oatbox\service\ConfigurableService;
32use oat\taoDelivery\model\execution\Delete\DeliveryExecutionDeleteRequest;
33
34/**
35 * Service to manage the execution of deliveries
36 *
37 * @access public
38 * @author Joel Bout, <joel@taotesting.com>
39 * @package taoDelivery
40 */
41class OntologyService extends ConfigurableService implements
42    DeliveryExecutionService,
43    Monitoring,
44    DeliveryExecutionServiceInterface
45{
46    use EventManagerAwareTrait;
47    use OntologyAwareTrait;
48
49    /**
50     * (non-PHPdoc)
51     * @see Monitoring::getExecutionsByDelivery()
52     * @param core_kernel_classes_Resource $compiled
53     * @return DeliveryExecution[]
54     */
55    public function getExecutionsByDelivery(core_kernel_classes_Resource $compiled)
56    {
57        $executionClass = new core_kernel_classes_Class(OntologyDeliveryExecution::CLASS_URI);
58        $resources = $executionClass->searchInstances([
59            OntologyDeliveryExecution::PROPERTY_DELIVERY => $compiled->getUri()
60        ], [
61            'like' => false
62        ]);
63        $returnValue = [];
64        foreach ($resources as $resource) {
65            $returnValue[] = $this->getDeliveryExecution($resource);
66        }
67        return $returnValue;
68    }
69
70    public function getDeliveryExecutionsByStatus($userUri, $status)
71    {
72        $executionClass = new core_kernel_classes_Class(OntologyDeliveryExecution::CLASS_URI);
73        $started = $executionClass->searchInstances([
74            OntologyDeliveryExecution::PROPERTY_SUBJECT => $userUri,
75            OntologyDeliveryExecution::PROPERTY_STATUS => $status
76        ], [
77            'like' => false
78        ]);
79        $returnValue = [];
80        foreach ($started as $resource) {
81            $returnValue[] = $this->getDeliveryExecution($resource);
82        }
83        return $returnValue;
84    }
85
86    /**
87     * (non-PHPdoc)
88     * @see DeliveryExecutionService::getUserExecutions()
89     */
90    public function getUserExecutions(core_kernel_classes_Resource $compiled, $userUri)
91    {
92        $executionClass = new core_kernel_classes_Class(OntologyDeliveryExecution::CLASS_URI);
93        $instances = $executionClass->searchInstances([
94            OntologyDeliveryExecution::PROPERTY_SUBJECT  => $userUri,
95            OntologyDeliveryExecution::PROPERTY_DELIVERY => $compiled->getUri()
96        ], [
97            'like' => false
98        ]);
99        $deliveryExecutions = [];
100        foreach ($instances as $resource) {
101            $deliveryExecutions[] = $this->getDeliveryExecution($resource->getUri());
102        }
103        return $deliveryExecutions;
104    }
105
106    /**
107     * @deprecated
108     * (non-PHPdoc)
109     * @see DeliveryExecutionService::initDeliveryExecution()
110     */
111    public function initDeliveryExecution(core_kernel_classes_Resource $assembly, $userUri)
112    {
113        common_Logger::w('Call to deprecated function ' . __FUNCTION__);
114        return $this->spawnDeliveryExecution(
115            $assembly->getLabel(),
116            $assembly->getUri(),
117            $userUri,
118            DeliveryExecution::STATE_ACTIVE
119        );
120    }
121
122    /**
123     * @param $label
124     * @param $deliveryId
125     * @param $userId
126     * @param $status
127     * @param string|null $deliveryExecutionId
128     * @return DeliveryExecution
129     * @throws \common_exception_Error
130     */
131    public function spawnDeliveryExecution($label, $deliveryId, $userId, $status, $deliveryExecutionId = null)
132    {
133        $propertyList = [
134            OntologyRdfs::RDFS_LABEL => $label,
135            DeliveryExecutionInterface::PROPERTY_DELIVERY => $deliveryId,
136            DeliveryExecutionInterface::PROPERTY_SUBJECT => $userId,
137            DeliveryExecutionInterface::PROPERTY_TIME_START => microtime(),
138            DeliveryExecutionInterface::PROPERTY_STATUS => $status,
139        ];
140
141        $executionClass = $this->getClass(DeliveryExecutionInterface::CLASS_URI);
142        if (empty($deliveryExecutionId)) {
143            $execution = $executionClass->createInstanceWithProperties($propertyList);
144        } else {
145            $execution = $executionClass->getResource($deliveryExecutionId);
146            $execution->setPropertiesValues($propertyList);
147            $this->getEventManager()->trigger(new ResourceCreated($execution));
148        }
149
150        return $this->getDeliveryExecution($execution);
151    }
152
153    /**
154     * (non-PHPdoc)
155     * @see DeliveryExecutionService::getDeliveryExecution()
156     */
157    public function getDeliveryExecution($identifier)
158    {
159        return $this->propagate(new DeliveryExecution(
160            new OntologyDeliveryExecution($identifier)
161        ));
162    }
163
164    /**
165     * @inheritdoc
166     */
167    public function deleteDeliveryExecutionData(DeliveryExecutionDeleteRequest $request)
168    {
169        $resource = new core_kernel_classes_Resource($request->getDeliveryExecution()->getIdentifier());
170
171        return $resource->delete();
172    }
173}