Passed
Push — master ( 42cd89...819998 )
by Jonathan
06:40 queued 04:56
created

ConsolidatedTrait::downloadConsolidated()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 47
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5.0026

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 24
c 2
b 0
f 0
nc 8
nop 0
dl 0
loc 47
ccs 20
cts 21
cp 0.9524
crap 5.0026
rs 9.2248
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;
16
17
use Tisd\Sdk\Cache\Cache;
18
use Tisd\Sdk\Defaults\Defaults;
19
20
/**
21
 * Trait ConsolidatedTrait
22
 *
23
 * @package Tisd\Sdk
24
 */
25
trait ConsolidatedTrait
26
{
27
    /**
28
     * Array of consolidated data
29
     *
30
     * @var array
31
     */
32
    private array $consolidated;
33
34
    /**
35
     * Get the Cache instance
36
     *
37
     * @return Cache
38
     */
39
    abstract public function getCache(): Cache;
40
41
    /**
42
     * Get the context
43
     *
44
     * @return string
45
     */
46
    abstract public function getContext(): string;
47
48
    /**
49
     * Get the hostname
50
     *
51
     * @return string
52
     */
53
    abstract public function getHostname(): string;
54
55
    /**
56
     * Get the locale
57
     *
58
     * @return string
59
     */
60
    abstract public function getLocale(): string;
61
62
    /**
63
     * Get the timeout
64
     *
65
     * @return int
66
     */
67
    abstract public function getTimeout(): int;
68
69
    /**
70
     * Get the version
71
     *
72
     * @return string
73
     */
74
    abstract public function getVersion(): string;
75
76
    /**
77
     * Filter the packages by key
78
     *
79
     * @param array        $packages
80
     * @param string       $key
81
     * @param array|string $value
82
     * @param bool         $fuzzy
83
     *
84
     * @return array
85
     */
86
    abstract protected function filter(array $packages, string $key, array|string $value, bool $fuzzy = false): array;
87
88
    /**
89
     * Get the array of consolidated data
90
     *
91
     * @return array
92
     */
93 38
    protected function getConsolidated(): array
94
    {
95 38
        if (0 === count($this->consolidated)) {
96
97 38
            $cache   = $this->getCache();
98 38
            $cacheId = $this->getLocale() . __METHOD__;
99
100 38
            if ($cache->getTtl() > 0) {
101 37
                $cacheId      = $cache->getId($cacheId);
102 37
                $consolidated = $cache->read($cacheId);
103 37
                if (0 === count($consolidated)) {
104 4
                    $consolidated = $this->downloadConsolidated();
105 37
                    $cache->write($cacheId, $consolidated);
106
                }
107
            } else {
108 1
                $consolidated = $this->downloadConsolidated();
109
            }
110
111 38
            if ('' !== $this->getContext()) {
112 1
                assert(is_array($consolidated['packages']));
113 1
                $packages                 = $this->filter($consolidated['packages'], 'contexts', $this->getContext());
114 1
                $consolidated['packages'] = $packages;
115
            }
116
117 38
            $this->setConsolidated($consolidated);
118
        }
119
120 38
        return $this->consolidated;
121
    }
122
123
    /**
124
     * Set the consolidated data array
125
     *
126
     * @param array $consolidated
127
     *
128
     * @return Sdk
129
     */
130 46
    protected function setConsolidated(array $consolidated): self
131
    {
132 46
        $this->consolidated = $consolidated;
133
134 46
        return $this;
135
    }
136
137
    /**
138
     * Download the consolidated data array
139
     *
140
     * @return array
141
     */
142 5
    private function downloadConsolidated(): array
143
    {
144 5
        $format = 'https://%s/api/%s/consolidated/%s.json';
145 5
        $uri    = sprintf($format, $this->getHostname(), $this->getVersion(), $this->getLocale());
146
147 5
        $options = [
148
            'http' => [
149 5
                'timeout' => $this->getTimeout(),
150
                'method'  => 'GET',
151 5
                'header'  => sprintf('User-Agent: TIS Download System SDK (PHP %s)', phpversion()),
152
            ],
153
        ];
154
155 5
        if ($this->getHostname() === Defaults::HOSTNAME_DEVELOPMENT) {
156
            $options['ssl'] = [
157
                'verify_peer'      => false,
158
                'verify_peer_name' => false,
159
            ];
160
        }
161
162 5
        $context = stream_context_create($options);
163
164 5
        $json = file_get_contents($uri, false, $context);
165 5
        assert(is_string($json));
166
167 5
        $consolidated = json_decode($json, true);
168 5
        assert(is_array($consolidated));
169
170
        /**
171
         * This is a temporary measure, until the package.xml files have been updated with the
172
         * "platform" => "windows|linux" key.
173
         * In the case of a download file, added either "windows" or "linux" to the "platform" key.
174
         * Otherwise, an empty string.
175
         */
176 5
        foreach ($consolidated['packages']['children'] as &$categories) {
177 5
            foreach ($categories['children'] as &$sections) {
178 5
                foreach ($sections['children'] as &$package) {
179 5
                    $package['platform'] = match ($package['category_id']) {
180 5
                        'downloads'       => 'windows',
181 5
                        'downloads-linux' => 'linux',
182 5
                        default           => '',
183
                    };
184
                }
185
            }
186
        }
187
188 5
        return $consolidated;
189
    }
190
}
191