1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Kunstmaan\MediaBundle\Helper\MediaManager; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @final since 5.1 |
16
|
|
|
* NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages |
17
|
|
|
*/ |
18
|
|
|
class CleanDeletedMediaCommand extends ContainerAwareCommand |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var EntityManager |
22
|
|
|
*/ |
23
|
|
|
private $em; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var MediaManager |
27
|
|
|
*/ |
28
|
|
|
private $mediaManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param EntityManagerInterface|null $em |
32
|
|
|
* @param MediaManager|null $mediaManager |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function __construct(/* EntityManagerInterface */ $em = null, /* MediaManager */ $mediaManager = null) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
parent::__construct(); |
37
|
|
|
|
38
|
|
|
if (!$em instanceof EntityManagerInterface) { |
39
|
|
|
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$this->setName(null === $em ? 'kuma:media:clean-deleted-media' : $em); |
42
|
|
|
|
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->em = $em; |
|
|
|
|
47
|
|
|
$this->mediaManager = $mediaManager; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function configure() |
51
|
|
|
{ |
52
|
|
|
parent::configure(); |
53
|
|
|
|
54
|
|
|
$this |
55
|
|
|
->setName('kuma:media:clean-deleted-media') |
56
|
|
|
->setDescription('Throw away all files from the file system that have been deleted in the database') |
57
|
|
|
->setHelp( |
58
|
|
|
"The <info>kuma:media:clean-deleted-media</info> command can be used to clean up your file system after having deleted Media items using the backend." |
59
|
|
|
) |
60
|
|
|
->addOption( |
61
|
|
|
'force', |
62
|
|
|
'f', |
63
|
|
|
InputOption::VALUE_NONE, |
64
|
|
|
'If set does not prompt the user if he is certain he wants to remove Media' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
69
|
|
|
{ |
70
|
|
View Code Duplication |
if (null === $this->em) { |
|
|
|
|
71
|
|
|
$this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
72
|
|
|
$this->mediaManager = $this->getContainer()->get('kunstmaan_media.media_manager'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($input->getOption('force') !== true) { |
76
|
|
|
$helper = $this->getHelper('question'); |
77
|
|
|
$question = new ConfirmationQuestion('<question>Are you sure you want to remove all deleted Media from the file system?</question> ', false); |
78
|
|
|
|
79
|
|
|
if (!$helper->ask($input, $output, $question)) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$output->writeln('<info>Removing all Media from the file system that have their status set to deleted in the database.</info>'); |
85
|
|
|
|
86
|
|
|
$medias = $this->em->getRepository('KunstmaanMediaBundle:Media')->findAllDeleted(); |
87
|
|
|
try { |
88
|
|
|
$this->em->beginTransaction(); |
89
|
|
|
foreach ($medias as $media) { |
90
|
|
|
$this->mediaManager->removeMedia($media); |
91
|
|
|
} |
92
|
|
|
$this->em->flush(); |
93
|
|
|
$this->em->commit(); |
94
|
|
|
$output->writeln('<info>All Media flagged as deleted, have now been removed from the file system.<info>'); |
95
|
|
|
} catch (\Exception $e) { |
96
|
|
|
$this->em->rollback(); |
97
|
|
|
$output->writeln('An error occured while trying to delete Media from the file system:'); |
98
|
|
|
$output->writeln('<error>'. $e->getMessage() . '</error>'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.