Completed
Pull Request — master (#1974)
by Jeroen
13:49
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 Kunstmaan\MediaBundle\Helper\MediaManager;
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
use Symfony\Component\Console\Question\ConfirmationQuestion;
11
12
/**
13
 * @final since 5.1
14
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
15
 */
16
class CleanDeletedMediaCommand extends ContainerAwareCommand
17
{
18
    /**
19
     * @var EntityManager
20
     */
21
    private $em;
22
23
    /**
24
     * @var MediaManager
25
     */
26
    private $mediaManager;
27
28
    /**
29
     * @param EntityManagerInterface|null $em
30
     * @param MediaManager|null           $mediaManager
31
     */
32
    public function __construct(/* EntityManagerInterface */ $em = null, /* MediaManager */ $mediaManager = null)
33
    {
34
        parent::__construct();
35
36
        if (!$em instanceof EntityManagerInterface) {
0 ignored issues
show
Bug introduced by
The class Kunstmaan\MediaBundle\Co...\EntityManagerInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
37
            @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...
38
39
            $this->setName(null === $em ? 'kuma:media:clean-deleted-media' : $em);
40
41
            return;
42
        }
43
44
        $this->em = $em;
0 ignored issues
show
Documentation Bug introduced by
It seems like $em of type object<Kunstmaan\MediaBu...EntityManagerInterface> is incompatible with the declared type object<Kunstmaan\MediaBu...\Command\EntityManager> of property $em.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
        $this->mediaManager = $mediaManager;
46
    }
47
48
    protected function configure()
49
    {
50
        parent::configure();
51
52
        $this
53
            ->setName('kuma:media:clean-deleted-media')
54
            ->setDescription('Throw away all files from the file system that have been deleted in the database')
55
            ->setHelp(
56
                "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."
57
            )
58
            ->addOption(
59
                'force',
60
                'f',
61
                InputOption::VALUE_NONE,
62
                'If set does not prompt the user if he is certain he wants to remove Media'
63
            );
64
    }
65
66
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68 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...
69
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
70
            $this->mediaManager = $this->getContainer()->get('kunstmaan_media.media_manager');
71
        }
72
73
        if ($input->getOption('force') !== true) {
74
            $helper = $this->getHelper('question');
75
            $question = new ConfirmationQuestion('<question>Are you sure you want to remove all deleted Media from the file system?</question> ', false);
76
77
            if (!$helper->ask($input, $output, $question)) {
78
                return;
79
            }
80
        }
81
82
        $output->writeln('<info>Removing all Media from the file system that have their status set to deleted in the database.</info>');
83
84
        $medias = $this->em->getRepository('KunstmaanMediaBundle:Media')->findAllDeleted();
85
        try {
86
            $this->em->beginTransaction();
87
            foreach ($medias as $media) {
88
                $this->mediaManager->removeMedia($media);
89
            }
90
            $this->em->flush();
91
            $this->em->commit();
92
            $output->writeln('<info>All Media flagged as deleted, have now been removed from the file system.<info>');
93
        } catch (\Exception $e) {
94
            $this->em->rollback();
95
            $output->writeln('An error occured while trying to delete Media from the file system:');
96
            $output->writeln('<error>'. $e->getMessage() . '</error>');
97
        }
98
    }
99
}
100