Completed
Push — master ( 71008e...41e944 )
by Stanislav
02:29
created

TorrentUtils::getSizeInGb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Popstas\Transmission\Console\Helpers;
4
5
use Martial\Transmission\API\Argument\Torrent;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class TorrentUtils
10
{
11
    public static function getTorrentsSize(array $torrentList, $fieldName = Torrent\Get::TOTAL_SIZE)
12
    {
13
        $torrentSize = 0;
14
        foreach ($torrentList as $torrent) {
15
            $torrentSize += $torrent[$fieldName];
16
        }
17
        return $torrentSize;
18
    }
19
20
    public static function getTorrentsField(array $torrentList, $fieldName)
21
    {
22
        $fields = [];
23
        foreach ($torrentList as $torrent) {
24
            $fields[] = $torrent[$fieldName];
25
        }
26
        return $fields;
27
    }
28
29
    /**
30
     * @param array $torrentList
31
     *
32
     * @param array $filters
33
     * filters[
34
     * - age
35
     * - age_min
36
     * - age_max
37
     * ]
38
     *
39
     * @return array
40
     */
41
    public static function filterTorrents(array $torrentList, array $filters)
42
    {
43
        $filters += ['age_min' => 0, 'age_max' => 99999, 'name' => ''];
44
        $filters['name'] = str_replace(['/', '.', '*'], ['\/', '\.', '.*?'], $filters['name']);
45
46
        if (isset($filters['age'])) {
47
            $filters = self::parseAgeFilter($filters['age']) + $filters;
48
        }
49
50
        return array_filter($torrentList, function ($torrent) use ($filters) {
51
            $age = self::getTorrentAgeInDays($torrent);
52
            if ($age < $filters['age_min'] || $age > $filters['age_max']) {
53
                return false;
54
            }
55
            if (isset($torrent[Torrent\Get::NAME])
56
                && !preg_match('/' . $filters['name'] . '/i', $torrent[Torrent\Get::NAME])
57
            ) {
58
                return false;
59
            }
60
            return true;
61
        });
62
    }
63
64
    private static function parseAgeFilter($age)
65
    {
66
        $filters = [];
67
        preg_match_all('/([<>])\s?(\d+)/', $age, $results, PREG_SET_ORDER);
68
        if ($results) {
69
            foreach ($results as $result) {
70
                $ageOperator = $result[1];
71
                $ageValue = $result[2];
72
                if ($ageOperator == '<') {
73
                    $filters['age_max'] = $ageValue - 1;
74
                }
75
                if ($ageOperator == '>') {
76
                    $filters['age_min'] = $ageValue + 1;
77
                }
78
            }
79
        }
80
        return $filters;
81
    }
82
83
    /**
84
     * @param $torrent
85
     * @return int seconds from torrent finish download
86
     */
87
    public static function getTorrentAge($torrent)
88
    {
89
        $date = isset($torrent[Torrent\Get::DONE_DATE]) && $torrent[Torrent\Get::DONE_DATE] ?
90
            $torrent[Torrent\Get::DONE_DATE] :
91
            (isset($torrent[Torrent\Get::ADDED_DATE]) ? $torrent[Torrent\Get::ADDED_DATE] : 0);
92
        return $date ? time() - $date : 0;
93
    }
94
95
    public static function getTorrentAgeInDays($torrent)
96
    {
97
        return round(self::getTorrentAge($torrent) / 86400);
98
    }
99
100
    public static function printTorrentsTable(array $torrentList, OutputInterface $output)
101
    {
102
        $table = new Table($output);
103
        $table->setHeaders(['Name', 'Id', 'Size']);
104
105
        foreach ($torrentList as $torrent) {
106
            $table->addRow([
107
                $torrent[Torrent\Get::NAME],
108
                $torrent[Torrent\Get::ID],
109
                TorrentUtils::getSizeInGb($torrent[Torrent\Get::TOTAL_SIZE]),
110
            ]);
111
        }
112
113
        $table->render();
114
    }
115
116
    public static function getObsoleteTorrents(array $torrentList)
117
    {
118
        $all = [];
119
        $obsolete = [];
120
121
        foreach ($torrentList as $torrent) {
122
            $name = $torrent[Torrent\Get::NAME];
123
            if (!isset($all[$name])) {
124
                $all[$name] = $torrent;
125
                continue;
126
            }
127
128
            $obs = self::detectObsoleteTorrent($all[$name], $torrent);
129
            if ($obs) {
130
                $obsolete[] = $obs;
131
                if ($obs[Torrent\Get::ID] !== $torrent[Torrent\Get::ID]) {
132
                    $all[$name] = $torrent;
133
                }
134
            }
135
        }
136
137
        return $obsolete;
138
    }
139
140
    public static function getSizeInGb($sizeInBytes, $round = 2)
141
    {
142
        return round($sizeInBytes / 1024 / 1024 / 1024, $round);
143
    }
144
145
    private static function detectObsoleteTorrent($torrentInList, $torrentNotInList)
146
    {
147
        if ($torrentInList[Torrent\Get::DOWNLOAD_DIR] !== $torrentNotInList[Torrent\Get::DOWNLOAD_DIR]) {
148
            return false;
149
        }
150
151
        return $torrentInList[Torrent\Get::TOTAL_SIZE] < $torrentNotInList[Torrent\Get::TOTAL_SIZE] ?
152
            $torrentInList : $torrentNotInList;
153
    }
154
}
155