Completed
Push — master ( ba8ed9...770316 )
by Jeroen
06:11
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
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 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
    public function __construct(/* EntityManagerInterface */ $em = null, /* PdfTransformer */ $pdfTransformer = null, $webRoot = null, $enablePdfPreview = null)
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
                $this->pdfTransformer->apply($this->webRoot . $media->getUrl());
95
            } catch (ImagickException $e) {
96
                $output->writeln('<comment>'.$e->getMessage().'</comment>');
97
            }
98
        }
99
100
        $output->writeln('<info>PDF preview images have been created.</info>');
101
    }
102
103
    /**
104
     * Checks whether the command is enabled or not in the current environment.
105
     *
106
     * Override this to check for x or y and return false if the command can not
107
     * run properly under the current conditions.
108
     *
109
     * @return bool
110
     */
111
    public function isEnabled()
112
    {
113
        if (null === $this->enablePdfPreview) {
114
            $this->enablePdfPreview = $this->getContainer()->getParameter('kunstmaan_media.enable_pdf_preview');
115
        }
116
117
        return $this->enablePdfPreview;
118
    }
119
}
120