Passed
Pull Request — master (#111)
by Adam
03:34 queued 01:29
created

CollectionResponse   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 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
        parent::__construct(
42
            array_map(
43
                function ($child) use ($class) {
44
                    return new $class($child);
45
                },
46
                $items
47
            ),
48
            self::ARRAY_AS_PROPS
49
        );
50
    }
51
}
52