Completed
Push — master ( 68e848...62c64b )
by Jean C.
14s
created

Links::getLink()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Moip\Helper;
4
5
use stdClass;
6
7
/**
8
 * Class that represents the HATEOAS structure of the API resources.
9
 */
10
class Links
11
{
12
    /**
13
     * @var array[string]Link Map a link name to it's representation.
14
     */
15
    private $links;
16
17
    /**
18
     * @var \stdClass
19
     */
20
    private $checkout;
21
22
    /**
23
     * Links constructor.
24
     *
25
     * @param stdClass $links "_link" returned from the API, if there isn't any.
26
     */
27
    public function __construct(stdClass $links)
28
    {
29
        $this->links = $links;
30
        $this->generateMethods();
31
    }
32
33
    /**
34
     * @return mixed
35
     */
36
    public function getSelf()
37
    {
38
        return $this->links->self->href;
39
    }
40
41
    /**
42
     * @param null|string $pay
43
     *
44
     * @return mixed
45
     */
46
    public function getCheckout($pay)
47
    {
48
        return $this->links->checkout->$pay->redirectHref;
49
    }
50
51
    /**
52
     * @param null|string $link
53
     *
54
     * @return mixed
55
     */
56
    public function getLink($link)
57
    {
58
        if (isset($this->links->$link->redirectHref)) {
59
            return $this->links->$link->redirectHref;
60
        }
61
62
        if (isset($this->links->$link->href)) {
63
            return $this->links->$link->href;
64
        }
65
66
        return $this->links->$link;
67
    }
68
69
    /**
70
     * @return mixed
71
     */
72
    public function getAllCheckout()
73
    {
74
        return $this->checkout;
75
    }
76
77
    private function generateMethods()
78
    {
79
        $this->checkout = new stdClass();
80
81
        if (isset($this->links->checkout)) {
82
            foreach ($this->links->checkout as $method => $link) {
83
                $this->checkout->$method = $link->redirectHref;
84
            }
85
        }
86
    }
87
}
88