Completed
Push — master ( d56c86...719266 )
by Nate
06:29 queued 04:50
created

Interpret::transform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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