Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ActionResponse
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 empty
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 success
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 withAttribute
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
14 / 14
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) 2021 (original work) Open Assessment Technologies SA;
19 *
20 * @author Ricardo Quintanilha <ricardo.quintanilha@taotesting.com>
21 */
22
23declare(strict_types=1);
24
25namespace oat\taoQtiTest\model\Service;
26
27final class ActionResponse
28{
29    /** @var bool */
30    private $isSuccess = false;
31
32    /** @var array|null */
33    private $testContext;
34
35    /** @var array|null */
36    private $testMap;
37
38    /** @var array */
39    private $extraAttributes = [];
40
41    private function __construct()
42    {
43    }
44
45    public static function empty(): self
46    {
47        return new self();
48    }
49
50    public static function success(?array $testContext = null, ?array $testMap = null): self
51    {
52        $response = new self();
53
54        $response->isSuccess = true;
55        $response->testContext = $testContext;
56        $response->testMap = $testMap;
57
58        return $response;
59    }
60
61    public function withAttribute(string $name, $value): self
62    {
63        $response = clone $this;
64
65        $response->extraAttributes[$name] = $value;
66
67        return $response;
68    }
69
70    public function toArray(): array
71    {
72        $payload = array_merge(
73            [
74                'success' => $this->isSuccess,
75                'testContext' => $this->testContext,
76                'testMap' => $this->testMap,
77            ],
78            $this->extraAttributes
79        );
80
81        return array_filter(
82            $payload,
83            function ($value): bool {
84                return $value !== null;
85            }
86        );
87    }
88}