Passed
Push — master ( aebc3a...07e708 )
by Irfaq
14:17
created

Torrent::isStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Transmission\Models;
4
5
use Transmission\Client;
6
7
/**
8
 * Torrent
9
 */
10
class Torrent extends AbstractModel
11
{
12
    /**
13
     * Statuses.
14
     *
15
     * Field: status
16
     */
17
    const STATUS_STOPPED = 0;
18
    const STATUS_CHECK_WAIT = 1;
19
    const STATUS_CHECK = 2;
20
    const STATUS_DOWNLOAD_WAIT = 3;
21
    const STATUS_DOWNLOAD = 4;
22
    const STATUS_SEED_WAIT = 5;
23
    const STATUS_SEED = 6;
24
    const STATUS_ISOLATED = 7;
25
26
    /**
27
     * Seed Ratio Modes.
28
     *
29
     * Field: seedRatioMode
30
     */
31
    const RATIO_USE_GLOBAL = 0;
32
    const RATIO_USE_LOCAL = 1;
33
    const RATIO_UNLIMITED = 2;
34
35
    /**
36
     * Errors.
37
     *
38
     * Field: error
39
     */
40
    const ERROR_NONE = 0;
41
    const ERROR_TRACKER_WARNING = 1;
42
    const ERROR_TRACKER_ERROR = 2;
43
    const ERROR_LOCAL_ERROR = 3;
44
45
    /**
46
     * Tracker Stats.
47
     *
48
     * Field: trackerStats->announceState
49
     */
50
    const TRACKER_INACTIVE = 0;
51
    const TRACKER_WAITING = 1;
52
    const TRACKER_QUEUED = 2;
53
    const TRACKER_ACTIVE = 3;
54
55
    /**
56
     * Common Fields.
57
     *
58
     * @var array
59
     */
60
    public static $fields = [
61
        'default'    => [
62
            'id',
63
            'eta',
64
            'name',
65
            'status',
66
            'isFinished',
67
            'files',
68
            'hashString',
69
            'downloadDir',
70
            'percentDone',
71
            'haveValid',
72
            'haveUnchecked',
73
            'totalSize',
74
            'leftUntilDone',
75
            'addedDate',
76
            'doneDate',
77
            'activityDate',
78
        ],
79
        'stats'      => [
80
            'error',
81
            'errorString',
82
            'eta',
83
            'isFinished',
84
            'isStalled',
85
            'leftUntilDone',
86
            'metadataPercentComplete',
87
            'peersConnected',
88
            'peersGettingFromUs',
89
            'peersSendingToUs',
90
            'percentDone',
91
            'queuePosition',
92
            'rateDownload',
93
            'rateUpload',
94
            'recheckProgress',
95
            'seedRatioMode',
96
            'seedRatioLimit',
97
            'sizeWhenDone',
98
            'status',
99
            'trackers',
100
            'downloadDir',
101
            'uploadedEver',
102
            'uploadRatio',
103
            'webseedsSendingToUs',
104
        ],
105
        'statsExtra' => [
106
            'activityDate',
107
            'corruptEver',
108
            'desiredAvailable',
109
            'downloadedEver',
110
            'fileStats',
111
            'haveUnchecked',
112
            'haveValid',
113
            'peers',
114
            'startDate',
115
            'trackerStats',
116
        ],
117
        'infoExtra'  => [
118
            'comment',
119
            'creator',
120
            'dateCreated',
121
            'files',
122
            'hashString',
123
            'isPrivate',
124
            'pieceCount',
125
            'pieceSize',
126
        ],
127
    ];
128
129
    /**
130
     * The attributes that should be cast to native and other supported types.
131
     *
132
     * Casts only when formatting is enabled.
133
     *
134
     * @var array
135
     */
136
    protected $casts = [
137
        'doneDate'      => 'datetime',
138
        'startDate'     => 'datetime',
139
        'activityDate'  => 'datetime',
140
        'addedDate'     => 'datetime',
141
        'dateCreated'   => 'datetime',
142
        'haveValid'     => 'bytes',
143
        'haveUnchecked' => 'bytes',
144
        'totalDone'     => 'bytes', // Custom
145
        'leftUntilDone' => 'bytes',
146
        'totalSize'     => 'bytes',
147
        'sizeWhenDone'  => 'bytes',
148
    ];
149
150
    /**
151
     * Get Name.
152
     *
153
     * @return mixed
154
     */
155
    public function getName()
156
    {
157
        return $this->get('name', 'Unknown');
158
    }
159
160
    /**
161
     * Get Percent Done.
162
     *
163
     * @param bool $format
164
     *
165
     * @return int
166
     */
167
    public function getPercentDone($format = false): int
168
    {
169
        $percentDone = $this->get('percentDone', 0);
170
171
        return $format ? $percentDone * 100 : $percentDone;
172
    }
173
174
    /**
175
     * Get Percent Done String.
176
     *
177
     * @return string
178
     */
179
    public function getPercentDoneString(): string
180
    {
181
        return $this->getPercentDone(true) . '%';
182
    }
183
184
    /**
185
     * Get Total Done.
186
     *
187
     * @param null|bool $castingEnabled
188
     *
189
     * @return mixed
190
     */
191
    public function getTotalDone($castingEnabled = null)
192
    {
193
        $value = $this->getHaveValid(false) + $this->getHaveUnchecked(false);
0 ignored issues
show
Bug introduced by
The method getHaveUnchecked() does not exist on Transmission\Models\Torrent. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
        $value = $this->getHaveValid(false) + $this->/** @scrutinizer ignore-call */ getHaveUnchecked(false);
Loading history...
Bug introduced by
The method getHaveValid() does not exist on Transmission\Models\Torrent. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
        $value = $this->/** @scrutinizer ignore-call */ getHaveValid(false) + $this->getHaveUnchecked(false);
Loading history...
194
195
        return $this->castAttribute('totalDone', $value, $castingEnabled ?? $this->castingEnabled);
196
    }
197
198
    /**
199
     * Get File Count.
200
     *
201
     * @return mixed
202
     */
203
    public function getFileCount()
204
    {
205
        return count($this->get('files', 0));
206
    }
207
208
    /**
209
     * Get a File by ID.
210
     *
211
     * @param int $id
212
     *
213
     * @return mixed
214
     */
215
    public function getFile(int $id)
216
    {
217
        return data_get($this->items, "files.$id");
218
    }
219
220
    /**
221
     * Check if status is stopped.
222
     *
223
     * @return bool
224
     */
225
    public function isStopped(): bool
226
    {
227
        return $this->isStatus(static::STATUS_STOPPED);
228
    }
229
230
    /**
231
     * Check if status is checking.
232
     *
233
     * @return bool
234
     */
235
    public function isChecking(): bool
236
    {
237
        return $this->isStatus(static::STATUS_CHECK);
238
    }
239
240
    /**
241
     * Check if status is downloading.
242
     *
243
     * @return bool
244
     */
245
    public function isDownloading(): bool
246
    {
247
        return $this->isStatus(static::STATUS_DOWNLOAD);
248
    }
249
250
    /**
251
     * Check if status is queued.
252
     *
253
     * @return bool
254
     */
255
    public function isQueued(): bool
256
    {
257
        return $this->isStatus(static::STATUS_DOWNLOAD_WAIT) || $this->isStatus(static::STATUS_SEED_WAIT);
258
    }
259
260
    /**
261
     * Check if status is seeding.
262
     *
263
     * @return bool
264
     */
265
    public function isSeeding(): bool
266
    {
267
        return $this->isStatus(static::STATUS_SEED);
268
    }
269
270
    /**
271
     * Check if done downloading.
272
     *
273
     * @return bool
274
     */
275
    public function isDone(): bool
276
    {
277
        return $this->get('leftUntilDone', 0) < 1;
278
    }
279
280
    /**
281
     * Check if given status matches the current status.
282
     *
283
     * @param $status
284
     *
285
     * @return bool
286
     */
287
    public function isStatus($status): bool
288
    {
289
        return $this->get('status') === $status;
290
    }
291
292
    /**
293
     * Get Status String.
294
     *
295
     * @return string
296
     */
297
    public function getStatusString()
298
    {
299
        switch ($this->get('status')) {
300
            case static::STATUS_STOPPED:
301
                return $this->get('isFinished', false) ? 'Seeding complete' : 'Paused';
302
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
303
            case static::STATUS_CHECK_WAIT:
304
                return 'Queued for verification';
305
                break;
306
            case static::STATUS_CHECK:
307
                return 'Verifying local data';
308
                break;
309
            case static::STATUS_DOWNLOAD_WAIT:
310
                return 'Queued for download';
311
                break;
312
            case static::STATUS_DOWNLOAD:
313
                return 'Downloading';
314
                break;
315
            case static::STATUS_SEED_WAIT:
316
                return 'Queued for seeding';
317
                break;
318
            case static::STATUS_SEED:
319
                return 'Seeding';
320
                break;
321
            case null:
322
                return 'Unknown';
323
                break;
324
            default:
325
                return 'Error';
326
                break;
327
        }
328
    }
329
330
    /**
331
     * Get Seed Ratio Limit.
332
     *
333
     * @param Client $client
334
     *
335
     * @return int|string
336
     */
337
    public function seedRatioLimit(Client $client)
338
    {
339
        switch ($this->get('seedRatioMode')) {
340
            case static::RATIO_USE_GLOBAL:
341
                return $client->seedRatioLimit();
342
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
343
            case static::RATIO_USE_LOCAL:
344
                return $this->get('seedRatioLimit');
345
                break;
346
            default:
347
                return -1;
348
                break;
349
        }
350
    }
351
352
    /**
353
     * Get Error Message.
354
     *
355
     * @return null|string
356
     */
357
    public function getErrorMessage()
358
    {
359
        $str = $this->get('errorString');
360
        switch ($this->get('error')) {
361
            case static::ERROR_TRACKER_WARNING:
362
                return 'Tracker returned a warning: ' . $str;
363
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
364
            case static::ERROR_TRACKER_ERROR:
365
                return 'Tracker returned an error: ' . $str;
366
                break;
367
            case static::ERROR_LOCAL_ERROR:
368
                return 'Error: ' . $str;
369
                break;
370
            default:
371
                return null;
372
                break;
373
        }
374
    }
375
}