Passed
Push — master ( 25b802...040228 )
by Darko
10:52
created

UsenetDownloadService::downloadByMessageIDs()   C

Complexity

Conditions 13
Paths 25

Size

Total Lines 68
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 68
rs 6.6166
c 0
b 0
f 0
cc 13
nc 25
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Services\AdditionalProcessing;
4
5
use App\Services\AdditionalProcessing\Config\ProcessingConfiguration;
0 ignored issues
show
Bug introduced by
The type App\Services\AdditionalP...ProcessingConfiguration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Blacklight\NNTP;
7
use Exception;
8
use Illuminate\Support\Facades\Log;
9
10
/**
11
 * Service for downloading content from Usenet via NNTP.
12
 * Handles message ID downloading with error handling and group availability detection.
13
 */
14
class UsenetDownloadService
15
{
16
    private NNTP $nntp;
17
18
    public function __construct(
19
        private readonly ProcessingConfiguration $config
20
    ) {
21
        $this->nntp = new NNTP();
22
    }
23
24
    /**
25
     * Download binary content from usenet using message IDs.
26
     *
27
     * @param array|string $messageIDs Single or array of message IDs
28
     * @param string $groupName Group name for logging
29
     * @param int|null $releaseId Release ID for logging
30
     * @return array{success: bool, data: string|null, groupUnavailable: bool, error: string|null}
31
     * @throws Exception
32
     */
33
    public function downloadByMessageIDs(
34
        array|string $messageIDs,
35
        string $groupName = '',
36
        ?int $releaseId = null
37
    ): array {
38
        $result = [
39
            'success' => false,
40
            'data' => null,
41
            'groupUnavailable' => false,
42
            'error' => null,
43
        ];
44
45
        if (empty($messageIDs)) {
46
            $result['error'] = 'No message IDs provided';
47
            return $result;
48
        }
49
50
        // Ensure array format
51
        if (is_string($messageIDs)) {
0 ignored issues
show
introduced by
The condition is_string($messageIDs) is always false.
Loading history...
52
            $messageIDs = [$messageIDs];
53
        }
54
55
        if ($this->config->debugMode) {
56
            Log::debug('Attempting NNTP fetch', [
57
                'release_id' => $releaseId,
58
                'message_ids' => $messageIDs,
59
                'group' => $groupName,
60
            ]);
61
        }
62
63
        $binary = $this->nntp->getMessagesByMessageID($messageIDs, $this->config->alternateNNTP);
64
65
        // Handle non-string or empty response as failure
66
        if (! is_string($binary) || $binary === '') {
67
            $errorMessage = null;
68
69
            if (is_object($binary) && method_exists($binary, 'getMessage')) {
70
                $errorMessage = $binary->getMessage();
71
72
                // Check for group unavailability
73
                if (stripos($errorMessage, 'No such news group') !== false
74
                    || stripos($errorMessage, 'Group not found') !== false
75
                ) {
76
                    $result['groupUnavailable'] = true;
77
                    $result['error'] = 'Group unavailable: '.$errorMessage;
78
                    return $result;
79
                }
80
            }
81
82
            if ($this->config->debugMode) {
83
                Log::debug('NNTP fetch failed', [
84
                    'release_id' => $releaseId,
85
                    'message_ids' => $messageIDs,
86
                    'group' => $groupName,
87
                    'error_object' => is_object($binary) ? get_class($binary) : null,
88
                    'error_message' => $errorMessage,
89
                    'raw_type' => gettype($binary),
90
                    'length' => is_string($binary) ? strlen($binary) : 0,
91
                ]);
92
            }
93
94
            $result['error'] = $errorMessage ?? 'Download failed';
95
            return $result;
96
        }
97
98
        $result['success'] = true;
99
        $result['data'] = $binary;
100
        return $result;
101
    }
102
103
    /**
104
     * Download sample video content.
105
     */
106
    public function downloadSample(
107
        array $messageIDs,
108
        string $groupName = '',
109
        ?int $releaseId = null
110
    ): array {
111
        return $this->downloadByMessageIDs($messageIDs, $groupName, $releaseId);
112
    }
113
114
    /**
115
     * Download media info video content.
116
     */
117
    public function downloadMediaInfo(
118
        string|array $messageID,
119
        string $groupName = '',
120
        ?int $releaseId = null
121
    ): array {
122
        return $this->downloadByMessageIDs($messageID, $groupName, $releaseId);
123
    }
124
125
    /**
126
     * Download audio content.
127
     */
128
    public function downloadAudio(
129
        string|array $messageID,
130
        string $groupName = '',
131
        ?int $releaseId = null
132
    ): array {
133
        return $this->downloadByMessageIDs($messageID, $groupName, $releaseId);
134
    }
135
136
    /**
137
     * Download JPG content.
138
     */
139
    public function downloadJPG(
140
        array $messageIDs,
141
        string $groupName = '',
142
        ?int $releaseId = null
143
    ): array {
144
        return $this->downloadByMessageIDs($messageIDs, $groupName, $releaseId);
145
    }
146
147
    /**
148
     * Download compressed file content (RAR, ZIP, etc.).
149
     *
150
     * @param array $messageIDs Message IDs to download
151
     * @param string $groupName Group name for logging
152
     * @param int|null $releaseId Release ID for logging
153
     * @param string|null $fileTitle File title for logging
154
     * @return array{success: bool, data: string|null, groupUnavailable: bool, error: string|null}
155
     * @throws Exception
156
     */
157
    public function downloadCompressedFile(
158
        array $messageIDs,
159
        string $groupName = '',
160
        ?int $releaseId = null,
161
        ?string $fileTitle = null
162
    ): array {
163
        if ($this->config->debugMode) {
164
            Log::debug('Attempting compressed fetch', [
165
                'release_id' => $releaseId,
166
                'file_title' => $fileTitle,
167
                'message_ids' => $messageIDs,
168
                'group' => $groupName,
169
            ]);
170
        }
171
172
        $result = $this->downloadByMessageIDs($messageIDs, $groupName, $releaseId);
173
174
        if (! $result['success'] && $this->config->debugMode) {
175
            Log::debug('Compressed fetch failed', [
176
                'release_id' => $releaseId,
177
                'file_title' => $fileTitle,
178
                'message_ids' => $messageIDs,
179
                'group' => $groupName,
180
                'error' => $result['error'],
181
            ]);
182
        }
183
184
        return $result;
185
    }
186
187
    /**
188
     * Get the NNTP client instance.
189
     */
190
    public function getNNTP(): NNTP
191
    {
192
        return $this->nntp;
193
    }
194
195
    /**
196
     * Check if the minimum content size requirement is met.
197
     */
198
    public function meetsMinimumSize(string $data, int $minimumBytes = 40): bool
199
    {
200
        return strlen($data) > $minimumBytes;
201
    }
202
}
203
204