Completed
Push — master ( df650f...2515f9 )
by Stanislav
07:24
created

TransmissionClient::waitForTransmission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Popstas\Transmission\Console;
4
5
use Martial\Transmission\API;
6
use Martial\Transmission\API\Argument\Torrent;
7
8
class TransmissionClient
9
{
10
    private $api;
11
    private $sessionId;
12
13
    public function __construct(API\TransmissionAPI $api)
14
    {
15
        $this->api = $api;
16
    }
17
18
    public function createSession()
19
    {
20
        $this->sessionId = $this->getSessionId($this->sessionId);
21
    }
22
23
    private function getSessionId($sessionId)
24
    {
25
        try {
26
            $this->api->sessionGet($sessionId);
27
        } catch (API\CSRFException $e) {
28
            // The session has been reinitialized. Fetch the new session ID with the method getSessionId().
29
            $sessionId = $e->getSessionId();
30
        }
31
32
        return $sessionId;
33
    }
34
35
    public function getTorrentData(array $ids = [], $fields = [])
36
    {
37
        if (empty($fields)) {
38
            $fields = [
39
                Torrent\Get::ID,
40
                Torrent\Get::NAME,
41
                Torrent\Get::TOTAL_SIZE,
42
                Torrent\Get::DOWNLOAD_DIR,
43
                Torrent\Get::UPLOAD_EVER,
44
                Torrent\Get::DOWNLOAD_EVER,
45
                Torrent\Get::DONE_DATE,
46
                Torrent\Get::ADDED_DATE,
47
            ];
48
        }
49
50
        $cleanedIds = array_map(function ($torrentId) {
51
            return (int)$torrentId;
52
        }, $ids);
53
54
        $this->createSession();
55
        $torrentList = $this->api->torrentGet($this->sessionId, $cleanedIds, $fields);
56
57
        return $torrentList;
58
    }
59
60
    public function addTorrent($torrentFile, $downloadDir = null)
61
    {
62
        $arguments = [];
63
        if (is_file($torrentFile)) {
64
            $arguments[Torrent\Add::METAINFO] = base64_encode(file_get_contents($torrentFile));
65
        } else {
66
            $arguments[Torrent\Add::FILENAME] = $torrentFile;
67
        }
68
69
        if (!is_null($downloadDir)) {
70
            $arguments[API\Argument\Session\Accessor::DOWNLOAD_DIR] = $downloadDir;
71
        }
72
73
        $this->createSession();
74
        return $this->api->torrentAdd($this->sessionId, $arguments);
75
    }
76
77
    /**
78
     * @param array $torrentList Array of Torrent data or torrent_ids
79
     * @param bool $deleteLocalData
80
     * @return bool
81
     */
82
    public function removeTorrents(array $torrentList, $deleteLocalData = false)
83
    {
84
        if (empty($torrentList)) {
85
            return false;
86
        }
87
88
        $torrentIds = [];
89
90
        foreach ($torrentList as $torrent) {
91
            $torrentIds[] = $torrent[Torrent\Get::ID];
92
        }
93
94
        $this->createSession();
95
        $this->api->torrentRemove($this->sessionId, $torrentIds, $deleteLocalData);
96
97
        return true;
98
    }
99
100
    public function waitForTransmission($int)
101
    {
102
        sleep($int);
103
        $this->createSession();
104
    }
105
}
106