InterpretResponseErrors::normalizeErrors()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.8666
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
        if (in_array($status, ['error', 'exception'], true)) {
43
            list($errorKey, $errorMessage) = $this->prepareError($errors);
44
            $preparedErrors[$errorKey] = $errorMessage;
45
        }
46
47
        return $preparedErrors;
48
    }
49
50
    /**
51
     * @param array $error
52
     * @return array
53
     */
54
    protected function prepareError(array $error): array
55
    {
56
        return [
57
            ArrayHelper::getValue($error, 'status'),
58
            ArrayHelper::getValue($error, 'message')
59
        ];
60
    }
61
}
62