AbstractResource   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A __get() 0 4 1
A getLink() 0 7 2
A hasLink() 0 4 1
1
<?php
2
3
namespace LibLynx\Connect\Resource;
4
5
use LibLynx\Connect\Exception\LogicException;
6
7
/**
8
 * Class LibLynxResource provides a base class for all HAL-style resources retrieved from the LibLynx API.
9
 *
10
 * All JSON returned by the LibLynx API is modelled on the Hypertext Application Language
11
 * http://stateless.co/hal_specification.html
12
 *
13
 * @package LibLynx\Connect
14
 *
15
 * @property \stdClass $_links
16
 */
17
abstract class AbstractResource
18
{
19
    public function __construct($obj)
20
    {
21
        $vars = get_object_vars($obj);
22
        foreach ($vars as $name => $value) {
23
            $this->$name = $value;
24
        }
25
    }
26
27
    public function __get($name)
28
    {
29
        throw new LogicException("No value called $name");
30
    }
31
32
    /**
33
     * @param $name
34
     * @return mixed
35
     * @throws LogicException if link with name isn't present
36
     */
37
    public function getLink($name)
38
    {
39
        if (isset($this->_links->$name)) {
40
            return $this->_links->$name->href;
41
        }
42
        throw new LogicException("resource did not contain a ".$name." link");
43
    }
44
45
    public function hasLink($name)
46
    {
47
        return isset($this->_links->$name);
48
    }
49
}
50