OptimizeImagesCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 1
b 0
f 0
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
A execute() 0 25 6
1
<?php
2
namespace TinyPngBundle\Command;
3
4
use Pimcore\Console\AbstractCommand;
5
use Pimcore\Log\ApplicationLogger;
6
use Pimcore\Model\Asset;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use TinyPngBundle\Service\TinyPngService;
10
11
class OptimizeImagesCommand extends AbstractCommand
12
{
13
    private $mimeTypes = [
14
        'image/jpeg',
15
        'image/png'
16
    ];
17
18
    /**
19
     *
20
     */
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('tinypng:optimize')
25
            ->setDescription('Optimize all Assets with TinyPNG');
26
    }
27
28
    /**
29
     * @param InputInterface $input
30
     * @param OutputInterface $output
31
     */
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        $listing = new Asset\Listing();
35
36
        foreach ($listing as $item) {
37
38
            if($item instanceof Asset\Image) {
39
                if(file_exists($item->getFileSystemPath())) {
40
41
                    if (!in_array($item->getMimetype(), $this->mimeTypes)) {
42
                        return false;
43
                    }
44
45
                    try {
46
                        (new TinyPngService())->minimize($item);
47
                    } catch (\Exception $e) {
48
                        (ApplicationLogger::getInstance())->error($e->getMessage(), [
49
                            'relatedObject' => $item,
50
                            'component' => 'TinyPNG'
51
                        ]);
52
                    }
53
54
                    $this->dump("Optimized: " . $item->getKey());
55
56
                    return true;
57
58
                }
59
            }
60
        }
61
    }
62
}
63