Completed
Push — develop ( 2d4f17...d5975b )
by Nate
05:25
created

InterpretResponseErrors::normalizeErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/hubspot/blob/master/LICENSE.md
6
 * @link       https://github.com/flipbox/hubspot
7
 */
8
9
namespace flipbox\craft\hubspot\transformers;
10
11
use craft\helpers\ArrayHelper;
12
13
/**
14
 * @author Flipbox Factory <[email protected]>
15
 * @since 1.0.0
16
 */
17
class InterpretResponseErrors
18
{
19
    /**
20
     * @inheritdoc
21
     */
22
    public function __invoke(array $data): array
23
    {
24
        if (empty($data)) {
25
            return [
26
                'error' => 'An unknown error occurred.'
27
            ];
28
        }
29
30
        return $this->normalizeErrors($data);
31
    }
32
33
    /**
34
     * @param array $errors
35
     * @return array
36
     */
37
    public function normalizeErrors(array $errors): array
38
    {
39
        $preparedErrors = [];
40
41
        $status = $errors['status'] ?? null;
42
43
        if (in_array($status, ['error', 'exception'], true)) {
44
            list($errorKey, $errorMessage) = $this->prepareError($errors);
45
            $preparedErrors[$errorKey] = $errorMessage;
46
        }
47
48
        return $preparedErrors;
49
    }
50
51
    /**
52
     * @param array $error
53
     * @return array
54
     */
55
    protected function prepareError(array $error): array
56
    {
57
        return [
58
            ArrayHelper::getValue($error, 'status'),
59
            ArrayHelper::getValue($error, 'message')
60
        ];
61
    }
62
}
63