Passed
Pull Request — master (#111)
by Adam
02:26
created

CollectionResponse   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 43
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 30 4
1
<?php
2
3
namespace AcquiaCloudApi\Response;
4
5
use AcquiaCloudApi\Traits\LinkedResourceTrait;
6
7
/**
8
 * @template TValue
9
 * @template-extends \ArrayObject<int,TValue>
10
 */
11
abstract class CollectionResponse extends \ArrayObject
12
{
13
    use LinkedResourceTrait;
0 ignored issues
show
Bug introduced by
The trait AcquiaCloudApi\Traits\LinkedResourceTrait requires the property $href which is not provided by AcquiaCloudApi\Response\CollectionResponse.
Loading history...
14
15
    /**
16
     * @var object $links
17
     */
18
    public $links;
19
20
    /**
21
     * @param string $class
22
     * @param object|array<int, object> $body
23
     */
24
    public function __construct($class, $body)
25
    {
26
        // Links will not be available if we create a CollectionResponse manually within
27
        // a GenericResponse e.g. a TeamsResponse as part of a MemberResponse.
28
        if (is_object($body)) {
29
            $this->links = $body->_links;
30
31
            if (!property_exists($body, '_embedded') || !property_exists($body->_embedded, 'items')) {
32
                throw new \Exception('CollectionResponse does not contain embedded items.');
33
            }
34
            $items = $body->_embedded->items;
35
        } else {
36
            $items = $body;
37
        }
38
39
        $class = '\AcquiaCloudApi\Response\\' . $class;
40
41
42
        // var_dump($body);
43
44
45
46
        parent::__construct(
47
            array_map(
48
                function ($child) use ($class) {
49
                    return new $class($child);
50
                },
51
                $items
52
            ),
53
            self::ARRAY_AS_PROPS
54
        );
55
    }
56
}
57