Completed
Branch cleanup (eec673)
by Paul
01:32
created

LibLynxResource   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 13
cts 13
cp 1
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;
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 LibLynxResource
18
{
19 10
    public function __construct($obj)
20
    {
21 10
        $vars = get_object_vars($obj);
22 10
        foreach ($vars as $name => $value) {
23 10
            $this->$name = $value;
24
        }
25 10
    }
26
27 2
    public function __get($name)
28
    {
29 2
        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 4
    public function getLink($name)
38
    {
39 4
        if (isset($this->_links->$name)) {
40 2
            return $this->_links->$name->href;
41
        }
42 2
        throw new LogicException("resource did not contain a ".$name." link");
43
    }
44
45 2
    public function hasLink($name)
46
    {
47 2
        return isset($this->_links->$name);
48
    }
49
}
50