Passed
Pull Request — master (#111)
by Adam
03:11
created

GenericResponse   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getLink() 0 10 4
1
<?php
2
3
namespace AcquiaCloudApi\Response;
4
5
use AcquiaCloudApi\Exception\NoLinkedResourceException;
6
use AcquiaCloudApi\Exception\LinkedResourceNotFoundException;
7
8
abstract class GenericResponse
9
{
10
11
    /**
12
     * @param  array<string,string>
13
     * @return string
14
     * @throws NoLinkedResourceException
15
     * @throws LinkedResourceNotFoundException
16
     */
17
    public function getLink(string $name)
18
    {
19
        if (!property_exists($this, 'links')) {
20
            throw new NoLinkedResourceException('No linked resources for' . get_called_class());
21
        } elseif (!property_exists($this->links, $name)) {
22
            throw new LinkedResourceNotFoundException('No property exists for ' . $name . '. Available links are ' . implode(' ', array_keys(get_object_vars($this->links))));
23
        } elseif (!property_exists($this->links->$name, 'href')) {
24
            throw new LinkedResourceNotFoundException('href property not found on ' . $name);
25
        }
26
        return [$name => $this->links->$name->href];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($name => $this->links->$name->href) returns the type array which is incompatible with the documented return type string.
Loading history...
27
    }
28
}
29