Completed
Push — master ( 9c8831...4e9e3a )
by Stanislav
05:36
created

TransmissionClient::addTorrent()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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