Wrapper::getObjectId()   D
last analyzed

Complexity

Conditions 25
Paths 49

Size

Total Lines 83
Code Lines 47

Duplication

Lines 46
Ratio 55.42 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 46
loc 83
rs 4.8442
cc 25
eloc 47
nc 49
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Gregoriohc\LaravelTrello;
4
5
use Illuminate\Config\Repository;
6
use Trello\Client;
7
use Trello\Manager;
8
9
class Wrapper
10
{
11
    /**
12
     * The config instance
13
     *
14
     * @var Repository
15
     */
16
    public $config;
17
18
    /**
19
     * The trello client instance
20
     *
21
     * @var \Trello\Client
22
     */
23
    private $client;
24
25
    /**
26
     * The trello manager instance
27
     *
28
     * @var \Trello\Manager
29
     */
30
    private $manager;
31
32
    /**
33
     * Client cache
34
     *
35
     * @var array
36
     */
37
    private $cache;
38
39
    /**
40
     * Client constructor
41
     *
42
     * @param Repository $config
43
     */
44
    public function __construct(Repository $config)
45
    {
46
        // Get the config data
47
        $this->config = $config;
48
49
        // Make the client instance
50
        $this->client = new Client();
51
        $this->client->authenticate($this->config->get('trello.api_key'), $this->config->get('trello.api_token'), Client::AUTH_URL_CLIENT_ID);
52
    }
53
54
    /**
55
     * Returns trello manager instance
56
     *
57
     * @return \Trello\Manager
58
     */
59
    public function manager()
60
    {
61
        if (!isset($this->manager)) {
62
            $this->manager = new Manager($this->client);
63
        }
64
65
        return $this->manager;
66
    }
67
68
    /**
69
     * Returns a trello object id by a given name
70
     *
71
     * @param string $type
72
     * @param string $name
73
     * @param array $options
74
     * @return bool|string
75
     */
76
    public function getObjectId($type, $name, $options = [])
77
    {
78
        switch ($type) {
79
            case 'organization':
80 View Code Duplication
                if (!isset($this->cache['organizations'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
                    $this->cache['organizations'] = $this->api('member')->organizations()->all('me');
0 ignored issues
show
Documentation Bug introduced by
The method api does not exist on object<Gregoriohc\LaravelTrello\Wrapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
82
                }
83
84
                foreach ($this->cache['organizations'] as $item) {
85
                    if ($name == $item['name']) {
86
                        return $item['id'];
87
                    }
88
                }
89
90
                break;
91
            case 'board':
92
                if (!isset($options['organization'])) {
93
                    $options['organization'] = $this->config->get('trello.organization');
94
                }
95
                $organizationId = $this->getObjectId('organization', $options['organization']);
96
97 View Code Duplication
                if (!isset($this->cache['boards'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
                    $this->cache['boards'] = $this->api('member')->boards()->all('me');
0 ignored issues
show
Documentation Bug introduced by
The method api does not exist on object<Gregoriohc\LaravelTrello\Wrapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
99
                }
100
101
                foreach ($this->cache['boards'] as $item) {
102
                    if ($name == $item['name'] && $organizationId == $item['idOrganization']) {
103
                        return $item['id'];
104
                    }
105
                }
106
107
                // Workaround for shared boards
108
                foreach ($this->cache['boards'] as $item) {
109
                    if ($name == $item['name']) {
110
                        return $item['id'];
111
                    }
112
                }
113
114
                break;
115 View Code Duplication
            case 'list':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
                if (!isset($options['organization'])) {
117
                    $options['organization'] = $this->config->get('trello.organization');
118
                }
119
                if (!isset($options['board'])) {
120
                    $options['board'] = $this->config->get('trello.board');
121
                }
122
123
                $boardId = $this->getObjectId('board', $options['board'], ['organization' => $options['organization']]);
124
                if (!isset($this->cache['lists'][$boardId])) {
125
                    $this->cache['lists'][$boardId] = $this->api('board')->lists()->all($boardId);
0 ignored issues
show
Documentation Bug introduced by
The method api does not exist on object<Gregoriohc\LaravelTrello\Wrapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
126
                }
127
128
                foreach ($this->cache['lists'][$boardId] as $item) {
129
                    if ($name == $item['name']) {
130
                        return $item['id'];
131
                    }
132
                }
133
134
                break;
135 View Code Duplication
            case 'label':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
                if (!isset($options['organization'])) {
137
                    $options['organization'] = $this->config->get('trello.organization');
138
                }
139
                if (!isset($options['board'])) {
140
                    $options['board'] = $this->config->get('trello.board');
141
                }
142
143
                $boardId = $this->getObjectId('board', $options['board'], ['organization' => $options['organization']]);
144
                if (!isset($this->cache['labels'][$boardId])) {
145
                    $this->cache['labels'][$boardId] = $this->api('board')->labels()->all($boardId);
0 ignored issues
show
Documentation Bug introduced by
The method api does not exist on object<Gregoriohc\LaravelTrello\Wrapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
146
                }
147
148
                foreach ($this->cache['labels'][$boardId] as $item) {
149
                    if ($name == $item['name']) {
150
                        return $item['id'];
151
                    }
152
                }
153
154
                break;
155
        }
156
157
        return false;
158
    }
159
160
    /**
161
     * Returns default organization id
162
     *
163
     * @return Manager
164
     */
165
    public function getDefaultOrganizationId()
166
    {
167
        return $this->getObjectId('organization', $this->config->get('trello.organization'));
168
    }
169
170
    /**
171
     * Returns default board id
172
     *
173
     * @return Manager
174
     */
175
    public function getDefaultBoardId()
176
    {
177
        return $this->getObjectId('board', $this->config->get('trello.board'));
178
    }
179
180
    /**
181
     * Returns default list id
182
     *
183
     * @return Manager
184
     */
185
    public function getDefaultListId()
186
    {
187
        return $this->getObjectId('list', $this->config->get('trello.list'));
188
    }
189
190
    /**
191
     * Handle dynamic calls to the client
192
     *
193
     * @param $name
194
     * @param $arguments
195
     *
196
     * @return mixed
197
     */
198
    public function __call($name, $arguments)
199
    {
200
        return call_user_func_array([$this->client, $name], $arguments);
201
    }
202
}