Completed
Push — master ( a06a95...a1d542 )
by dan
15s
created

StyleManager.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace IrishDan\ResponsiveImageBundle;
4
5
/**
6
 * Class StyleManager
7
 *
8
 * @package ResponsiveImageBundle
9
 */
10
class StyleManager
11
{
12
    /**
13
     * @var array
14
     */
15
    private $breakpoints = [];
16
    /**
17
     * @var
18
     */
19
    private $displayPathPrefix = '/';
20
    /**
21
     * @var
22
     */
23
    private $remoteFilePolicy;
24
    /**
25
     * @var FileManager
26
     */
27
    private $fileManager;
28
    /**
29
     * @var array
30
     */
31
    private $pictureSets = [];
32
    /**
33
     * @var array
34
     */
35
    private $styles = [];
36
37
    /**
38
     * StyleManager constructor.
39
     *
40
     * @param \ResponsiveImageBundle\FileManager $system
41
     * @param array                              $parameters
42
     */
43
    public function __construct(FileManager $system, array $parameters)
44
    {
45
        $this->fileManager = $system;
46
47
        // Set the styles array.
48
        if (!empty($parameters['image_styles'])) {
49
            $this->styles = $parameters['image_styles'];
50
        }
51
52
        // Set the picture sets array
53
        if (!empty($parameters['picture_sets'])) {
54
            $this->pictureSets = $parameters['picture_sets'];
55
            // Get the any picture set styles and incorporate into the configured styles array.
56
            foreach ($parameters['picture_sets'] as $pictureSetName => $picture_set) {
57
                foreach ($picture_set as $breakpoint => $set_style) {
58
                    if (is_array($set_style)) {
59
                        $this->styles[$pictureSetName . '-' . $breakpoint] = $set_style;
60
                    }
61
                }
62
            }
63
        }
64
65
        // Set the breakpoints array.
66
        if (!empty($parameters['breakpoints'])) {
67
            $this->breakpoints = $parameters['breakpoints'];
68
        }
69
70
        // Set the prefix.
71
        if (!empty($parameters['path_prefix'])) {
72
            $this->displayPathPrefix = $parameters['path_prefix'];
73
        }
74
75
        if (!empty($parameters['aws_s3'])) {
76
            if (!empty($parameters['aws_s3']['remote_file_policy'])) {
77
                $this->remoteFilePolicy = $parameters['aws_s3']['remote_file_policy'];
78
            }
79
        }
80
    }
81
82
    /**
83
     * Deletes a file.
84
     *
85
     * @param $filename
86
     */
87
    public function deleteImageFile($filename)
88
    {
89
        $system_upload_path = $this->fileManager->getSystemUploadPath();
90
        $path = $system_upload_path . '/' . $filename;
91
        // Delete the source file.
92
        $this->fileManager->deleteFile($path);
93
        // Delete the styled files.
94
        $this->deleteImageStyledFiles($filename);
0 ignored issues
show
The method deleteImageStyledFiles() does not seem to exist on object<IrishDan\Responsi...ageBundle\StyleManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
    }
96
97
    /**
98
     * Deletes all of the files in an image style folder.
99
     *
100
     * @param array $styles
101
     */
102
    public function deleteStyledImages(array $styles)
103
    {
104
        foreach ($styles as $style) {
105
            $system_styles_path = $this->fileManager->getSystemStylesPath();
106
            $path = $system_styles_path . '/' . $style;
107
            $this->fileManager->deleteDirectory($path);
108
        }
109
    }
110
111
    /**
112
     * Checks if a given style name is a defined style.
113
     *
114
     * @param $styleName
115
     * @return bool
116
     */
117
    public function styleExists($styleName)
118
    {
119
        $style = $this->getStyle($styleName);
120
121
        return !empty($style);
122
    }
123
124
    /**
125
     * @param mixed $displayPathPrefix
126
     */
127
    public function setDisplayPathPrefix($displayPathPrefix)
128
    {
129
        $this->displayPathPrefix = $displayPathPrefix;
130
    }
131
132
    /**
133
     * @param mixed $remoteFilePolicy
134
     */
135
    public function setRemoteFilePolicy($remoteFilePolicy)
136
    {
137
        $this->remoteFilePolicy = $remoteFilePolicy;
138
    }
139
140
    /**
141
     * @return array
142
     */
143
    public function getAllStyles()
144
    {
145
        return $this->styles;
146
    }
147
148
    /**
149
     * Returns a style information array.
150
     *
151
     * @param $stylename
152
     * @return bool
153
     */
154
    public function getStyle($stylename)
155
    {
156
        if (!in_array($stylename, array_keys($this->styles))) {
157
            return false;
158
        } else {
159
            return $this->styles[$stylename];
160
        }
161
    }
162
163
    /**
164
     * Prefixes url string with the displayPathPrefix string, if the style and the config require it.
165
     *
166
     * @param $url
167
     * @param $style
168
     * @return string
169
     */
170
    protected function prefixPath($url, $style = null)
171
    {
172
        // Remote fle policy values ALL, STYLED_ONLY.
173
        if (!empty($this->displayPathPrefix) && $style !== null) {
174
            $url = $this->displayPathPrefix . $url;
175
        } else {
176
            if ($this->remoteFilePolicy != 'STYLED_ONLY' && $style == null) {
177
                $url = $this->displayPathPrefix . $url;
178
            } else {
179
                if ($this->remoteFilePolicy == 'STYLED_ONLY' && $style == null) {
180
                    $url = '/' . $url;
181
                } else {
182
                    $url = '/' . $url;
183
                }
184
            }
185
        }
186
187
        return $url;
188
    }
189
190
    public function getMediaQuerySourceMappings(ResponsiveImageInterface $image, $pictureSetName)
191
    {
192
        $mappings = [];
193
        $filename = $image->getPath();
194
195
        // First mapping is the default image.
196
        $mappings[] = $image->getStyle();
197
198
        if (!empty($this->pictureSets[$pictureSetName])) {
199
            $set = $this->pictureSets[$pictureSetName];
200
201
            foreach ($set as $break => $style) {
202
                if (is_array($style)) {
203
                    $styleName = $pictureSetName . '-' . $break;
204
                } else {
205
                    $styleName = $style;
206
                }
207
                $path = $this->buildStylePath($styleName, $filename);
208
209
                $mappings[$this->breakpoints[$break]] = $path;
210
            }
211
        }
212
213
        return $mappings;
214
    }
215
216
    protected function buildStylePath($styleName, $fileName)
217
    {
218
        $styles_directory = $this->fileManager->getStylesDirectory();
219
        $path = $styles_directory . '/' . $styleName . '/' . $fileName;
220
        $path = $this->prefixPath($path, $styleName);
221
222
        return $path;
223
    }
224
225
    /**
226
     * Sets the path of a an Image object to the full styled image path.
227
     *
228
     * @param ResponsiveImageInterface $image
229
     * @param null                     $styleName
230
     * @return ResponsiveImageInterface
231
     */
232
    public function setImageStyle(ResponsiveImageInterface $image, $styleName = null)
233
    {
234
        // @TODO: Hack to avoid looping over itself and string paths together.
235
        if ($styleName !== null && empty($this->getStyle($styleName))) {
236
            return $image;
237
        }
238
239
        $filename = $image->getPath();
240
241
        if (!empty($styleName)) {
242
            $stylePath = $this->fileManager->styleWebPath($styleName);
243
        } else {
244
            $stylePath = $this->fileManager->getUploadsDirectory();
245
        }
246
247
        $webPath = $stylePath . '/' . $filename;
248
        $webPath = $this->prefixPath($webPath, $styleName);
249
250
        $image->setStyle($webPath);
251
252
        return $image;
253
    }
254
}