GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#62)
by
unknown
02:09
created

Transmission::getHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Transmission;
3
4
use Transmission\Model\Torrent;
5
use Transmission\Model\Session;
6
use Transmission\Model\FreeSpace;
7
use Transmission\Model\Stats\Session as SessionStats;
8
use Transmission\Util\PropertyMapper;
9
use Transmission\Util\ResponseValidator;
10
11
/**
12
 * @author Ramon Kleiss <[email protected]>
13
 */
14
class Transmission
15
{
16
    /**
17
     * @var Client
18
     */
19
    protected $client;
20
21
    /**
22
     * @var ResponseValidator
23
     */
24
    protected $validator;
25
26
    /**
27
     * @var PropertyMapper
28
     */
29
    protected $mapper;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param string  $host
35
     * @param integer $port
36
     * @param string  $path
37
     */
38
    public function __construct($host = null, $port = null, $path = null)
39
    {
40
        $this->setClient(new Client($host, $port, $path));
41
        $this->setMapper(new PropertyMapper());
42
        $this->setValidator(new ResponseValidator());
43
    }
44
45
    /**
46
     * Get all the torrents in the download queue
47
     *
48
     * @return Torrent[]
49
     */
50
    public function all()
51
    {
52
        $client   = $this->getClient();
53
        $mapper   = $this->getMapper();
54
        $response = $this->getClient()->call(
55
            'torrent-get',
56
            array('fields' => array_keys(Torrent::getMapping()))
57
        );
58
59
        $torrents = array_map(function ($data) use ($mapper, $client) {
60
            return $mapper->map(
61
                new Torrent($client),
62
                $data
63
            );
64
        }, $this->getValidator()->validate('torrent-get', $response));
65
66
        return $torrents;
67
    }
68
69
    /**
70
     * Get a specific torrent from the download queue
71
     *
72
     * @param  integer                    $id
73
     * @return Torrent
74
     * @throws \RuntimeException
75
     */
76
    public function get($id)
77
    {
78
        $client   = $this->getClient();
79
        $mapper   = $this->getMapper();
80
        $response = $this->getClient()->call('torrent-get', array(
81
            'fields' => array_keys(Torrent::getMapping()),
82
            'ids'    => array($id)
83
        ));
84
85
        $torrent = array_reduce(
86
            $this->getValidator()->validate('torrent-get', $response),
87
            function ($torrent, $data) use ($mapper, $client) {
88
                return $torrent ? $torrent : $mapper->map(new Torrent($client), $data);
89
            });
90
91
        if (!$torrent instanceof Torrent) {
92
            throw new \RuntimeException(sprintf("Torrent with ID %s not found", $id));
93
        }
94
95
        return $torrent;
96
    }
97
98
    /**
99
     * Get the Transmission session
100
     *
101
     * @return Session
102
     */
103 View Code Duplication
    public function getSession()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
104
    {
105
        $response = $this->getClient()->call(
106
            'session-get',
107
            array()
108
        );
109
110
        return $this->getMapper()->map(
111
            new Session($this->getClient()),
112
            $this->getValidator()->validate('session-get', $response)
113
        );
114
    }
115
116
    /**
117
     * @return SessionStats
118
     */
119 View Code Duplication
    public function getSessionStats()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
120
    {
121
        $response = $this->getClient()->call(
122
            'session-stats',
123
            array()
124
        );
125
126
        return $this->getMapper()->map(
127
            new SessionStats(),
128
            $this->getValidator()->validate('session-stats', $response)
129
        );
130
    }
131
132
    /**
133
     * Get Free space
134
     * @param  string $path
135
     * @return FreeSpace
136
     */
137
    public function getFreeSpace($path=null)
138
    {
139
        if (!$path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
140
            $path = $this->getSession()->getDownloadDir();
141
        }
142
        $response = $this->getClient()->call(
143
            'free-space',
144
            array('path'=>$path)
145
        );
146
147
        return $this->getMapper()->map(
148
            new FreeSpace(),
149
            $this->getValidator()->validate('free-space', $response)
150
        );
151
    }
152
153
    /**
154
     * Add a torrent to the download queue
155
     *
156
     * @param  string   $torrent
157
     * @param  boolean  $metainfo
158
     * @param  string   $savepath
159
     * @return Torrent
160
     */
161
    public function add($torrent, $metainfo = false, $savepath = null)
162
    {
163
        $parameters = array($metainfo ? 'metainfo' : 'filename' => $torrent);
164
165
        if ($savepath !== null) {
166
            $parameters['download-dir'] = (string) $savepath;
167
        }
168
169
        $response = $this->getClient()->call(
170
            'torrent-add',
171
            $parameters
172
        );
173
174
        return $this->getMapper()->map(
175
            new Torrent($this->getClient()),
176
            $this->getValidator()->validate('torrent-add', $response)
177
        );
178
    }
179
180
    /**
181
     * Start the download of a torrent
182
     *
183
     * @param Torrent $torrent
184
     * @param bool    $now
185
     */
186
    public function start(Torrent $torrent, $now = false)
187
    {
188
        $this->getClient()->call(
189
            $now ? 'torrent-start-now' : 'torrent-start',
190
            array('ids' => array($torrent->getId()))
191
        );
192
    }
193
194
    /**
195
     * Stop the download of a torrent
196
     *
197
     * @param Torrent $torrent
198
     */
199
    public function stop(Torrent $torrent)
200
    {
201
        $this->getClient()->call(
202
            'torrent-stop',
203
            array('ids' => array($torrent->getId()))
204
        );
205
    }
206
207
    /**
208
     * Verify the download of a torrent
209
     *
210
     * @param Torrent $torrent
211
     */
212
    public function verify(Torrent $torrent)
213
    {
214
        $this->getClient()->call(
215
            'torrent-verify',
216
            array('ids' => array($torrent->getId()))
217
        );
218
    }
219
220
    /**
221
     * Request a reannounce of a torrent
222
     *
223
     * @param Torrent $torrent
224
     */
225
    public function reannounce(Torrent $torrent)
226
    {
227
        $this->getClient()->call(
228
            'torrent-reannounce',
229
            array('ids' => array($torrent->getId()))
230
        );
231
    }
232
233
    /**
234
     * Remove a torrent from the download queue
235
     *
236
     * @param Torrent $torrent
237
     */
238
    public function remove(Torrent $torrent, $localData = false)
239
    {
240
        $arguments = array('ids' => array($torrent->getId()));
241
242
        if ($localData) {
243
            $arguments['delete-local-data'] = true;
244
        }
245
246
        $this->getClient()->call('torrent-remove', $arguments);
247
    }
248
249
    /**
250
     * Checks whether or not Transmission is listening on configured port/host
251
     *
252
     * @return boolean
253
     */
254
    public function isAvailable()
255
    {
256
        try {
257
            $this->getClient()->call('', []);
258
259
            return true;
260
        } catch (\RuntimeException $e) {
261
            return false;
262
        }
263
    }
264
265
    /**
266
     * Set the client used to connect to Transmission
267
     *
268
     * @param Client $client
269
     */
270
    public function setClient(Client $client)
271
    {
272
        $this->client = $client;
273
    }
274
275
    /**
276
     * Get the client used to connect to Transmission
277
     *
278
     * @return Client
279
     */
280
    public function getClient()
281
    {
282
        return $this->client;
283
    }
284
285
    /**
286
     * Set the hostname of the Transmission server
287
     *
288
     * @param string $host
289
     */
290
    public function setHost($host)
291
    {
292
        $this->getClient()->setHost($host);
293
    }
294
295
    /**
296
     * Get the hostname of the Transmission server
297
     *
298
     * @return string
299
     */
300
    public function getHost()
301
    {
302
        return $this->getClient()->getHost();
303
    }
304
305
    /**
306
     * Set the port the Transmission server is listening on
307
     *
308
     * @param integer $port
309
     */
310
    public function setPort($port)
311
    {
312
        $this->getClient()->setPort($port);
313
    }
314
315
    /**
316
     * Get the port the Transmission server is listening on
317
     *
318
     * @return integer
319
     */
320
    public function getPort()
321
    {
322
        return $this->getClient()->getPort();
323
    }
324
325
    /**
326
     * Set the mapper used to map responses from Transmission to models
327
     *
328
     * @param PropertyMapper $mapper
329
     */
330
    public function setMapper(PropertyMapper $mapper)
331
    {
332
        $this->mapper = $mapper;
333
    }
334
335
    /**
336
     * Get the mapper used to map responses from Transmission to models
337
     *
338
     * @return PropertyMapper
339
     */
340
    public function getMapper()
341
    {
342
        return $this->mapper;
343
    }
344
345
    /**
346
     * Set the validator used to validate Transmission responses
347
     *
348
     * @param ResponseValidator $validator
349
     */
350
    public function setValidator(ResponseValidator $validator)
351
    {
352
        $this->validator = $validator;
353
    }
354
355
    /**
356
     * Get the validator used to validate Transmission responses
357
     *
358
     * @return ResponseValidator
359
     */
360
    public function getValidator()
361
    {
362
        return $this->validator;
363
    }
364
}
365