Test Failed
Push — master ( fe57db...42cd89 )
by Jonathan
12:45
created

MetaTrait::getBuildTime()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.4326

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 0
loc 23
ccs 7
cts 11
cp 0.6364
crap 3.4326
rs 9.8333
c 0
b 0
f 0
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\Exception\InvalidArgumentException;
18
19
/**
20
 * Trait MetaTrait
21
 *
22
 * @package Tisd\Sdk
23
 */
24
trait MetaTrait
25
{
26
    /**
27
     * Get the array of consolidated data
28
     *
29
     * @return array
30
     */
31
    abstract protected function getConsolidated(): array;
32
33
    /**
34
     * Get the array of meta-data
35
     *
36
     * @return array
37
     */
38
    public function getMeta(): array
39
    {
40
        $ret = [];
41
42
        $consolidated = $this->getConsolidated();
43
44
        if (isset($consolidated['meta'])) {
45
            $ret = $consolidated['meta'];
46
        }
47
48
        return $ret;
49
    }
50
51
    /**
52
     * Get the category count
53
     *
54
     * @return int
55
     */
56
    public function getCategoryCount(): int
57
    {
58
        $ret = 0;
59
60
        $meta = $this->getMeta();
61
        if (isset($meta['category']['count'])) {
62
            $ret = (int) $meta['category']['count'];
63
        }
64
65
        return $ret;
66
    }
67
68
    /**
69
     * Get the section count
70
     *
71
     * @return int
72
     */
73
    public function getSectionCount(): int
74
    {
75
        $ret = 0;
76
77
        $meta = $this->getMeta();
78
        if (isset($meta['section']['count'])) {
79
            $ret = $meta['section']['count'];
80
        }
81
82
        return $ret;
83
    }
84
85
    /**
86
     * Get the package count
87
     *
88
     * @return int
89
     */
90
    public function getPackageCount(): int
91
    {
92
        $ret = 0;
93
94
        $meta = $this->getMeta();
95
        if (isset($meta['package']['count'])) {
96
            $ret = (int) $meta['package']['count'];
97
        }
98
99
        return $ret;
100
    }
101
102
    /**
103
     * Get the build time
104
     *
105
     * @param string $type
106
     *
107
     * @return mixed
108
     */
109 1
    public function getBuildTime(string $type = 'timestamp')
110
    {
111 1
        $ret = null;
112
113 1
        $types = [
114
            'timestamp',
115
            'rtf_2822',
116
            'iso_8601',
117
        ];
118
119 1
        if (!in_array($type, $types, true)) {
120 1
            $format  = '"type" must be one of %s';
121 1
            $message = sprintf($format, implode(', ', $types));
122 1
            throw new InvalidArgumentException($message);
123
        }
124
125
        $meta = $this->getMeta();
126
127
        if (isset($meta['build']['time'][$type])) {
128
            $ret = $meta['build']['time'][$type];
129
        }
130
131
        return $ret;
132
    }
133
}
134