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

CreatePdfPreviewCommand::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 4
1
<?php
2
3
namespace Kunstmaan\MediaBundle\Command;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use \ImagickException;
8
use Kunstmaan\MediaBundle\Helper\Transformer\PdfTransformer;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * @final since 5.1
15
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
16
 */
17
class CreatePdfPreviewCommand extends ContainerAwareCommand
18
{
19
    /**
20
     * @var EntityManager
21
     */
22
    private $em;
23
24
    /**
25
     * @var PdfTransformer
26
     */
27
    private $pdfTransformer;
28
29
    /**
30
     * @var string
31
     */
32
    private $rootDir;
33
34
    /**
35
     * @var bool
36
     */
37
    private $enablePdfPreview;
38
39
    /**
40
     * @param EntityManagerInterface|null $em
41
     * @param PdfTransformer|null         $mediaManager
0 ignored issues
show
Bug introduced by
There is no parameter named $mediaManager. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
42
     */
43 View Code Duplication
    public function __construct(/* EntityManagerInterface */ $em = null, /* PdfTransformer */ $pdfTransformer = null, $rootDir = null, $enablePdfPreview = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
44
    {
45
        parent::__construct();
46
47
        if (!$em instanceof EntityManagerInterface) {
48
            @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...
49
50
            $this->setName(null === $em ? 'kuma:media:create-pdf-previews' : $em);
51
52
            return;
53
        }
54
55
        $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...
56
        $this->pdfTransformer = $pdfTransformer;
57
        $this->rootDir = $rootDir;
58
        $this->enablePdfPreview = $enablePdfPreview;
59
    }
60
61
    protected function configure()
62
    {
63
        parent::configure();
64
65
        $this
66
            ->setName('kuma:media:create-pdf-previews')
67
            ->setDescription('Create preview images for PDFs that have already been uploaded')
68
            ->setHelp(
69
                "The <info>kuma:media:create-pdf-previews</info> command can be used to create preview images for PDFs that have already been uploaded."
70
            );
71
    }
72
73
    public function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        if (null ===  $this->em) {
76
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
77
            $this->pdfTransformer = $this->getContainer()->get('kunstmaan_media.pdf_transformer');
78
            $this->rootDir = $this->getContainer()->get('kernel')->getRootDir();
79
            $this->enablePdfPreview = $this->getContainer()->getParameter('kunstmaan_media.enable_pdf_preview');
80
        }
81
82
        $output->writeln('Creating PDF preview images...');
83
        $webPath = realpath($this->rootDir . '/../web') . DIRECTORY_SEPARATOR;
84
85
        /**
86
         * @var EntityManager
87
         */
88
        $medias = $this->em->getRepository('KunstmaanMediaBundle:Media')->findBy(
89
            array('contentType' => 'application/pdf', 'deleted' => false)
90
        );
91
        /** @var Media $media */
92
        foreach ($medias as $media) {
93
            try
94
            {
95
                $this->pdfTransformer->apply($webPath . $media->getUrl());
96
            }
97
            catch(ImagickException $e)
98
            {
99
                $output->writeln('<comment>'.$e->getMessage().'</comment>');
100
            }
101
        }
102
103
        $output->writeln('<info>PDF preview images have been created.</info>');
104
    }
105
106
    /**
107
     * Checks whether the command is enabled or not in the current environment.
108
     *
109
     * Override this to check for x or y and return false if the command can not
110
     * run properly under the current conditions.
111
     *
112
     * @return bool
113
     */
114
    public function isEnabled()
115
    {
116
        if (null === $this->enablePdfPreview) {
117
            $this->enablePdfPreview = $this->getContainer()->getParameter('kunstmaan_media.enable_pdf_preview');
118
        }
119
120
        return $this->enablePdfPreview;
121
    }
122
}
123