Completed
Pull Request — master (#59)
by
unknown
03:43 queued 01:10
created

Links   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 2 Features 2
Metric Value
wmc 7
c 4
b 2
f 2
lcom 1
cbo 1
dl 0
loc 54
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parseLinks() 0 10 3
A getLinks() 0 4 1
A getLink() 0 7 2
1
<?php
2
3
namespace Moip;
4
5
/**
6
 * Class that represents the HATEOAS structure of the API resources.
7
 */
8
class Links
9
{
10
    /**
11
     * @var array[string]Link Map a link name to it's representation.
12
     */
13
    private $links;
14
15
    /**
16
     * Links constructor.
17
     *
18
     * @param \stdClass $links "_link" returned from the API, if there isn't any.
19
     */
20
    public function __construct(\stdClass $links)
21
    {
22
        $this->parseLinks($links);
23
    }
24
25
    /**
26
     * @param \stdClass $links
27
     */
28
    private function parseLinks(\stdClass $links)
29
    {
30
        foreach (get_object_vars($links) as $property => $value) {
31
            if ($property == 'checkout') { // nested links? eg: checkout=>payOnlineBankDebitItau,payCreditCard...
32
                $this->parseLinks($value);
33
            } else {
34
                $this->links[$property] = new Link($property, $value);
35
            }
36
        }
37
    }
38
39
    /**
40
     * Returns all the links from the HATEOAS structure.
41
     *
42
     * @return array[string]Link.
0 ignored issues
show
Documentation introduced by
The doc-type array[string]Link. could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
43
     */
44
    public function getLinks()
45
    {
46
        return $this->links;
47
    }
48
49
    /**
50
     * @param $name string Links name, e.g: "self".
51
     *
52
     * @return null|Link Link object of the corresponding $name or null if the link doesn't exist.
53
     */
54
    public function getLink($name)
55
    {
56
        if (!isset($this->links[$name])) {
57
            return;
58
        }
59
        return $this->links[$name];
60
    }
61
}
62