Passed
Push — master ( 3bf788...763f00 )
by Jon
02:39 queued 10s
created

Social::getComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace FlatPlan\Components;
2
3
class Social extends AbstractComponent {
4
5
    private $role;
6
    private $url;
7
8
    protected $roles = ['instagram', 'facebook_post', 'tweet'];
9
10
    /**
11
     * @param string $role
12
     * @param string $url
13
     * @return void
14
     */
15
    public function __construct($role, $url)
16
    {
17
        $this->setRole($role);
18
        $this->setUrl($url);
19
    }
20
21
    public function setRole($role)
22
    {
23
        if (!in_array($role, $this->roles)) {
24
            throw new \ErrorException('Invalid role supplied.');
25
        }
26
        $this->role = $role;
27
    }
28
29
    public function getRole()
30
    {
31
        return $this->role;
32
    }
33
34
    public function setUrl($url)
35
    {
36
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
37
            throw new \ErrorException('Invalid url supplied.');
38
        }
39
        $this->url = $url;
40
    }
41
42
    public function getUrl()
43
    {
44
        return $this->url;
45
    }
46
47
    public function getComponent()
48
    {
49
        $component = new \stdClass();
50
        $component->role = $this->getRole();
51
        $component->url  = $this->getUrl();
52
        return $component;
53
    }
54
55
    public function __toString()
56
    {
57
        return json_encode($this->getComponent());
58
    }
59
}
60