Completed
Push — master ( 47bb75...281d52 )
by
unknown
29:45
created

MediaBundle/Command/CreatePdfPreviewCommand.php (1 issue)

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 \ImagickException;
8
use Kunstmaan\MediaBundle\Entity\Media;
9
use Kunstmaan\MediaBundle\Helper\Transformer\PdfTransformer;
10
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * @final since 5.1
16
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
17
 */
18
class CreatePdfPreviewCommand extends ContainerAwareCommand
19
{
20
    /**
21
     * @var EntityManager
22
     */
23
    private $em;
24
25
    /**
26
     * @var PdfTransformer
27
     */
28
    private $pdfTransformer;
29
30
    /**
31
     * @var string
32
     */
33
    private $webRoot;
34
35
    /**
36
     * @var bool
37
     */
38
    private $enablePdfPreview;
39
40
    /**
41
     * @param EntityManagerInterface|null $em
42
     * @param PdfTransformer|null         $mediaManager
43
     */
44 View Code Duplication
    public function __construct(/* EntityManagerInterface */ $em = null, /* PdfTransformer */ $pdfTransformer = null, $webRoot = null, $enablePdfPreview = null)
0 ignored issues
show
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...
45
    {
46
        parent::__construct();
47
48
        if (!$em instanceof EntityManagerInterface) {
49
            @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);
50
51
            $this->setName(null === $em ? 'kuma:media:create-pdf-previews' : $em);
52
53
            return;
54
        }
55
56
        $this->em = $em;
57
        $this->pdfTransformer = $pdfTransformer;
58
        $this->webRoot = $webRoot;
59
        $this->enablePdfPreview = $enablePdfPreview;
60
    }
61
62
    protected function configure()
63
    {
64
        parent::configure();
65
66
        $this
67
            ->setName('kuma:media:create-pdf-previews')
68
            ->setDescription('Create preview images for PDFs that have already been uploaded')
69
            ->setHelp(
70
                "The <info>kuma:media:create-pdf-previews</info> command can be used to create preview images for PDFs that have already been uploaded."
71
            );
72
    }
73
74
    public function execute(InputInterface $input, OutputInterface $output)
75
    {
76
        if (null ===  $this->em) {
77
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
78
            $this->pdfTransformer = $this->getContainer()->get('kunstmaan_media.pdf_transformer');
79
            $this->webRoot = $this->getContainer()->getParameter('kunstmaan_media.web_root');
80
            $this->enablePdfPreview = $this->getContainer()->getParameter('kunstmaan_media.enable_pdf_preview');
81
        }
82
83
        $output->writeln('Creating PDF preview images...');
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($this->webRoot . $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