|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Uxmp\Core\Component\Catalog\Manage; |
|
6
|
|
|
|
|
7
|
|
|
use Ahc\Cli\IO\Interactor; |
|
8
|
|
|
use DateTime; |
|
9
|
|
|
use Uxmp\Core\Component\Catalog\Manage\Update\AudioFileRetrieverInterface; |
|
10
|
|
|
use Uxmp\Core\Component\Catalog\Manage\Update\RecursiveFileReaderInterface; |
|
11
|
|
|
use Uxmp\Core\Component\Catalog\Scanner\DiscCacheInterface; |
|
12
|
|
|
use Uxmp\Core\Component\Tag\Container\AudioFileInterface; |
|
13
|
|
|
use Uxmp\Core\Orm\Repository\CatalogRepositoryInterface; |
|
14
|
|
|
use Uxmp\Core\Orm\Repository\SongRepositoryInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Updates the media files of a catalog |
|
18
|
|
|
*/ |
|
19
|
|
|
final readonly class CatalogUpdater implements CatalogUpdaterInterface |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
3 |
|
public function __construct( |
|
22
|
|
|
private CatalogRepositoryInterface $catalogRepository, |
|
23
|
|
|
private SongRepositoryInterface $songRepository, |
|
24
|
|
|
private DiscCacheInterface $discCache, |
|
25
|
|
|
private RecursiveFileReaderInterface $recursiveFileReader, |
|
26
|
|
|
private AudioFileRetrieverInterface $audioFileRetriever, |
|
27
|
|
|
) { |
|
28
|
3 |
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
public function update(Interactor $io, int $catalogId): void |
|
31
|
|
|
{ |
|
32
|
3 |
|
$catalog = $this->catalogRepository->find($catalogId); |
|
33
|
3 |
|
if ($catalog === null) { |
|
34
|
1 |
|
$io->error( |
|
35
|
1 |
|
sprintf('Catalog `%d` not found', $catalogId), |
|
36
|
1 |
|
true |
|
37
|
1 |
|
); |
|
38
|
1 |
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
$directory = $catalog->getPath(); |
|
42
|
|
|
|
|
43
|
2 |
|
if (!is_dir($directory)) { |
|
44
|
1 |
|
$io->error( |
|
45
|
1 |
|
sprintf('The path `%s` is not accessible', $directory), |
|
46
|
1 |
|
true |
|
47
|
1 |
|
); |
|
48
|
1 |
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
$io->info( |
|
52
|
1 |
|
sprintf('Updating catalog from `%s`', $directory), |
|
53
|
1 |
|
true |
|
54
|
1 |
|
); |
|
55
|
|
|
|
|
56
|
1 |
|
$reader = $this->audioFileRetriever->retrieve( |
|
57
|
1 |
|
$this->recursiveFileReader->read($directory), |
|
58
|
1 |
|
$catalog->getLastUpdated() |
|
59
|
1 |
|
); |
|
60
|
|
|
|
|
61
|
|
|
// set the last-update date at the beginning |
|
62
|
1 |
|
$catalog->setLastUpdated(new DateTime()); |
|
63
|
|
|
|
|
64
|
1 |
|
foreach ($reader as $audioFile) { |
|
65
|
|
|
/** @var AudioFileInterface|null $audioFile */ |
|
66
|
1 |
|
if ($audioFile === null) { |
|
67
|
1 |
|
$io->error('.'); |
|
68
|
1 |
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
$io->info('.'); |
|
72
|
|
|
|
|
73
|
1 |
|
$song = $this->songRepository->findByMbId($audioFile->getMbid()); |
|
74
|
1 |
|
if ($song === null) { |
|
75
|
1 |
|
$song = $this->songRepository->prototype(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
$disc = $this->discCache->retrieve($catalog, $audioFile); |
|
79
|
|
|
|
|
80
|
1 |
|
$song |
|
81
|
1 |
|
->setDisc($disc) |
|
82
|
1 |
|
->setArtist($disc->getAlbum()->getArtist()) |
|
83
|
1 |
|
->setCatalog($catalog) |
|
84
|
1 |
|
; |
|
85
|
|
|
|
|
86
|
|
|
// apply the audio-file state onto the song |
|
87
|
1 |
|
$audioFile->apply($song); |
|
88
|
|
|
|
|
89
|
1 |
|
$this->songRepository->save($song); |
|
90
|
|
|
|
|
91
|
1 |
|
$disc->addSong($song); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$this->catalogRepository->save($catalog); |
|
95
|
|
|
|
|
96
|
1 |
|
$io->eol(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|