Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

MediaBundle/Command/CleanDeletedMediaCommand.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
$em is of type object<Doctrine\ORM\EntityManagerInterface>, but the property $em was declared to be of type object<Doctrine\ORM\EntityManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
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
88
        try {
89
            $this->em->beginTransaction();
90
            foreach ($medias as $media) {
91
                $this->mediaManager->removeMedia($media);
92
            }
93
            $this->em->flush();
94
            $this->em->commit();
95
            $output->writeln('<info>All Media flagged as deleted, have now been removed from the file system.<info>');
96
        } catch (\Exception $e) {
97
            $this->em->rollback();
98
            $output->writeln('An error occured while trying to delete Media from the file system:');
99
            $output->writeln('<error>'. $e->getMessage() . '</error>');
100
        }
101
    }
102
}
103