Passed
Push — main ( e933de...5ef56a )
by Daniel
03:35
created

AudioFileRetriever::build()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 64
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 49
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 37
c 0
b 0
f 0
dl 0
loc 64
ccs 49
cts 49
cp 1
rs 9.328
cc 4
nc 4
nop 1
crap 4

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 4
    public function __construct(
25
        private readonly getID3 $id3Analyzer,
26
        private readonly ExtractorDeterminatorInterface $extractorDeterminator,
27
        private readonly LoggerInterface $logger,
28
    ) {
29 4
    }
30
31
    /**
32
     * @param iterable<string> $fileList
33
     *
34
     * @return Generator<AudioFileInterface|null>
35
     */
36 4
    public function retrieve(
37
        iterable $fileList
38
    ): Generator {
39 4
        foreach ($fileList as $filename) {
40 4
            yield $this->build($filename);
41
        }
42
    }
43
44
    /**
45
     * Builds an AudioFile for the given filename
46
     */
47 4
    public function build(string $filename): ?AudioFileInterface
48
    {
49 4
        $this->logger->info(
50 4
            sprintf(
51 4
                'Reading file: %s',
52 4
                $filename
53 4
            )
54 4
        );
55
56 4
        $analysisResult = $this->id3Analyzer->analyze($filename);
57
58 4
        $fileFormat = $analysisResult['fileformat'] ?? '';
59
60 4
        if (!in_array(
61 4
            $fileFormat,
62 4
            self::SUPPORTED_FORMATS,
63 4
            true
64 4
        )) {
65 1
            $this->logger->error(
66 1
                sprintf(
67 1
                    '`%s`: Unknown fileformat `%s`',
68 1
                    $filename,
69 1
                    $fileFormat
70 1
                )
71 1
            );
72
73 1
            return null;
74
        }
75
76 3
        $extractor = $this->extractorDeterminator->determine($analysisResult['tags']);
77 3
        if ($extractor === null) {
78 1
            $this->logger->error(
79 1
                sprintf(
80 1
                    '`%s`: Meta-Tag extraction failed',
81 1
                    $filename,
82 1
                )
83 1
            );
84
85 1
            return null;
86
        }
87
88 2
        $audioFile = (new AudioFile())
89 2
            ->setFilename($filename)
90 2
            ->setFileSize($analysisResult['filesize'] ?? 0)
91 2
            ->setLength((int) round($analysisResult['playtime_seconds']))
92 2
        ;
93
94 2
        $audioFile = $extractor->extract(
95 2
            $analysisResult,
96 2
            $audioFile
97 2
        );
98
99 2
        if (!$audioFile->isValid()) {
100 1
            $this->logger->error(
101 1
                sprintf(
102 1
                    '`%s`: Missing mandatory metadata',
103 1
                    $filename,
104 1
                )
105 1
            );
106
107 1
            return null;
108
        }
109
110 1
        return $audioFile;
111
    }
112
}
113