Passed
Push — main ( 7dcca1...e70bce )
by Daniel
04:22
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 DateTimeInterface;
8
use Generator;
9
use getID3;
10
use Psr\Log\LoggerInterface;
11
use Uxmp\Core\Component\Tag\Container\AudioFile;
12
use Uxmp\Core\Component\Tag\Container\AudioFileInterface;
13
use Uxmp\Core\Component\Tag\Extractor\ExtractorDeterminatorInterface;
14
use Uxmp\Core\Lib\StringHelperInterface;
15
16
final readonly class AudioFileRetriever implements AudioFileRetrieverInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 16 at column 6
Loading history...
17
{
18
    /** @var string[] */
19
    private const SUPPORTED_FORMATS = [
20
        'mp3',
21
        'flac',
22
        'ogg',
23
        'wav',
24
    ];
25
26 5
    public function __construct(
27
        private getID3 $id3Analyzer,
28
        private ExtractorDeterminatorInterface $extractorDeterminator,
29
        private StringHelperInterface $stringHelper,
30
        private LoggerInterface $logger,
31
    ) {
32 5
    }
33
34
    /**
35
     * @param iterable<string> $fileList
36
     *
37
     * @return Generator<AudioFileInterface|null>
38
     */
39 5
    public function retrieve(
40
        iterable $fileList,
41
        ?DateTimeInterface $thresholdDate
42
    ): Generator {
43 5
        $thresholdTimestamp = $thresholdDate?->getTimestamp() ?? 0;
44
45 5
        foreach ($fileList as $filename) {
46 5
            $fileModificationTime = filectime($filename);
47
48 5
            if ($fileModificationTime > $thresholdTimestamp) {
49 4
                yield $this->build($filename);
50
            }
51
        }
52
    }
53
54
    /**
55
     * Builds an AudioFile for the given filename
56
     */
57 4
    public function build(string $filename): ?AudioFileInterface
58
    {
59 4
        $this->logger->info(
60 4
            sprintf(
61 4
                'Reading file: %s',
62 4
                $filename
63 4
            )
64 4
        );
65
66 4
        $analysisResult = $this->id3Analyzer->analyze($filename);
67
68 4
        $fileFormat = $analysisResult['fileformat'] ?? '';
69
70 4
        if (!in_array(
71 4
            $fileFormat,
72 4
            self::SUPPORTED_FORMATS,
73 4
            true
74 4
        )) {
75 1
            $this->logger->error(
76 1
                sprintf(
77 1
                    '`%s`: Unknown fileformat `%s`',
78 1
                    $filename,
79 1
                    $fileFormat
80 1
                )
81 1
            );
82
83 1
            return null;
84
        }
85
86 3
        $extractor = $this->extractorDeterminator->determine($analysisResult['tags']);
87 3
        if ($extractor === null) {
88 1
            $this->logger->error(
89 1
                sprintf(
90 1
                    '`%s`: Meta-Tag extraction failed',
91 1
                    $filename,
92 1
                )
93 1
            );
94
95 1
            return null;
96
        }
97
98 2
        $audioFile = (new AudioFile($this->stringHelper))
99 2
            ->setFilename($filename)
100 2
            ->setFileSize($analysisResult['filesize'] ?? 0)
101 2
            ->setLength((int) round($analysisResult['playtime_seconds']))
102 2
        ;
103
104 2
        $audioFile = $extractor->extract(
105 2
            $analysisResult,
106 2
            $audioFile
107 2
        );
108
109 2
        if (!$audioFile->isValid()) {
110 1
            $this->logger->error(
111 1
                sprintf(
112 1
                    '`%s`: Missing mandatory metadata',
113 1
                    $filename,
114 1
                )
115 1
            );
116
117 1
            return null;
118
        }
119
120 1
        return $audioFile;
121
    }
122
}
123