Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
30 / 36
60.00% covered (warning)
60.00%
9 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeliveryExecutionContext
83.33% covered (warning)
83.33%
30 / 36
60.00% covered (warning)
60.00%
9 / 15
18.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 createFromArray
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 validateExecutionId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 validateExecutionContextId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getExecutionId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setExecutionId
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
1.12
 setExecutionContextId
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
1.12
 getExecutionContextId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLabel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExtraData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setExtraData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 jsonSerialize
100.00% covered (success)
100.00%
7 / 7
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) 2019 (original work) Open Assessment Technologies SA ;
19 */
20
21namespace oat\taoDelivery\model\execution;
22
23use InvalidArgumentException;
24
25class DeliveryExecutionContext implements DeliveryExecutionContextInterface
26{
27    public const EXECUTION_CONTEXT_TYPE = 'delivery';
28
29    /**
30     * @var string
31     */
32    private $executionId;
33
34    /**
35     * @var string
36     */
37    private $executionContextId;
38
39    /**
40     * @var string
41     */
42    private $type;
43
44    /**
45     * @var string
46     */
47    private $label;
48
49    /**
50     * @var array
51     */
52    private $extraData = [];
53
54    /**
55     * DeliveryExecutionContext constructor.
56     *
57     * @param string $executionId
58     * @param string $executionContextId
59     * @param string $type
60     * @param string $label
61     * @param array|null $extraData
62     */
63    public function __construct(
64        string $executionId,
65        string $executionContextId,
66        string $type,
67        string $label,
68        ?array $extraData = []
69    ) {
70        $this->validateExecutionId($executionId);
71        $this->validateExecutionContextId($executionContextId);
72
73        $this->executionId = $executionId;
74        $this->executionContextId = $executionContextId;
75        $this->type = $type;
76        $this->label = $label;
77        $this->extraData = $extraData;
78    }
79
80    /**
81     * @param array $contextData
82     * @return DeliveryExecutionContextInterface
83     */
84    public static function createFromArray(array $contextData): DeliveryExecutionContextInterface
85    {
86        $executionId = $contextData[self::PARAM_EXECUTION_ID] ?? '';
87        $executionContextId = $contextData[self::PARAM_CONTEXT_ID] ?? '';
88        $type = $contextData[self::PARAM_TYPE] ?? '';
89        $label = $contextData[self::PARAM_LABEL] ?? '';
90        $extraData = $contextData[self::PARAM_EXTRA_DATA] ?? [];
91
92        return new static($executionId, $executionContextId, $type, $label, $extraData);
93    }
94
95    /**
96     * @param $executionId
97     * @throws InvalidArgumentException
98     */
99    private function validateExecutionId($executionId)
100    {
101        if (empty($executionId)) {
102            throw new InvalidArgumentException('Execution ID value must be not empty string.');
103        }
104    }
105
106    /**
107     * @param $executionContextId
108     * @throws InvalidArgumentException
109     */
110    private function validateExecutionContextId($executionContextId)
111    {
112        if (empty($executionContextId)) {
113            throw new InvalidArgumentException('Execution context ID value must be not empty string.');
114        }
115    }
116
117    /**
118     * @return string
119     */
120    public function getExecutionId(): string
121    {
122        return $this->executionId;
123    }
124
125    /**
126     * @param string $executionId
127     * @throws InvalidArgumentException
128     */
129    public function setExecutionId(string $executionId): void
130    {
131        $this->validateExecutionId($executionId);
132
133        $this->executionId = $executionId;
134    }
135
136    /**
137     * @param string $contextId
138     * @throws InvalidArgumentException
139     */
140    public function setExecutionContextId(string $contextId): void
141    {
142        $this->validateExecutionContextId($contextId);
143
144        $this->executionContextId = $contextId;
145    }
146
147    /**
148     * @return string
149     */
150    public function getExecutionContextId(): string
151    {
152        return $this->executionContextId;
153    }
154
155
156    /**
157     * @return string
158     */
159    public function getType(): string
160    {
161        return $this->type;
162    }
163
164    /**
165     * @param string $type
166     */
167    public function setType(string $type): void
168    {
169        $this->type = $type;
170    }
171
172    /**
173     * @return string
174     */
175    public function getLabel(): string
176    {
177        return $this->label;
178    }
179
180    /**
181     * @param string $label
182     */
183    public function setLabel(string $label): void
184    {
185        $this->label = $label;
186    }
187
188    /**
189     * @return array
190     */
191    public function getExtraData(): array
192    {
193        return $this->extraData;
194    }
195
196    /**
197     * @param array $data
198     */
199    public function setExtraData(array $data): void
200    {
201        $this->extraData = $data;
202    }
203
204    /**
205     * {@inheritdoc}
206     */
207    public function jsonSerialize()
208    {
209        return [
210            self::PARAM_EXECUTION_ID => $this->getExecutionId(),
211            self::PARAM_CONTEXT_ID => $this->getExecutionContextId(),
212            self::PARAM_TYPE => $this->getType(),
213            self::PARAM_LABEL => $this->getLabel(),
214            self::PARAM_EXTRA_DATA => $this->extraData,
215        ];
216    }
217}