Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
AdjustmentMap
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
11 / 11
19
100.00% covered (success)
100.00%
1 / 1
 increase
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 decrease
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getByType
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromArray
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 put
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 isValidAction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 ensureEntryInitialized
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 ensureEntryActionInitialized
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 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) 2020 (original work) Open Assessment Technologies SA ;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoQtiTest\models\runner\time;
24
25use JsonSerializable;
26use oat\taoTests\models\runner\time\TimerAdjustmentMapInterface;
27use oat\taoTests\models\runner\time\ArraySerializable;
28
29class AdjustmentMap implements TimerAdjustmentMapInterface, JsonSerializable, ArraySerializable
30{
31    public const ACTION_DECREASE = 'decrease';
32    public const ACTION_INCREASE = 'increase';
33
34    private $map = [];
35
36    /**
37     * @inheritDoc
38     */
39    public function increase(string $sourceId, string $type, int $seconds): TimerAdjustmentMapInterface
40    {
41        return $this->put($sourceId, $type, self::ACTION_INCREASE, $seconds);
42    }
43
44    /**
45     * @inheritDoc
46     */
47    public function decrease(string $sourceId, string $type, int $seconds): TimerAdjustmentMapInterface
48    {
49        return $this->put($sourceId, $type, self::ACTION_DECREASE, $seconds);
50    }
51
52    /**
53     * @inheritDoc
54     */
55    public function get(string $sourceId): int
56    {
57        $adjustmentTime = 0;
58        if (!isset($this->map[$sourceId])) {
59            return $adjustmentTime;
60        }
61
62        foreach ($this->map[$sourceId] as $type => $adjustments) {
63            $adjustmentTime += $this->getByType($sourceId, $type);
64        }
65
66        return $adjustmentTime;
67    }
68
69    public function getByType(string $sourceId, string $type): int
70    {
71        if (!isset($this->map[$sourceId][$type])) {
72            return 0;
73        }
74
75        // phpcs:disable Generic.Files.LineLength
76        return $this->map[$sourceId][$type][self::ACTION_INCREASE] - $this->map[$sourceId][$type][self::ACTION_DECREASE];
77        // phpcs:enable Generic.Files.LineLength
78    }
79
80    public function jsonSerialize()
81    {
82        return $this->map;
83    }
84
85    public function toArray()
86    {
87        return $this->map;
88    }
89
90    public function fromArray($map)
91    {
92        $this->map = [];
93        if (is_array($map)) {
94            $this->map = $map;
95        }
96    }
97
98    private function put(string $sourceId, string $type, string $action, int $seconds): TimerAdjustmentMapInterface
99    {
100        if (empty($sourceId) || !$this->isValidAction($action) || !$seconds) {
101            throw new \InvalidArgumentException('Provided arguments should not be empty.');
102        }
103        $this->ensureEntryInitialized($sourceId, $type);
104        $this->map[$sourceId][$type][$action] += $seconds;
105
106        return $this;
107    }
108
109    private function isValidAction(string $action): bool
110    {
111        return in_array($action, [self::ACTION_INCREASE, self::ACTION_DECREASE], true);
112    }
113
114    private function ensureEntryInitialized(string $sourceId, string $type): void
115    {
116        $this->ensureEntryActionInitialized($sourceId, $type, self::ACTION_INCREASE);
117        $this->ensureEntryActionInitialized($sourceId, $type, self::ACTION_DECREASE);
118    }
119
120    private function ensureEntryActionInitialized(string $sourceId, string $type, string $action): void
121    {
122        if (!isset($this->map[$sourceId][$type][$action])) {
123            $this->map[$sourceId][$type][$action] = 0;
124        }
125    }
126}