Passed
Push — main ( 33c779...46c527 )
by Daniel
04:17
created

AudioFileRetriever::retrieve()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 69
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 41
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 69
ccs 0
cts 28
cp 0
crap 30
rs 8.9528

How to fix   Long Method   

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
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Catalog\Manage\Update;
6
7
use Generator;
8
use getID3;
9
use Psr\Log\LoggerInterface;
10
use Uxmp\Core\Component\Tag\Container\AudioFile;
11
use Uxmp\Core\Component\Tag\Container\AudioFileInterface;
12
use Uxmp\Core\Component\Tag\Extractor\ExtractorDeterminatorInterface;
13
14
final class AudioFileRetriever implements AudioFileRetrieverInterface
15
{
16
    /** @var string[] */
17
    private const SUPPORTED_FORMATS = [
18
        'mp3',
19
        'flac',
20
        'ogg',
21
        'wav',
22
    ];
23
24
    public function __construct(
25
        private readonly getID3 $id3Analyzer,
26
        private readonly ExtractorDeterminatorInterface $extractorDeterminator,
27
        private readonly LoggerInterface $logger,
28
    ) {
29
    }
30
31
    /**
32
     * @param iterable<string> $fileList
33
     *
34
     * @return Generator<AudioFileInterface|null>
35
     */
36
    public function retrieve(
37
        iterable $fileList
38
    ): Generator {
39
        foreach ($fileList as $filename) {
40
            $this->logger->info(
41
                sprintf(
42
                    'Reading file: %s',
43
                    $filename
44
                )
45
            );
46
47
            $analysisResult = $this->id3Analyzer->analyze($filename);
48
49
            $audioFile = (new AudioFile())
50
                ->setFilename($filename)
51
                ->setFileSize($analysisResult['filesize'] ?? 0)
52
                ->setLength((int) round($analysisResult['playtime_seconds']))
53
            ;
54
55
            $fileFormat = $analysisResult['fileformat'] ?? '';
56
57
            if (!in_array(
58
                $fileFormat,
59
                self::SUPPORTED_FORMATS,
60
                true
61
            )) {
62
                $this->logger->error(
63
                    sprintf(
64
                        '`%s`: Unknown fileformat `%s`',
65
                        $filename,
66
                        $fileFormat
67
                    )
68
                );
69
70
                yield null;
71
                continue;
72
            }
73
74
            $extractor = $this->extractorDeterminator->determine($analysisResult['tags']);
75
            if ($extractor === null) {
76
                $this->logger->error(
77
                    sprintf(
78
                        '`%s`: Meta-Tag extraction failed',
79
                        $filename,
80
                    )
81
                );
82
83
                yield null;
84
                continue;
85
            }
86
87
            $extractor->extract(
88
                $analysisResult,
89
                $audioFile
90
            );
91
92
            if (!$audioFile->isValid()) {
93
                $this->logger->error(
94
                    sprintf(
95
                        'Skipped `%s`: Invalid Meta-Tags',
96
                        $filename,
97
                    )
98
                );
99
100
                yield null;
101
                continue;
102
            }
103
104
            yield $audioFile;
105
        }
106
    }
107
}
108