TorrentListUtils   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 158
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A sumArrayField() 0 8 2
A getArrayField() 0 8 2
A filterTorrents() 0 12 3
A buildTableData() 0 24 3
A printTorrentsTable() 0 22 1
A getObsoleteTorrents() 0 20 4
A detectObsoleteTorrent() 0 17 4
A getCoveredTorrent() 0 13 3
A isFilesContainsAllOtherFiles() 0 4 1
A getFilesArray() 0 6 1
1
<?php
2
3
namespace Popstas\Transmission\Console\Helpers;
4
5
use Martial\Transmission\API\Argument\Torrent;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
class TorrentListUtils
9
{
10
    public static function sumArrayField(array $rows, $fieldKey)
11
    {
12
        $sum = 0;
13
        foreach ($rows as $row) {
14
            $sum += $row[$fieldKey];
15
        }
16
        return $sum;
17
    }
18
19
    public static function getArrayField(array $rows, $fieldKey)
20
    {
21
        $fields = [];
22
        foreach ($rows as $torrent) {
23
            $fields[] = $torrent[$fieldKey];
24
        }
25
        return $fields;
26
    }
27
28
    /**
29
     * @param array $torrentList
30
     *
31
     * @param array $filters
32
     * filters[
33
     * - age
34
     * - age_min
35
     * - age_max
36
     * ]
37
     *
38
     * @return array
39
     */
40
    public static function filterTorrents(array $torrentList, array $filters)
41
    {
42
        if (isset($filters['name'])) {
43
            $filters[Torrent\Get::NAME] = ['type' => 'regex', 'value' => $filters['name']];
44
        }
45
        if (isset($filters['age'])) {
46
            $filters['age'] = ['type' => 'numeric', 'value' => $filters['age']];
47
        }
48
49
        $torrentList = TableUtils::filterRows($torrentList, $filters);
50
        return $torrentList;
51
    }
52
53
    public static function buildTableData(array $torrentList)
54
    {
55
        $headers = ['Name', 'Id', 'Age', 'Size', 'Uploaded', 'Per day'];
56
        $rows = [];
57
58
        foreach ($torrentList as $torrent) {
59
            $age = TorrentUtils::getTorrentAgeInDays($torrent);
60
            $perDay = $age ? TorrentUtils::getSizeInGb($torrent[Torrent\Get::UPLOAD_EVER] / $age) : 0;
61
62
            $rows[] = [
63
                $torrent[Torrent\Get::NAME],
64
                $torrent[Torrent\Get::ID],
65
                $age,
66
                TorrentUtils::getSizeInGb($torrent[Torrent\Get::TOTAL_SIZE]),
67
                TorrentUtils::getSizeInGb($torrent[Torrent\Get::UPLOAD_EVER]),
68
                $perDay,
69
            ];
70
        }
71
72
        return [
73
            'headers' => $headers,
74
            'rows' => $rows,
75
        ];
76
    }
77
78
    public static function printTorrentsTable(
79
        array $torrentList,
80
        OutputInterface $output,
81
        $sortColumnNumber = 1,
82
        $limit = 0
83
    ) {
84
        $data = self::buildTableData($torrentList);
85
86
        $data['rows'] = TableUtils::sortRowsByColumnNumber($data['rows'], $sortColumnNumber);
87
        $data['rows'] = TableUtils::limitRows($data['rows'], $limit);
88
89
        $data['totals'] = [
90
            'Total: ' . count($data['rows']),
91
            '',
92
            '',
93
            self::sumArrayField($data['rows'], 3),
94
            self::sumArrayField($data['rows'], 4),
95
            ''
96
        ];
97
98
        TableUtils::printTable($data, $output);
99
    }
100
101
    public static function getObsoleteTorrents(array $torrentList)
102
    {
103
        $sameNamesAndDirectory = [];
104
        $obsolete = [];
105
106
        foreach ($torrentList as $torrent) {
107
            $key = $torrent[Torrent\Get::NAME] . '_' . $torrent[Torrent\Get::DOWNLOAD_DIR];
108
            $sameNamesAndDirectory[$key][$torrent[Torrent\Get::ID]] = $torrent;
109
        }
110
111
        foreach ($sameNamesAndDirectory as $key => $torrents) {
112
            if (count($torrents) < 2) {
113
                continue;
114
            }
115
116
            $obsolete = array_merge($obsolete, self::detectObsoleteTorrent($torrents));
117
        }
118
119
        return $obsolete;
120
    }
121
122
    private static function detectObsoleteTorrent($torrents)
123
    {
124
        $all = [];
125
        $obsoleteTorrents = [];
126
127
        foreach ($torrents as $torrentNotInList) {
128
            foreach ($all as $torrentInList) {
129
                $obsolete = self::getCoveredTorrent($torrentInList, $torrentNotInList);
130
                if ($obsolete) {
131
                    $obsoleteTorrents[] = $obsolete;
132
                }
133
            }
134
            $all[] = $torrentNotInList;
135
        }
136
137
        return $obsoleteTorrents;
138
    }
139
140
    private static function getCoveredTorrent($torrent1, $torrent2)
141
    {
142
        $files1 = self::getFilesArray($torrent1);
143
        $files2 = self::getFilesArray($torrent2);
144
145
        if (self::isFilesContainsAllOtherFiles($files1, $files2)) {
146
            return $torrent2;
147
        } elseif (self::isFilesContainsAllOtherFiles($files2, $files1)) {
148
            return $torrent1;
149
        }
150
151
        return false;
152
    }
153
154
    private static function isFilesContainsAllOtherFiles($files, $otherFiles)
155
    {
156
        return empty(array_diff($otherFiles, $files));
157
    }
158
159
    private static function getFilesArray($torrent)
160
    {
161
        return array_map(function ($file) {
162
            return $file['length'] . '_' . $file['name'];
163
        }, $torrent[Torrent\Get::FILES]);
164
    }
165
}
166