Completed
Push — master ( 39e189...a82ea3 )
by Dev
13:08
created

MediaCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 30
c 1
b 1
f 0
dl 0
loc 73
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 4 1
A configure() 0 5 1
A __construct() 0 18 1
A generate() 0 12 3
1
<?php
2
3
namespace PiedWeb\CMSBundle\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
7
use Liip\ImagineBundle\Imagine\Data\DataManager;
8
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
9
use PiedWeb\CMSBundle\EventListener\MediaCacheGeneratorTrait;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
14
use Symfony\Component\Filesystem\Filesystem;
15
16
class MediaCommand extends Command
17
{
18
    use MediaCacheGeneratorTrait;
19
20
    /**
21
     * @var EntityManagerInterface
22
     */
23
    private $em;
24
25
    /**
26
     * @var string
27
     */
28
    private $webDir;
29
30
    /**
31
     * @var string
32
     */
33
    private $staticDir;
34
35
    private $params;
36
37
    protected $redirections = '';
38
39
    private $cacheManager;
40
    private $dataManager;
41
    private $filterManager;
42
43
    public function __construct(
44
        EntityManagerInterface $em,
45
        ParameterBagInterface $params,
46
        CacheManager $cacheManager,
47
        DataManager $dataManager,
48
        FilterManager $filterManager,
49
        string $webDir
50
    ) {
51
        $this->em = $em;
52
        $this->filesystem = new Filesystem();
0 ignored issues
show
Bug Best Practice introduced by
The property filesystem does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
        $this->params = $params;
54
        $this->webDir = $webDir;
55
        $this->cacheManager = $cacheManager;
56
        $this->dataManager = $dataManager;
57
        $this->filterManager = $filterManager;
58
        $this->staticDir = $this->webDir.'/../static';
59
60
        parent::__construct();
61
    }
62
63
    protected function configure()
64
    {
65
        $this
66
            ->setName('media:cache:generate')
67
            ->setDescription('Generate all images cache')
68
        ;
69
    }
70
71
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73
        $this->generate();
74
        $output->writeln('Media generated with success.');
75
    }
76
77
    protected function generate(): self
78
    {
79
        $medias = $this->em->getRepository($this->params->get('app.entity_media'))->findAll();
80
81
        foreach ($medias as $media) {
82
            if (false !== strpos($media->getMimeType(), 'image/')) {
83
                $path = '/'.$media->getRelativeDir().'/'.$media->getMedia();
84
                $this->generateCache($path);
85
            }
86
        }
87
88
        return $this;
89
    }
90
}
91