MediaCacheGeneratorCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use PiedWeb\CMSBundle\Service\MediaCacheGeneratorService;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\ProgressBar;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class MediaCacheGeneratorCommand extends Command
14
{
15
    /**
16
     * @var EntityManagerInterface
17
     */
18
    private $em;
19
20
    /**
21
     * @var string
22
     */
23
    private $mediaClass;
24
25
    /**
26
     * @var MediaCacheGeneratorService
27
     */
28
    private $mediaCacheGenerator;
29
30
    public function __construct(
31
        EntityManagerInterface $em,
32
        MediaCacheGeneratorService $mediaCacheGenerator,
33
        string $mediaClass
34
    ) {
35
        $this->em = $em;
36
        $this->mediaClass = $mediaClass;
37
        $this->mediaCacheGenerator = $mediaCacheGenerator;
38
39
        parent::__construct();
40
    }
41
42
    protected function configure()
43
    {
44
        $this
45
            ->setName('piedweb:media:cache')
46
            ->setDescription('Generate all images cache')
47
            ->addArgument('media', InputArgument::OPTIONAL, 'Image path (without `/media/`) to (re)generate cache.');
48
    }
49
50
    protected function getMedias(InputInterface $input)
51
    {
52
        $repo = $this->em->getRepository($this->mediaClass);
53
54
        if ($input->getArgument('media')) {
55
            return $repo->findBy(['media' => $input->getArgument('media')]);
56
        }
57
58
        return $repo->findAll();
59
    }
60
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $medias = $this->getMedias($input);
64
65
        $progressBar = new ProgressBar($output, \count($medias));
66
        $progressBar->start();
67
        foreach ($medias as $media) {
68
            if (false !== strpos($media->getMimeType(), 'image/')) {
69
                $this->mediaCacheGenerator->generateCache($media);
70
            }
71
            $progressBar->advance();
72
        }
73
        $progressBar->finish();
74
75
        return 0;
76
    }
77
}
78