Completed
Push — master ( e1baba...a0d507 )
by Stanislav
02:30
created

TransmissionClient   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 19
Bugs 6 Features 6
Metric Value
wmc 15
c 19
b 6
f 6
lcom 1
cbo 2
dl 0
loc 119
rs 10

8 Methods

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