1 | <?php |
||
2 | |||
3 | namespace Backend\Modules\MediaGalleries\Console; |
||
4 | |||
5 | use Backend\Modules\MediaGalleries\Domain\MediaGallery\Command\DeleteMediaGallery; |
||
6 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
||
7 | use Symfony\Component\Console\Input\InputInterface; |
||
8 | use Symfony\Component\Console\Input\InputOption; |
||
9 | use Symfony\Component\Console\Output\OutputInterface; |
||
10 | |||
11 | /** |
||
12 | * Delete media galleries Console Command |
||
13 | * Example: "bin/console media_galleries:delete:galleries", will delete all galleries |
||
14 | * Example: "bin/console media_galleries:delete:galleries --delete-media-items", will delete all galleries and all MediaItem entities |
||
15 | */ |
||
16 | class MediaGalleryDeleteAllCommand extends ContainerAwareCommand |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
17 | { |
||
18 | /** |
||
19 | * The MediaGroupMediaItem connections are always deleted, |
||
20 | * but should we delete the source MediaItem items as well? |
||
21 | * |
||
22 | * @var bool |
||
23 | */ |
||
24 | protected $deleteMediaItems = false; |
||
25 | |||
26 | protected function configure(): void |
||
27 | { |
||
28 | $this |
||
29 | ->setName('media_galleries:delete:galleries') |
||
30 | ->setDescription('Delete media galleries.') |
||
31 | ->addOption( |
||
32 | 'delete-media-items', |
||
33 | null, |
||
34 | InputOption::VALUE_NONE, |
||
35 | 'If set, all connected MediaItems will be deleted as well from the library.' |
||
36 | ); |
||
37 | } |
||
38 | |||
39 | protected function execute(InputInterface $input, OutputInterface $output): void |
||
40 | { |
||
41 | $output->writeln('<info>-Started deleting media galleries.</info>'); |
||
42 | |||
43 | $this->checkOptions($input); |
||
44 | $this->deleteMediaGalleries(); |
||
45 | |||
46 | $output->writeln('<info>-Finished deleting media galleries.</info>'); |
||
47 | } |
||
48 | |||
49 | private function checkOptions(InputInterface $input): void |
||
50 | { |
||
51 | if ($input->getOption('delete-media-items')) { |
||
52 | $this->deleteMediaItems = true; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | private function deleteMediaGalleries(): void |
||
57 | { |
||
58 | /** @var array $mediaGalleries */ |
||
59 | $mediaGalleries = $this->getContainer()->get('media_galleries.repository.gallery')->findAll(); |
||
60 | |||
61 | if (empty($mediaGalleries)) { |
||
62 | return; |
||
63 | } |
||
64 | |||
65 | // Loop all media galleries |
||
66 | foreach ($mediaGalleries as $mediaGallery) { |
||
67 | $this->getContainer()->get('command_bus')->handle( |
||
68 | new DeleteMediaGallery($mediaGallery), |
||
69 | $this->deleteMediaItems |
||
70 | ); |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 |