PHPushbullet   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 85.54%

Importance

Changes 10
Bugs 1 Features 1
Metric Value
wmc 31
c 10
b 1
f 1
lcom 1
cbo 3
dl 0
loc 237
ccs 71
cts 83
cp 0.8554
rs 9.8

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A devices() 0 12 2
A user() 0 8 1
A device() 0 16 3
A channel() 0 6 1
A all() 0 10 3
B push() 0 26 6
A getClient() 0 4 1
A pushRequest() 0 7 1
A getDeviceIden() 0 12 4
A argsToArray() 0 8 2
A fromJson() 0 4 1
A __call() 0 13 2
1
<?php
2
3
namespace PHPushbullet;
4
5
/**
6
 * @method array address(string $name, string|array $address)
7
 * @method array file(string $file_name, string $file_url, string $body = null)
8
 * @method array link(string $title, string $url, string $body = null)
9
 * @method array list(string $title, array $items)
10
 * @method array note(string $title, string $body)
11
 */
12
13
class PHPushbullet
14
{
15
    /**
16
     * An instance of the Guzzle client to make requests
17
     *
18
     * @var \GuzzleHttp\Client $api
19
     */
20
    protected $api;
21
22
    /**
23
     * The set of devices we are currently pushing to
24
     *
25
     * @var array $devices
26
     */
27
    protected $devices = [];
28
29
    /**
30
     * The set of users we are currently pushing to,
31
     * an array of emails
32
     *
33
     * @var array $users
34
     */
35
    protected $users = [];
36
37
    /**
38
     * The set of channels we are currntly pushing to
39
     *
40
     * @var array $channels
41
     */
42
    protected $channels = [];
43
44
    /**
45
     * An array of all devices available
46
     *
47
     * @var array $all_devices
48
     */
49
    protected $all_devices = [];
50
51 27
    public function __construct($access_token = null, Connection $connection =  null, array $config = [])
52
    {
53 27
        $access_token = $access_token ?: getenv('pushbullet.access_token');
54
55 27
        if (!$access_token) {
56
            throw new \Exception('Your Pushbullet access token is not set.');
57
        }
58
59 27
        $connection = $connection ?: new Connection($access_token, null, $config);
60
61 27
        $this->api = $connection->client();
62 27
    }
63
64
    /**
65
     * Get a list of all of the devices available
66
     *
67
     * @return array
68
     */
69 12
    public function devices()
70
    {
71 12
        if (empty($this->all_devices)) {
72 12
            $response = $this->fromJson($this->api->get('devices'));
73
74 12
            $this->all_devices = array_map(function ($device) {
75 12
                return new Device($device);
76 12
            }, $response['devices']);
77 12
        }
78
79 12
        return $this->all_devices;
80
    }
81
82 1
    public function user()
83
    {
84 1
        $this->users = array_merge($this->argsToArray(func_get_args()), $this->users);
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a object<PHPushbullet\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85 1
        $this->users = array_filter($this->users);
86 1
        $this->users = array_unique($this->users);
87
88 1
        return $this;
89
    }
90
91
    /**
92
     * Set the passed in device(s) for the current push
93
     *
94
     * @return \PHPushbullet\PHPushbullet
95
     */
96 11
    public function device()
97
    {
98 11
        $devices = $this->argsToArray(func_get_args());
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a object<PHPushbullet\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
100 11
        foreach ($devices as $destination) {
101 11
            $device = $this->getDeviceIden($destination);
102
103 11
            if (!$device) {
104 1
                throw new \Exception("{$destination} is not a valid device.");
105
            }
106
107 10
            $this->devices[] = $device;
108 10
        }
109
110 10
        return $this;
111
    }
112
113
    /**
114
     * Set the passed in channel(s) for the current push
115
     *
116
     * @return \PHPushbullet\PHPushbullet
117
     */
118 2
    public function channel()
119
    {
120 2
        $this->channels = array_merge($this->argsToArray(func_get_args()));
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a object<PHPushbullet\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
122 2
        return $this;
123
    }
124
125
    /**
126
     * Set all of the devices for the current push
127
     *
128
     * @return \PHPushbullet\PHPushbullet
129
     */
130
    public function all()
131
    {
132
        foreach ($this->devices() as $device) {
133
            if ($device->pushable == true) {
134
                $this->devices[] = $device->iden;
135
            }
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * Actually send the push
143
     *
144
     * @return array
145
     */
146 14
    public function push($request)
147
    {
148 14
        if (empty($this->devices) && empty($this->users) && empty($this->channels)) {
149 1
            throw new \Exception('You must specify something to push to.');
150
        }
151
152 13
        $responses = [];
153
154
        $destinations = [
155 13
            'devices'  => 'device_iden',
156 13
            'users'    => 'email',
157 13
            'channels' => 'channel_tag',
158 13
        ];
159
160 13
        foreach ($destinations as $destination => $key) {
161 13
            foreach ($this->{$destination} as $dest) {
162 13
                $responses[] = $this->pushRequest($request, [$key => $dest]);
163 13
            }
164 13
        }
165
166 13
        $this->devices  = [];
167 13
        $this->users    = [];
168 13
        $this->channels = [];
169
170 13
        return $responses;
171
    }
172
173
    public function getClient()
174
    {
175
        return $this->api;
176
    }
177
178
    /**
179
     * Create push request and... push it
180
     *
181
     * @param array $request
182
     * @param array $merge
183
     *
184
     * @return array
185
     */
186 13
    protected function pushRequest($request, $merge)
187
    {
188 13
        $request  = array_merge($request, $merge);
189 13
        $response = $this->api->post('pushes', ['json' => $request]);
190
191 13
        return $this->fromJson($response);
192
    }
193
194
    /**
195
     * Get the `iden` for the device by either the iden or nickname
196
     *
197
     * @param string $device
198
     *
199
     * @return mixed (boolean|string)
200
     */
201 11
    protected function getDeviceIden($device)
202
    {
203 11
        foreach ($this->devices() as $available_device) {
204 11
            foreach (['iden', 'nickname'] as $field) {
205 11
                if ($available_device->$field == $device) {
206 10
                    return $available_device->iden;
207
                }
208 11
            }
209 11
        }
210
211 1
        return false;
212
    }
213
214
    /**
215
     * @param type $args
216
     *
217
     * @return array
218
     */
219 14
    protected function argsToArray($args)
220
    {
221 14
        if (is_array($args[0])) {
222
            return $args[0];
223
        }
224
225 14
        return $args;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $args; (PHPushbullet\type) is incompatible with the return type documented by PHPushbullet\PHPushbullet::argsToArray of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
226
    }
227
228 15
    protected function fromJson($response)
229
    {
230 15
        return json_decode((string) $response->getBody(), true);
231
    }
232
233
    /**
234
     * Magic method, figures out what sort of push the user is trying to do
235
     */
236 14
    public function __call($method, $arguments)
237
    {
238 14
        $request_class = 'PHPushbullet\Request\Push' . ucwords($method);
239
240 14
        if (!class_exists($request_class)) {
241
            throw new \Exception(sprintf('Unknown method "%s"', $method));
242
        }
243
244 14
        $class   = new \ReflectionClass($request_class);
245 14
        $request = $class->newInstanceArgs($arguments);
246
247 14
        return $this->push($request->request());
248
    }
249
}
250