Passed
Push — master ( b35614...49fb5f )
by Marcin
07:22
created

Helper::getKeyValues()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 11
nc 7
nop 3
dl 0
loc 18
ccs 0
cts 11
cp 0
crap 90
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * TERYT-API
4
 *
5
 * Copyright (c) 2017 pudelek.org.pl
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For the full copyright and license information, please view source file
10
 * that is bundled with this package in the file LICENSE
11
 *
12
 * @author  Marcin Pudełek <[email protected]>
13
 *
14
 */
15
16
namespace mrcnpdlk\Teryt;
17
18
19
use mrcnpdlk\Teryt\Exception\Connection;
20
21
/**
22
 * Class Helper
23
 *
24
 * @package mrcnpdlk\Teryt
25
 */
26
class Helper
27
{
28
    /**
29
     * Converting value to boolean
30
     *
31
     * @param $exclude
32
     *
33
     * @return boolean
34
     */
35 1
    public static function convertToBoolean($exclude): bool
36
    {
37 1
        if (is_bool($exclude)) {
38 1
            return $exclude;
39
        }
40
41 1
        if (is_numeric($exclude)) {
42 1
            return $exclude === 1;
43
        }
44
45 1
        if (is_string($exclude)) {
46 1
            return strtolower(trim($exclude)) === 'true' || trim($exclude) === '1';
47
        }
48
49
        return false;
50
    }
51
52
53
    /**
54
     * Catching and managment of exceptions
55
     *
56
     * @param \Exception $e
57
     *
58
     * @return \Exception|Exception|Connection
59
     */
60 1
    public static function handleException(\Exception $e)
61
    {
62 1
        if ($e instanceof \SoapFault) {
63 1
            switch ($e->faultcode ?? null) {
64
                case 'a:InvalidSecurityToken':
65 1
                    return new Connection(sprintf('Invalid Security Token'), 1, $e);
66
                case 'WSDL':
67
                    return new Connection(sprintf('%s', $e->faultstring ?? 'Unknown'), 2, $e);
68
                case 'Client':
69
                    return new Connection(sprintf('%s', $e->faultstring ?? 'Unknown'), 3, $e);
70
                default:
71
                    return new Connection(sprintf('%s', 'Unknown'), 99, $e);
72
            }
73
        } else {
74
            if ($e instanceof Exception) {
75
                return $e;
76
            }
77
78
            return new Exception('Unknown Exception', 1, $e);
79
        }
80
    }
81
82
    /**
83
     * Return unique property/key values from array of object/array
84
     *
85
     * @param array  $tItems
86
     * @param string $sKey
87
     * @param bool   $asUnique
88
     *
89
     * @return array
90
     * @throws Exception
91
     */
92
    public static function getKeyValues(array $tItems, string $sKey, bool $asUnique = true): array
93
    {
94
        $answer = [];
95
        foreach ($tItems as $item) {
96
            if (!is_object($item) && !is_array($item)) {
97
                throw new Exception(sprintf('%s() elem of tInput is not an array|object, is %s', __METHOD__, gettype($item)));
98
            }
99
            $item = (array)$item;
100
            if (
101
                array_key_exists($sKey, $item)
102
                && (is_string($item[$sKey]) || is_numeric($item[$sKey]))
103
                && !in_array($item[$sKey], $answer, true)
104
            ) {
105
                $answer[] = $item[$sKey];
106
            }
107
        }
108
109
        return $asUnique ? array_unique($answer) : $answer;
110
    }
111
112
    /**
113
     * Fixing Teryt API bug
114
     *
115
     * If only one item exists in response, returned property is not a array but object type
116
     *
117
     * @param \stdClass $oObject
118
     * @param string    $sPropertyName
119
     *
120
     * @return array
121
     * @throws Exception
122
     */
123 7
    public static function getPropertyAsArray(\stdClass $oObject, string $sPropertyName): array
124
    {
125 7
        if (!property_exists($oObject, $sPropertyName)) {
126 1
            throw new Exception\NotFound(sprintf('%s() Property [%s] not exist in object', __METHOD__, $sPropertyName));
127
        }
128 6
        if (!is_array($oObject->{$sPropertyName})) {
129 2
            return [$oObject->{$sPropertyName}];
130
        }
131
132 5
        return $oObject->{$sPropertyName};
133
    }
134
135
    /**
136
     * Fixing Teryt API bug
137
     *
138
     * If only one item exists in response, returned property is not a array but object type
139
     *
140
     * @param \stdClass $oObject
141
     * @param string    $sPropertyName
142
     *
143
     * @return mixed
144
     * @throws Exception
145
     * @throws Exception\NotFound
146
     */
147 1
    public static function getPropertyAsObject(\stdClass $oObject, string $sPropertyName)
148
    {
149 1
        if (!property_exists($oObject, $sPropertyName)) {
150
            throw new Exception\NotFound(sprintf('%s() Property [%s] not exist in object', __METHOD__, $sPropertyName));
151
        }
152 1
        if (!is_object($oObject->{$sPropertyName})) {
153
            throw new Exception(sprintf('%s() Property [%s] is not an object type [is:%s]', __METHOD__, $sPropertyName,
154
                gettype($oObject->{$sPropertyName})));
155
        }
156
157 1
        return $oObject->{$sPropertyName};
158
    }
159
160
    /**
161
     * Save file on disk
162
     *
163
     * @param string $sPath   Destination path
164
     * @param string $content File content
165
     *
166
     * @return \SplFileObject
167
     * @throws \RuntimeException
168
     * @throws \LogicException
169
     */
170 2
    public static function saveFile(string $sPath, string $content): \SplFileObject
171
    {
172 2
        if (!file_exists($sPath) || (md5_file($sPath) !== md5($content))) {
173 2
            $oFile = new \SplFileObject($sPath, 'w+');
174 2
            $oFile->fwrite($content);
175
        }
176
177 2
        return new \SplFileObject($sPath);
178
    }
179
}
180