InterpretResponseErrors   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 45
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 10 2
A normalizeErrors() 0 12 2
A prepareError() 0 7 1
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