Completed
Pull Request — master (#10)
by dan
09:38
created

StyledImagesSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 60
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 7 1
A onImagesGenerated() 0 14 2
A onImagesDeleted() 0 4 1
1
<?php
2
/**
3
 * This file is part of the IrishDan\ResponsiveImageBundle package.
4
 *
5
 * (c) Daniel Byrne <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source
8
 * code.
9
 */
10
11
namespace IrishDan\ResponsiveImageBundle\EventSubscriber;
12
13
use IrishDan\ResponsiveImageBundle\Event\StyledImagesEvent;
14
use IrishDan\ResponsiveImageBundle\Event\StyledImagesEvents;
15
use IrishDan\ResponsiveImageBundle\FileSystem\PrimaryFileSystemWrapper;
16
use League\Flysystem\FilesystemInterface;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
19
/**
20
 * Class StyledImagesSubscriber
21
 *
22
 * @package IrishDan\ResponsiveImageBundle\EventSubscriber
23
 */
24
class StyledImagesSubscriber implements EventSubscriberInterface
25
{
26
    /**
27
     * @var FilesystemInterface
28
     */
29
    private $temporaryFileSystem;
30
    /**
31
     * @var FilesystemInterface|null
32
     */
33
    private $primaryFileSystem;
34
35
    /**
36
     * StyledImagesSubscriber constructor.
37
     *
38
     * @param PrimaryFileSystemWrapper $PrimaryFileSystemWrapper
39
     * @param FilesystemInterface|null $temporaryFileSystem
40
     */
41
    public function __construct(PrimaryFileSystemWrapper $PrimaryFileSystemWrapper, FilesystemInterface $temporaryFileSystem = null)
42
    {
43
        $this->temporaryFileSystem = $temporaryFileSystem;
44
        $this->primaryFileSystem   = $PrimaryFileSystemWrapper->getFileSystem();
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public static function getSubscribedEvents()
51
    {
52
        return [
53
            StyledImagesEvents::STYLED_IMAGES_GENERATED => 'onImagesGenerated',
54
            StyledImagesEvents::STYLED_IMAGES_DELETED   => 'onImagesDeleted',
55
        ];
56
    }
57
58
    /**
59
     * @param StyledImagesEvent $event
60
     */
61
    public function onImagesGenerated(StyledImagesEvent $event)
62
    {
63
        $image = $event->getImage();
64
        $this->temporaryFileSystem->delete($image->getPath());
65
66
        $generatedImages = $event->getStyleImageLocationArray();
67
68
        foreach ($generatedImages as $style => $relativePath) {
69
            $contents = $this->temporaryFileSystem->read($relativePath);
70
            $this->primaryFileSystem->put($relativePath, $contents);
0 ignored issues
show
Security Bug introduced by
It seems like $contents defined by $this->temporaryFileSystem->read($relativePath) on line 69 can also be of type false; however, League\Flysystem\FilesystemInterface::put() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
71
72
            $this->temporaryFileSystem->delete($relativePath);
73
        }
74
    }
75
76
    /**
77
     * @param StyledImagesEvent $event
78
     */
79
    public function onImagesDeleted(StyledImagesEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        // @TODO: Implement.
82
    }
83
}