Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
20 / 60
50.00% covered (danger)
50.00%
6 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_models_classes_service_ServiceCall
33.33% covered (danger)
33.33%
20 / 60
50.00% covered (danger)
50.00%
6 / 12
165.41
0.00% covered (danger)
0.00%
0 / 1
 __construct
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 addInParameter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOutParameter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getServiceDefinitionId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getInParameters
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRequiredVariables
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 toOntology
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 fromResource
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 serializeToString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromString
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 jsonSerialize
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 fromJson
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
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
22use oat\generis\model\OntologyAwareTrait;
23use oat\generis\model\OntologyRdfs;
24use oat\tao\model\WfEngineOntology;
25
26/**
27 * Represents a call of an interactive tao service
28 *
29 * @access public
30 * @author Joel Bout, <joel@taotesting.com>
31 * @package tao
32 * phpcs:disable Squiz.Classes.ValidClassName
33 */
34class tao_models_classes_service_ServiceCall implements JsonSerializable
35{
36    use OntologyAwareTrait;
37
38    /**
39     * @var core_kernel_classes_Resource
40     */
41
42    private $serviceDefinitionId = null;
43
44    /**
45     * Input Parameters used to call this service
46     *
47     * @var array
48     */
49    private $inParameters = [];
50
51    /**
52     * Variable parameter to which the outcome of the service is send
53     *
54     * @var tao_models_classes_service_VariableParameter
55     */
56    private $outParameter = null;
57
58    /**
59     * Instantiates a new service call
60     *
61     * @param core_kernel_classes_Resource $serviceDefinition
62     */
63    public function __construct($serviceDefinition)
64    {
65        $this->serviceDefinitionId = is_object($serviceDefinition)
66           ? $serviceDefinition->getUri()
67           : $serviceDefinition;
68    }
69
70    /**
71     * Adds an input parameter
72     *
73     * @param tao_models_classes_service_Parameter $param
74     */
75    public function addInParameter(tao_models_classes_service_Parameter $param)
76    {
77        $this->inParameters[] = $param;
78    }
79
80    /**
81     * Sets the output parameter, does not except constants
82     *
83     * @param tao_models_classes_service_VariableParameter $param
84     */
85    public function setOutParameter(tao_models_classes_service_VariableParameter $param)
86    {
87        $this->outParameter = $param;
88    }
89
90    /**
91     * returns the definition of the called service
92     *
93     * @return core_kernel_classes_Resource
94     */
95    public function getServiceDefinitionId()
96    {
97        return $this->serviceDefinitionId;
98    }
99
100    /**
101     * returns the call parameters
102     *
103     * @return array:
104     */
105    public function getInParameters()
106    {
107        return $this->inParameters;
108    }
109
110    /**
111     * Gets the variables expected to be present to call this service
112     *
113     * @return array:
114     */
115    public function getRequiredVariables()
116    {
117        $variables = [];
118        foreach ($this->inParameters as $param) {
119            if ($param instanceof tao_models_classes_service_VariableParameter) {
120                $variables[] = $param->getVariable();
121            }
122        }
123        return $variables;
124    }
125
126    /**
127     * Stores a service call in the ontology
128     *
129     * @return core_kernel_classes_Resource
130     */
131    public function toOntology()
132    {
133        $inResources = [];
134        $outResources = is_null($this->outParameter)
135           ? []
136           : $this->outParameter->toOntology($this->getModel());
137        foreach ($this->inParameters as $param) {
138            $inResources[] = $param->toOntology($this->getModel());
139        }
140        $serviceCallClass = $this->getClass(WfEngineOntology::CLASS_URI_CALL_OF_SERVICES);
141        $resource = $serviceCallClass->createInstanceWithProperties([
142            OntologyRdfs::RDFS_LABEL => 'serviceCall',
143            WfEngineOntology::PROPERTY_CALL_OF_SERVICES_SERVICE_DEFINITION    => $this->serviceDefinitionId,
144            WfEngineOntology::PROPERTY_CALL_OF_SERVICES_ACTUAL_PARAMETER_IN    => $inResources,
145            WfEngineOntology::PROPERTY_CALL_OF_SERVICES_ACTUAL_PARAMETER_OUT   => $outResources,
146            WfEngineOntology::PROPERTY_CALL_OF_SERVICES_WIDTH                => '100',
147            WfEngineOntology::PROPERTY_CALL_OF_SERVICES_HEIGHT               => '100'
148        ]);
149
150        return $resource;
151    }
152
153    /**
154     * Builds a service call from it's serialized form
155     *
156     * @param core_kernel_classes_Resource $resource
157     * @return tao_models_classes_service_ServiceCall
158     */
159    public static function fromResource(core_kernel_classes_Resource $resource)
160    {
161        $values = $resource->getPropertiesValues([
162            WfEngineOntology::PROPERTY_CALL_OF_SERVICES_SERVICE_DEFINITION,
163            WfEngineOntology::PROPERTY_CALL_OF_SERVICES_ACTUAL_PARAMETER_IN,
164            WfEngineOntology::PROPERTY_CALL_OF_SERVICES_ACTUAL_PARAMETER_OUT
165        ]);
166        $serviceDefUri = current($values[WfEngineOntology::PROPERTY_CALL_OF_SERVICES_SERVICE_DEFINITION]);
167        $serviceCall = new self($serviceDefUri);
168        foreach ($values[WfEngineOntology::PROPERTY_CALL_OF_SERVICES_ACTUAL_PARAMETER_IN] as $inRes) {
169            $param = tao_models_classes_service_Parameter::fromResource($inRes);
170            $serviceCall->addInParameter($param);
171        }
172        if (!empty($values[WfEngineOntology::PROPERTY_CALL_OF_SERVICES_ACTUAL_PARAMETER_OUT])) {
173            $param = tao_models_classes_service_Parameter::fromResource(
174                current($values[WfEngineOntology::PROPERTY_CALL_OF_SERVICES_ACTUAL_PARAMETER_OUT])
175            );
176
177            $serviceCall->setOutParameter($param);
178        }
179        return $serviceCall;
180    }
181
182    /**
183     * Serialize the current serivceCall object to a string
184     *
185     * @return string
186     *
187     * @deprecated Use json_encode($serviceCall) instead
188     */
189    public function serializeToString()
190    {
191        return json_encode($this);
192    }
193
194    /**
195     * Unserialize the string to a serivceCall object
196     *
197     * @param string $string
198     * @return tao_models_classes_service_ServiceCall
199     * @throws InvalidArgumentException
200     */
201    public static function fromString($string)
202    {
203        $data = json_decode($string, true);
204        if (json_last_error() !== JSON_ERROR_NONE) {
205            throw new InvalidArgumentException("Provided string is not a valid JSON.");
206        }
207
208        return self::fromJson($data);
209    }
210
211    public function jsonSerialize(): array
212    {
213        return [
214            'service' => $this->serviceDefinitionId,
215            'in' => $this->inParameters,
216            'out' => $this->outParameter
217        ];
218    }
219
220    /**
221     * @param array $data
222     * @return tao_models_classes_service_ServiceCall
223     */
224    public static function fromJson(array $data)
225    {
226        $call = new self($data['service']);
227        if (!empty($data['out'])) {
228            $call->setOutParameter(tao_models_classes_service_Parameter::fromJson($data['out']));
229        }
230        foreach ($data['in'] as $in) {
231            $call->addInParameter(tao_models_classes_service_Parameter::fromJson($in));
232        }
233        return $call;
234    }
235}