AbstractLut   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 92.11%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 135
ccs 35
cts 38
cp 0.9211
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeys() 0 3 1
A setSdk() 0 5 1
A getSdk() 0 3 1
A getValues() 0 3 1
B buildLut() 0 38 6
A __construct() 0 5 1
A getValue() 0 9 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * The Imaging Source Download System PHP Wrapper
6
 *
7
 * PHP wrapper for The Imaging Source Download System Web API. Authored and supported by The Imaging Source Europe GmbH.
8
 *
9
 * @link      http://dl-gui.theimagingsource.com to learn more about The Imaging Source Download System
10
 * @link      https://github.com/jonathanmaron/theimagingsource-tisd-sdk for the canonical source repository
11
 * @license   https://github.com/jonathanmaron/theimagingsource-tisd-sdk/blob/master/LICENSE.md
12
 * @copyright © 2022 The Imaging Source Europe GmbH
13
 */
14
15
namespace Tisd\Sdk\Lut;
16
17
use RecursiveArrayIterator;
18
use RecursiveIteratorIterator;
19
use Tisd\Sdk\Exception\RuntimeException;
20
use Tisd\Sdk\Sdk;
21
22
/**
23
 * Class AbstractLut
24
 *
25
 * @package Tisd\Sdk\Lut
26
 */
27
class AbstractLut
28
{
29
    /**
30
     * Instance of Sdk
31
     *
32
     * @var Sdk
33
     */
34
    protected Sdk $sdk;
35
36
    /**
37
     * Look-up-table
38
     *
39
     * @var array
40
     */
41
    protected array $lut;
42
43
    /**
44
     * AbstractLut constructor
45
     *
46
     * @param array $options
47
     */
48 17
    public function __construct(array $options = [])
49
    {
50 17
        $sdk = new Sdk($options);
51
52 17
        $this->setSdk($sdk);
53
    }
54
55
    /**
56
     * Get all keys in the LUT
57
     *
58
     * @return array
59
     */
60 8
    public function getKeys(): array
61
    {
62 8
        return array_keys($this->getValues());
63
    }
64
65
    /**
66
     * Get all data in the LUT
67
     *
68
     * @return array
69
     */
70 12
    public function getValues(): array
71
    {
72 12
        return $this->lut;
73
    }
74
75
    /**
76
     * Get the value for key in the LUT
77
     *
78
     * @param string $key
79
     *
80
     * @return array|string
81
     */
82 4
    public function getValue(string $key): array|string
83
    {
84 4
        $ret = '';
85
86 4
        if (isset($this->lut[$key])) {
87 4
            $ret = $this->lut[$key];
88
        }
89
90 4
        return $ret;
91
    }
92
93
    /**
94
     * Get an instance of the Sdk
95
     *
96
     * @return Sdk
97
     */
98 17
    public function getSdk(): Sdk
99
    {
100 17
        return $this->sdk;
101
    }
102
103
    /**
104
     * Set an instance of the Sdk
105
     *
106
     * @param Sdk $sdk
107
     *
108
     * @return $this
109
     */
110 17
    public function setSdk(Sdk $sdk): self
111
    {
112 17
        $this->sdk = $sdk;
113
114 17
        return $this;
115
    }
116
117
    /**
118
     * Build the LUT
119
     *
120
     * @param string $keyName
121
     *
122
     * @return array
123
     */
124 17
    protected function buildLut(string $keyName): array
125
    {
126 17
        $ret = [];
127
128 17
        $packages = $this->getSdk()->getPackages();
129
130 17
        $rai = new RecursiveArrayIterator($packages);
131 17
        $rii = new RecursiveIteratorIterator($rai, RecursiveIteratorIterator::SELF_FIRST);
132
133 17
        foreach ($rii as $package) {
134
135 17
            if (!is_array($package)) {
136 17
                continue;
137
            }
138
139 17
            if (!array_key_exists('package_id', $package)) {
140 17
                continue;
141
            }
142
143 17
            if (!array_key_exists($keyName, $package)) {
144 1
                $format  = "The '%s' does not exist in the package.";
145 1
                $message = sprintf($format, $keyName);
146 1
                throw new RuntimeException($message);
147
            }
148
149 16
            $key = $package[$keyName];
150
151 16
            if (array_key_exists($key, $ret)) {
152
                $format  = "The '%s' is not unique in the LUT. The offending key is '%s'.";
153
                $message = sprintf($format, $keyName, $key);
154
                //@todo: Fix this by updating repository.
155
                throw new RuntimeException($message);
156
            }
157
158 16
            $ret[$key] = $package;
159
        }
160
161 16
        return $ret;
162
    }
163
}
164