PackagesTrait::getPackages()   B
last analyzed

Complexity

Conditions 10
Paths 7

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10.2537

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
nc 7
nop 3
dl 0
loc 43
ccs 19
cts 22
cp 0.8636
crap 10.2537
rs 7.6666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
/**
18
 * Trait PackagesTrait
19
 *
20
 * @package Tisd\Sdk
21
 */
22
trait PackagesTrait
23
{
24
    /**
25
     * Filter the packages by key
26
     *
27
     * @param array        $packages
28
     * @param string       $key
29
     * @param array|string $value
30
     * @param bool         $fuzzy
31
     *
32
     * @return array
33
     */
34
    abstract protected function filter(array $packages, string $key, array|string $value, bool $fuzzy = false): array;
35
36
    /**
37
     * Get the array of consolidated data
38
     *
39
     * @return array
40
     */
41
    abstract protected function getConsolidated(): array;
42
43
    /**
44
     * Get the array of packages data
45
     *
46
     * @param string $categoryId
47
     * @param string $sectionId
48
     * @param string $packageId
49
     *
50
     * @return array
51
     */
52 24
    public function getPackages(string $categoryId = '', string $sectionId = '', string $packageId = ''): array
53
    {
54 24
        $consolidated = $this->getConsolidated();
55
56 24
        $packages = $consolidated['packages'];
57
58 24
        if ('' !== $categoryId && '' !== $sectionId && '' !== $packageId) {
59
60 1
            $packages = $this->filter($packages, 'category_id', $categoryId);
61 1
            $packages = $this->filter($packages, 'section_id', $sectionId);
62 1
            $packages = $this->filter($packages, 'package_id', $packageId);
63
64 1
            if (isset($packages['children'][$categoryId]['children'][$sectionId]['children'][$packageId])) {
65 1
                return $packages['children'][$categoryId]['children'][$sectionId]['children'][$packageId];
66
            }
67
68
            return [];
69
        }
70
71 23
        if ('' !== $categoryId && '' !== $sectionId) {
72
73 1
            $packages = $this->filter($packages, 'category_id', $categoryId);
74 1
            $packages = $this->filter($packages, 'section_id', $sectionId);
75
76 1
            if (isset($packages['children'][$categoryId]['children'][$sectionId])) {
77 1
                return $packages['children'][$categoryId]['children'][$sectionId];
78
            }
79
80
            return [];
81
        }
82
83 22
        if ('' !== $categoryId) {
84
85 1
            $packages = $this->filter($packages, 'category_id', $categoryId);
86
87 1
            if (isset($packages['children'][$categoryId])) {
88 1
                return $packages['children'][$categoryId];
89
            }
90
91
            return [];
92
        }
93
94 21
        return $packages;
95
    }
96
97
    /**
98
     * Get array of package data for the specified product codes
99
     *
100
     * @param array $productCodes
101
     *
102
     * @return array
103
     */
104 1
    public function getPackagesByProductCodes(array $productCodes): array
105
    {
106 1
        $consolidated = $this->getConsolidated();
107
108 1
        return $this->filter($consolidated['packages'], 'product_code', $productCodes);
109
    }
110
111
    /**
112
     * Get array of package data for the specified search term (starting with)
113
     *
114
     * @param string $q
115
     *
116
     * @return array
117
     */
118 1
    public function getPackagesByProductCodeSearch(string $q): array
119
    {
120 1
        $consolidated = $this->getConsolidated();
121
122 1
        return $this->filter($consolidated['packages'], 'product_code', $q, true);
123
    }
124
125
    /**
126
     * Get array of package data for the specified platform ID
127
     *
128
     * @param string $platformId
129
     * @return array
130
     */
131 1
    public function getPackagesByPlatformId(string $platformId): array
132
    {
133 1
        $consolidated = $this->getConsolidated();
134
135 1
        return $this->filter($consolidated['packages'], 'platform_id', $platformId);
136
    }
137
}
138