Completed
Push — master ( 27dee6...22d68a )
by Stanislav
05:48
created

TransmissionClient::getTorrentStatusName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace Popstas\Transmission\Console;
4
5
use Martial\Transmission\API;
6
use Martial\Transmission\API\Argument\Session;
7
use Martial\Transmission\API\Argument\Torrent;
8
use Martial\Transmission\API\TorrentIdList;
9
use Martial\Transmission\API\DuplicateTorrentException;
10
11
class TransmissionClient
12
{
13
    private $api;
14
    private $sessionId;
15
16
    public function __construct(API\TransmissionAPI $api)
17
    {
18
        $this->api = $api;
19
    }
20
21
    private function getSessionId()
22
    {
23
        try {
24
            $this->api->sessionGet($this->sessionId);
25
        } catch (API\CSRFException $e) {
26
            // The session has been reinitialized. Fetch the new session ID with the method getSessionId().
27
            $this->sessionId = $e->getSessionId();
28
        }
29
        return $this->sessionId;
30
    }
31
32
    public function getTorrentData(array $ids = [], $fields = [])
33
    {
34
        if (empty($fields)) {
35
            $fields = [
36
                Torrent\Get::ID,
37
                Torrent\Get::NAME,
38
                Torrent\Get::TOTAL_SIZE,
39
                Torrent\Get::DOWNLOAD_DIR,
40
                Torrent\Get::UPLOAD_EVER,
41
                Torrent\Get::DOWNLOAD_EVER,
42
                Torrent\Get::DONE_DATE,
43
                Torrent\Get::ADDED_DATE,
44
                Torrent\Get::FILES,
45
                Torrent\Get::STATUS,
46
            ];
47
        }
48
49
        $cleanedIds = array_map(function ($torrentId) {
50
            return (int)$torrentId;
51
        }, $ids);
52
53
        $this->getSessionId();
54
55
        $torrentList = $this->api->torrentGet($this->sessionId, new TorrentIdList($cleanedIds), $fields);
56
57
        if (in_array(Torrent\Get::NAME, $fields)) {
58
            foreach ($torrentList as $ind => $torrent) {
59
                $torrentList[$ind][Torrent\Get::NAME] = str_replace("'", "\\'", $torrent[Torrent\Get::NAME]);
60
            }
61
        }
62
63
        return $torrentList;
64
    }
65
66
    public static function getTorrentStatusName($statusId)
67
    {
68
        $statuses = [
69
            0 => 'stopped',
70
            1 => 'check_wait',
71
            2 => 'check',
72
            3 => 'download_wait',
73
            4 => 'download',
74
            5 => 'seed_wait',
75
            6 => 'seed',
76
        ];
77
        return $statuses[$statusId];
78
    }
79
80
    public function addTorrent($torrentFile, $downloadDir = null)
81
    {
82
        $arguments = [];
83
        if (is_file($torrentFile)) {
84
            $arguments[Torrent\Add::METAINFO] = base64_encode(file_get_contents($torrentFile));
85
        } else {
86
            $arguments[Torrent\Add::FILENAME] = $torrentFile;
87
        }
88
89
        if (!is_null($downloadDir)) {
90
            $arguments[API\Argument\Session\Accessor::DOWNLOAD_DIR] = $downloadDir;
91
        }
92
93
        $this->getSessionId();
94
95
        try {
96
            $response = $this->api->torrentAdd($this->sessionId, $arguments);
97
        } catch (DuplicateTorrentException $e) {
98
            return [
99
                'id'         => $e->getTorrentId(),
100
                'name'       => $e->getTorrentName(),
101
                'hashString' => $e->getTorrentHashString(),
102
                'duplicate'  => true
103
            ];
104
        }
105
        return $response;
106
    }
107
108
    /**
109
     * @param array $torrentList Array of Torrent data or torrent_ids
110
     * @param bool $deleteLocalData
111
     * @return bool
112
     */
113
    public function removeTorrents(array $torrentList, $deleteLocalData = false)
114
    {
115
        if (empty($torrentList)) {
116
            return false;
117
        }
118
119
        $torrentIds = [];
120
121
        foreach ($torrentList as $torrent) {
122
            $torrentIds[] = $torrent[Torrent\Get::ID];
123
        }
124
125
        $this->getSessionId();
126
        $this->api->torrentRemove($this->sessionId, new TorrentIdList($torrentIds), $deleteLocalData);
127
128
        return true;
129
    }
130
131
    public function waitForTransmission($int)
132
    {
133
        sleep($int);
134
        $this->getSessionId();
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getDownloadDir()
141
    {
142
        $this->getSessionId();
143
        $session = $this->api->sessionGet($this->sessionId);
144
        return $session[Session\Get::DOWNLOAD_DIR];
145
    }
146
147
    public function getFreeSpace($downloadDir = null)
148
    {
149
        if (is_null($downloadDir)) {
150
            $downloadDir = $this->getDownloadDir();
151
        }
152
        $this->getSessionId();
153
        $freeSpace = $this->api->freeSpace($this->sessionId, $downloadDir);
154
        return $freeSpace['size-bytes'];
155
    }
156
}
157