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

LinkedResourceTrait::getLink()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace AcquiaCloudApi\Traits;
4
5
use AcquiaCloudApi\Exception\NoLinkedResourceException;
6
use AcquiaCloudApi\Exception\LinkedResourceNotFoundException;
7
8
/**
9
 * Trait LinkedResponseTrait
10
 *
11
 * @package AcquiaCloudApi\CloudApi
12
 */
13
trait LinkedResourceTrait
14
{
15
16
    /**
17
     * @param string $name
18
     * @return array{type:string, path:string, responseClass:class-string}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{type:string, path:...onseClass:class-string} at position 12 could not be parsed: Unknown type name 'class-string' at position 12 in array{type:string, path:string, responseClass:class-string}.
Loading history...
19
     * @throws NoLinkedResourceException
20
     * @throws LinkedResourceNotFoundException
21
     */
22
    public function getLink(string $name)
23
    {
24
        if (!property_exists($this, 'links')) {
25
            throw new NoLinkedResourceException('No linked resources for ' . get_called_class());
26
        } elseif (!property_exists($this->links, $name)) {
27
            throw new LinkedResourceNotFoundException('No property exists for ' . $name . '. Available links are ' . implode(' ', array_keys((array) $this->links)));
28
        } elseif (!property_exists($this->links->$name, 'href')) {
29
            throw new LinkedResourceNotFoundException('No href property exists for ' . $name);
30
        }
31
32
        return ['type' => $name, 'path' => $this->links->$name->href, 'responseClass' => get_class($this)];
33
    }
34
}
35