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

AudioFileRetriever   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 91
ccs 0
cts 29
cp 0
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B retrieve() 0 69 5
A __construct() 0 5 1
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