|
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 |
|
|
|
|
|
|
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
|
|
|
|