ImageManager   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 145
Duplicated Lines 4.14 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 6
dl 6
loc 145
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteAllImages() 0 5 1
A deleteStyledImages() 0 10 3
A deleteImage() 6 10 2
A createStyleDirectory() 0 9 2
A imageExists() 0 4 1
A createCustomStyledImage() 0 17 3
A deleteCustomStyledImages() 0 4 1
A __construct() 0 13 1
A createAllStyledImages() 0 6 1
A createStyledImages() 0 20 3
B createStyledImage() 0 24 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\ImageProcessing;
12
13
use IrishDan\ResponsiveImageBundle\Event\StyledImagesEvent;
14
use IrishDan\ResponsiveImageBundle\Event\StyledImagesEvents;
15
use IrishDan\ResponsiveImageBundle\FileSystem\PrimaryFileSystemWrapper;
16
use IrishDan\ResponsiveImageBundle\ResponsiveImageInterface;
17
use IrishDan\ResponsiveImageBundle\StyleManager;
18
use League\Flysystem\FilesystemInterface;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
21
/**
22
 * Class ImageManager
23
 *
24
 * @package IrishDan\ResponsiveImageBundle\Image
25
 */
26
class ImageManager
27
{
28
    /**
29
     * @var StyleManager
30
     */
31
    protected $styleManager;
32
    protected $ImageStyler;
33
    protected $fileSystem;
34
    protected $temporaryFileSystem;
35
    protected $eventDispatcher;
36
    protected $generatedImages = [];
37
38
    public function __construct(
39
        StyleManager $styleManager,
40
        ImageStyler $imageStyler,
41
        PrimaryFileSystemWrapper $fileSystem,
42
        FilesystemInterface $temporaryFileSystem = null,
43
        EventDispatcherInterface $eventDispatcher = null
44
    ) {
45
        $this->styleManager        = $styleManager;
46
        $this->ImageStyler         = $imageStyler;
47
        $this->fileSystem          = $fileSystem->getFileSystem();
48
        $this->temporaryFileSystem = $temporaryFileSystem;
49
        $this->eventDispatcher     = $eventDispatcher;
50
    }
51
52
    public function createAllStyledImages(ResponsiveImageInterface $image)
53
    {
54
        $styles = $this->styleManager->getAllStylesNames();
55
56
        return $this->createStyledImages($image, $styles);
57
    }
58
59
    public function createStyledImages(ResponsiveImageInterface $image, array $styles = [])
60
    {
61
        // Copy the image from its current filesystem onto the filesystem used by intervention.
62
        $contents = $this->fileSystem->read($image->getPath());
63
        $this->temporaryFileSystem->put($image->getPath(), $contents);
64
65
        // Generate all of the required files
66
        foreach ($styles as $style) {
67
            // @TODO: Use the relative part instead of the full path!!!
68
            $this->createStyledImage($image, $style);
69
        }
70
71
        // Dispatch an event.
72
        if (!empty($this->eventDispatcher)) {
73
            $imagesGeneratedEvent = new StyledImagesEvent($image, $this->generatedImages);
74
            $this->eventDispatcher->dispatch(StyledImagesEvents::STYLED_IMAGES_GENERATED, $imagesGeneratedEvent);
75
        }
76
77
        return $this->generatedImages;
78
    }
79
80
    protected function createStyledImage(ResponsiveImageInterface $image, $style)
81
    {
82
        $styleData = $this->styleManager->getStyleData($style);
83
84
        $directory = $this->temporaryFileSystem->getAdapter()->getPathPrefix();
85
        $source    = $directory . $image->getPath();
86
87
        if (!empty($styleData)) {
88
            $cropFocusData     = $image->getCropCoordinates();
89
            $relativeStylePath = $this->styleManager->getStylePath($image, $style);
90
91
            $destination = $directory . $relativeStylePath;
92
93
            // Intervention needs directories to exist prior to creating images.
94
            $this->createStyleDirectory($relativeStylePath);
95
96
            try {
97
                $this->ImageStyler->createImage($source, $destination, $styleData, $cropFocusData);
98
                $this->generatedImages[$style] = $relativeStylePath;
99
            } catch (\Exception $e) {
100
                // @TODO: Throw exception
101
            }
102
        }
103
    }
104
105
    public function deleteAllImages(ResponsiveImageInterface $image)
106
    {
107
        $this->deleteImage($image);
108
        $this->deleteStyledImages($image);
109
    }
110
111
    public function deleteStyledImages(ResponsiveImageInterface $image, array $styles = [])
112
    {
113
        if (empty($styles)) {
114
            $styles = $this->styleManager->getAllStylesNames();
115
        }
116
117
        foreach ($styles as $style) {
118
            $this->deleteImage($image, $style);
119
        }
120
    }
121
122
    public function deleteImage(ResponsiveImageInterface $image, $style = '')
123
    {
124 View Code Duplication
        if (!empty($style)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
            $path = $this->styleManager->getStylePath($image, $style);
126
        }
127
        else {
128
            $path = $image->getPath();
129
        }
130
        $this->fileSystem->delete($path);
131
    }
132
133
    protected function createStyleDirectory($destination)
134
    {
135
        $filename  = basename($destination);
136
        $directory = explode($filename, $destination)[0];
137
138
        if (!$this->temporaryFileSystem->has($directory)) {
139
            $this->temporaryFileSystem->createDir($directory);
140
        }
141
    }
142
143
    public function imageExists($path)
144
    {
145
        return $this->fileSystem->has($path);
146
    }
147
148
    public function createCustomStyledImage(ResponsiveImageInterface $image, $customStyleString, $forceGenerate = false)
149
    {
150
        // @TODO: To avoid creating images, that already exist, check if it exists, need a way to disable this checking
151
152
        // check is it exists, using the string
153
        $stylePath = $this->styleManager->getStylePath($image, $customStyleString);
154
155
        $exists = $this->imageExists($stylePath);
156
        if (!$exists || $forceGenerate) {
157
            // Create the style array and add to the existing styles
158
            $style = $this->styleManager->styleDataFromCustomStyleString($customStyleString);
159
            $this->styleManager->addStyle($customStyleString, $style);
160
161
            // generate the image
162
            $this->createStyledImages($image, [$customStyleString]);
163
        }
164
    }
165
166
    public function deleteCustomStyledImages(ResponsiveImageInterface $image)
0 ignored issues
show
Unused Code introduced by
The parameter $image 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...
167
    {
168
        // @TODO: Implement
169
    }
170
}