Completed
Push — master ( b80b9c...4559ee )
by Stanislav
02:17
created

TorrentUtils::getTorrentsField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
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 days from torrent finish download
86
     */
87
    public static function getTorrentAgeInDays($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 ? round((time() - $date) / 86400) : 0;
93
    }
94
95
    public static function printTorrentsTable(array $torrentList, OutputInterface $output)
96
    {
97
        $table = new Table($output);
98
        $table->setHeaders(['Name', 'Id', 'Size']);
99
100
        foreach ($torrentList as $torrent) {
101
            $table->addRow([
102
                $torrent[Torrent\Get::NAME],
103
                $torrent[Torrent\Get::ID],
104
                round($torrent[Torrent\Get::TOTAL_SIZE] / 1024 / 1000 / 1000, 2),
105
            ]);
106
        }
107
108
        $table->render();
109
    }
110
111
    public static function getObsoleteTorrents(array $torrentList)
112
    {
113
        $all = [];
114
        $obsolete = [];
115
116
        foreach ($torrentList as $torrent) {
117
            $name = $torrent[Torrent\Get::NAME];
118
            if (!isset($all[$name])) {
119
                $all[$name] = $torrent;
120
                continue;
121
            }
122
123
            $obs = self::detectObsoleteTorrent($all[$name], $torrent);
124
            if ($obs) {
125
                $obsolete[] = $obs;
126
                if ($obs[Torrent\Get::ID] !== $torrent[Torrent\Get::ID]) {
127
                    $all[$name] = $torrent;
128
                }
129
            }
130
        }
131
132
        return $obsolete;
133
    }
134
135
    private static function detectObsoleteTorrent($torrentInList, $torrentNotInList)
136
    {
137
        if ($torrentInList[Torrent\Get::DOWNLOAD_DIR] !== $torrentNotInList[Torrent\Get::DOWNLOAD_DIR]) {
138
            return false;
139
        }
140
141
        return $torrentInList[Torrent\Get::TOTAL_SIZE] < $torrentNotInList[Torrent\Get::TOTAL_SIZE] ?
142
            $torrentInList : $torrentNotInList;
143
    }
144
}
145