Completed
Push — master ( c30a0b...37c0e6 )
by Nate
08:20
created

__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 4
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\base\Element;
12
use craft\base\ElementInterface;
13
use craft\helpers\Json;
14
use flipbox\craft\hubspot\fields\ObjectsFieldInterface;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class PopulateElementErrorsFromUpsertResponse
22
{
23
    /**
24
     * @param ResponseInterface $response
25
     * @param ElementInterface $element
26
     * @param ObjectsFieldInterface $field
27
     * @param string|null $id
28
     * @return ElementInterface
29
     */
30
    public function __invoke(
31
        ResponseInterface $response,
32
        ElementInterface $element,
33
        ObjectsFieldInterface $field,
34
        string $id = null
35
    ): ElementInterface {
36
        /** @var Element $element */
37
38
        $data = Json::decodeIfJson(
39
            $response->getBody()->getContents()
40
        );
41
42
        $errors = call_user_func_array(
43
            new InterpretUpsertResponseErrors(),
44
            [
45
                $data
46
            ]
47
        );
48
49
        $element->addErrors($errors);
50
51
        return $element;
52
    }
53
}
54