Completed
Push — master ( c44a53...3a33b3 )
by Sergey
02:56
created

Api::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace seregazhuk\Favro\Api;
4
5
use seregazhuk\Favro\Api\Endpoints\Cards;
6
use seregazhuk\Favro\Api\Endpoints\Comments;
7
use seregazhuk\Favro\Api\Endpoints\TaskLists;
8
use seregazhuk\Favro\Api\Endpoints\Tasks;
9
use seregazhuk\Favro\Api\Endpoints\Users;
10
use seregazhuk\Favro\Api\Endpoints\Columns;
11
use seregazhuk\Favro\Api\Endpoints\Widgets;
12
use seregazhuk\Favro\Api\Endpoints\Endpoint;
13
use seregazhuk\Favro\Api\Endpoints\Collections;
14
use seregazhuk\Favro\Api\Endpoints\Organizations;
15
use seregazhuk\Favro\Api\Endpoints\EndpointsContainer;
16
use seregazhuk\Favro\Exceptions\WrongOrganizationName;
17
18
/**
19
 * Class Api
20
 *
21
 * @property Organizations $organizations
22
 * @property Users $users
23
 * @property Collections $collections
24
 * @property Widgets $widgets
25
 * @property Columns $columns
26
 * @property Cards $cards
27
 * @property Tasks $tasks
28
 * @property TaskLists $tasklists
29
 * @property Comments $comments
30
 *
31
 * @method array getRateInfo
32
 */
33
class Api
34
{
35
    /**
36
     * @var EndpointsContainer
37
     */
38
    protected $endpointsContainer;
39
40
    /**
41
     * @var string
42
     */
43
    protected $organizationId;
44
45
    public function __construct(EndpointsContainer $endpointsContainer)
46
    {
47
        $this->endpointsContainer = $endpointsContainer;
48
    }
49
50
    /**
51
     * Magic method to access different endpoints.
52
     *
53
     * @param string $endpoint
54
     *
55
     * @return Endpoint
56
     */
57
    public function __get($endpoint)
58
    {
59
        $endpoint = $this->endpointsContainer->resolve($endpoint);
60
61
        if (method_exists($endpoint, 'setOrganizationId')) {
62
            $endpoint->setOrganizationId($this->organizationId);
63
        }
64
65
        return $endpoint;
66
    }
67
68
    /**
69
     * @param $organizationName
70
     * @return $this
71
     */
72
    public function setOrganization($organizationName)
73
    {
74
        if($organization = $this->getOrganizationByName($organizationName)) {
75
            $this->setOrganizationId($organization['organizationId']);
76
        }
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param int $organizationId
83
     * @return $this
84
     */
85
    public function setOrganizationId($organizationId)
86
    {
87
        $this->organizationId = $organizationId;
0 ignored issues
show
Documentation Bug introduced by
The property $organizationId was declared of type string, but $organizationId is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getOrganizationId()
96
    {
97
        return $this->organizationId;
98
    }
99
100
    /**
101
     * @param string $name
102
     * @param array $arguments
103
     * @return mixed
104
     */
105
    public function __call($name, $arguments)
106
    {
107
        return call_user_func([$this->endpointsContainer, $name], $arguments);
108
    }
109
110
    /**
111
     * @param $organization
112
     * @return array|bool
113
     * @throws WrongOrganizationName
114
     */
115
    protected function getOrganizationByName($organization)
116
    {
117
        $organizations = $this->organizations->getAll();
118
        foreach ($organizations['entities'] as $entity) {
119
            if ($entity['name'] == $organization) {
120
                return $entity;
121
            }
122
        }
123
124
        throw new WrongOrganizationName("Organization $organization not found!");
125
    }
126
}