Completed
Push — 6.0 ( eb641c...92cb73 )
by Ruud
148:12 queued 131:31
created

CleanDeletedMediaCommand::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 2
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
    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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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