Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
OntologyDeliveryExecution
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 9
462
0.00% covered (danger)
0.00%
0 / 1
 getIdentifier
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOriginalIdentifier
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStartTime
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getFinishTime
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getState
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getDelivery
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getUserIdentifier
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 setState
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 getData
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
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-2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\taoDelivery\model\execution;
23
24use common_exception_NotFound;
25use common_exception_ResourceNotFound;
26use common_Logger;
27use core_kernel_classes_Resource;
28
29/**
30 * Service to manage the execution of deliveries
31 *
32 * @access public
33 * @author Joel Bout, <joel@taotesting.com>
34 * @package taoDelivery
35 */
36class OntologyDeliveryExecution extends core_kernel_classes_Resource implements
37    DeliveryExecutionInterface,
38    OriginalIdAwareDeliveryExecutionInterface
39{
40    private $startTime;
41    private $finishTime;
42    private $state;
43    private $delivery;
44    private $userIdentifier;
45
46    /**
47     * (non-PHPdoc)
48     * @see DeliveryExecution::getIdentifier()
49     */
50    public function getIdentifier()
51    {
52        return $this->getUri();
53    }
54
55    /**
56     * @inheritDoc
57     */
58    public function getOriginalIdentifier(): string
59    {
60        return $this->getUri();
61    }
62
63    /**
64     * (non-PHPdoc)
65     * @see DeliveryExecution::getStartTime()
66     * @throws \common_exception_NotFound
67     */
68    public function getStartTime()
69    {
70        if (!isset($this->startTime)) {
71            $this->startTime = (string)$this->getData(OntologyDeliveryExecution::PROPERTY_TIME_START);
72        }
73        return $this->startTime;
74    }
75
76
77    /**
78     * (non-PHPdoc)
79     * @see DeliveryExecution::getFinishTime()
80     */
81    public function getFinishTime()
82    {
83        if (!isset($this->finishTime)) {
84            try {
85                $this->finishTime = (string)$this->getData(OntologyDeliveryExecution::PROPERTY_TIME_END);
86            } catch (common_exception_NotFound $missingException) {
87                $this->finishTime = null;
88            }
89        }
90        return $this->finishTime;
91    }
92
93    /**
94     * (non-PHPdoc)
95     * @see DeliveryExecution::getState()
96     */
97    public function getState()
98    {
99        if (!isset($this->state)) {
100            $state = $this->getData(OntologyDeliveryExecution::PROPERTY_STATUS);
101            if (!$state instanceof core_kernel_classes_Resource) {
102                $state = $this->getResource((string)$state);
103            }
104            $this->state = $state;
105        }
106        return $this->state;
107    }
108
109    /**
110     * @throws common_exception_ResourceNotFound
111     * @see DeliveryExecution::getDelivery()
112     */
113    public function getDelivery()
114    {
115        if (!isset($this->delivery)) {
116            try {
117                $this->delivery = $this->getData(OntologyDeliveryExecution::PROPERTY_DELIVERY);
118            } catch (common_exception_NotFound $missingException) {
119                throw new common_exception_ResourceNotFound(
120                    __('Delivery Execution for %s not found', $this->getUri())
121                );
122            }
123        }
124        return $this->delivery;
125    }
126
127    /**
128     * (non-PHPdoc)
129     * @see DeliveryExecution::getUserIdentifier()
130     */
131    public function getUserIdentifier()
132    {
133        if (!isset($this->userIdentifier)) {
134            $user = $this->getData(OntologyDeliveryExecution::PROPERTY_SUBJECT);
135            $this->userIdentifier =  ($user instanceof core_kernel_classes_Resource) ? $user->getUri() : (string)$user;
136        }
137        return $this->userIdentifier;
138    }
139
140    /**
141     * (non-PHPdoc)
142     * @see DeliveryExecution::setState()
143     */
144    public function setState($state)
145    {
146        $statusProp = $this->getProperty(OntologyDeliveryExecution::PROPERTY_STATUS);
147        $state = $this->getResource($state);
148        $currentStatus = $this->getState();
149        if ($currentStatus->getUri() == $state->getUri()) {
150            common_Logger::w('Delivery execution ' . $this->getIdentifier() . ' already in state ' . $state->getUri());
151            return false;
152        }
153        $this->editPropertyValues($statusProp, $state);
154        if ($state->getUri() == DeliveryExecutionInterface::STATE_FINISHED) {
155            $this->setPropertyValue($this->getProperty(OntologyDeliveryExecution::PROPERTY_TIME_END), microtime());
156        }
157        $this->state = $state;
158        return true;
159    }
160
161    /**
162     * @param $propertyName
163     * @return \core_kernel_classes_Container
164     * @throws common_exception_NotFound
165     */
166    private function getData($propertyName)
167    {
168        $property = $this->getProperty($propertyName);
169        $propertyValue = $this->getOnePropertyValue($property);
170        if (is_null($propertyValue)) {
171            throw new common_exception_NotFound(
172                'Property ' . $propertyName . ' not found for resource ' . $this->getUri()
173            );
174        }
175
176        return $propertyValue;
177    }
178}