Imagy::makeNew()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4286
c 1
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
1
<?php namespace Modules\Media\Image;
2
3
use Illuminate\Contracts\Config\Repository;
4
use Illuminate\Support\Facades\App;
5
use Modules\Media\Entities\File;
6
7
class Imagy
8
{
9
    /**
10
     * @var \Intervention\Image\Image
11
     */
12
    private $image;
13
    /**
14
     * @var \Illuminate\Filesystem\Filesystem
15
     */
16
    private $finder;
17
    /**
18
     * @var ImageFactoryInterface
19
     */
20
    private $imageFactory;
21
    /**
22
     * @var ThumbnailsManager
23
     */
24
    private $manager;
25
26
    /**
27
     * All the different images types where thumbnails should be created
28
     * @var array
29
     */
30
    private $imageExtensions = ['jpg', 'png', 'jpeg', 'gif'];
31
    /**
32
     * @var Repository
33
     */
34
    private $config;
35
36
    /**
37
     * @param ImageFactoryInterface $imageFactory
38
     * @param ThumbnailsManager     $manager
39
     * @param Repository            $config
40
     */
41
    public function __construct(ImageFactoryInterface $imageFactory, ThumbnailsManager $manager, Repository $config)
42
    {
43
        $this->image = app('Intervention\Image\ImageManager');
44
        $this->finder = app('Illuminate\Filesystem\Filesystem');
45
        $this->imageFactory = $imageFactory;
46
        $this->manager = $manager;
47
        $this->config = $config;
48
    }
49
50
    /**
51
     * Get an image in the given thumbnail options
52
     * @param  string $path
53
     * @param  string $thumbnail
54
     * @param  bool   $forceCreate
55
     * @return string
56
     */
57
    public function get($path, $thumbnail, $forceCreate = false)
58
    {
59
        if (!$this->isImage($path)) {
60
            return;
61
        }
62
63
        $filename = $this->config->get('asgard.media.config.files-path') . $this->newFilename($path, $thumbnail);
64
65
        if ($this->returnCreatedFile($filename, $forceCreate)) {
66
            return $filename;
67
        }
68
69
        $this->makeNew($path, $filename, $thumbnail);
70
71
        return $filename;
72
    }
73
74
    /**
75
     * Return the thumbnail path
76
     * @param  string $originalImage
77
     * @param  string $thumbnail
78
     * @return string
79
     */
80
    public function getThumbnail($originalImage, $thumbnail)
81
    {
82
        if (!$this->isImage($originalImage)) {
83
            return $originalImage;
84
        }
85
86
        return $this->config->get('asgard.media.config.files-path') . $this->newFilename($originalImage, $thumbnail);
87
    }
88
89
    /**
90
     * Create all thumbnails for the given image path
91
     * @param string $path
92
     */
93
    public function createAll($path)
94
    {
95
        if (!$this->isImage($path)) {
96
            return;
97
        }
98
99
        foreach ($this->manager->all() as $thumbnail) {
100
            $image = $this->image->make(public_path() . $path);
101
            $filename = $this->config->get('asgard.media.config.files-path') . $this->newFilename($path, $thumbnail->name());
102
            foreach ($thumbnail->filters() as $manipulation => $options) {
103
                $image = $this->imageFactory->make($manipulation)->handle($image, $options);
104
            }
105
            $image = $image->encode(pathinfo($path, PATHINFO_EXTENSION));
106
            $this->writeImage($filename, $image);
107
        }
108
    }
109
110
    /**
111
     * Prepend the thumbnail name to filename
112
     * @param $path
113
     * @param $thumbnail
114
     * @return mixed|string
115
     */
116
    private function newFilename($path, $thumbnail)
117
    {
118
        $filename = pathinfo($path, PATHINFO_FILENAME);
119
120
        return $filename . '_' . $thumbnail . '.' . pathinfo($path, PATHINFO_EXTENSION);
121
    }
122
123
    /**
124
     * Return the already created file if it exists and force create is false
125
     * @param  string $filename
126
     * @param  bool   $forceCreate
127
     * @return bool
128
     */
129
    private function returnCreatedFile($filename, $forceCreate)
130
    {
131
        return $this->finder->isFile(public_path() . $filename) && !$forceCreate;
132
    }
133
134
    /**
135
     * Write the given image
136
     * @param string $filename
137
     * @param string $image
138
     */
139
    private function writeImage($filename, $image)
140
    {
141
        $this->finder->put(public_path($filename), $image);
142
        @chmod(public_path($filename), 0666);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
143
    }
144
145
    /**
146
     * Make a new image
147
     * @param string      $path
148
     * @param string      $filename
149
     * @param string null $thumbnail
150
     */
151
    private function makeNew($path, $filename, $thumbnail)
152
    {
153
        $image = $this->image->make(public_path() . $path);
154
155
        foreach ($this->manager->find($thumbnail) as $manipulation => $options) {
156
            $image = $this->imageFactory->make($manipulation)->handle($image, $options);
157
        }
158
159
        $image = $image->encode(pathinfo($path, PATHINFO_EXTENSION));
160
161
        $this->writeImage($filename, $image);
162
    }
163
164
    /**
165
     * Check if the given path is en image
166
     * @param  string $path
167
     * @return bool
168
     */
169
    public function isImage($path)
170
    {
171
        return in_array(pathinfo($path, PATHINFO_EXTENSION), $this->imageExtensions);
172
    }
173
174
    /**
175
     * Delete all files on disk for the given file in storage
176
     * This means the original and the thumbnails
177
     * @param $file
178
     * @return bool
179
     */
180
    public function deleteAllFor(File $file)
181
    {
182
        if (!$this->isImage($file->path)) {
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<Modules\Media\Entities\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
183
            return $this->finder->delete($file->path);
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<Modules\Media\Entities\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
184
        }
185
186
        $paths[] = public_path() . $file->path;
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<Modules\Media\Entities\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Coding Style Comprehensibility introduced by
$paths was never initialized. Although not strictly required by PHP, it is generally a good practice to add $paths = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
187
        $fileName = pathinfo($file->path, PATHINFO_FILENAME);
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<Modules\Media\Entities\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
188
        $extension = pathinfo($file->path, PATHINFO_EXTENSION);
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<Modules\Media\Entities\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
189
        foreach ($this->manager->all() as $thumbnail) {
190
            $paths[] = public_path() . $this->config->get(
191
                    'asgard.media.config.files-path'
192
                ) . "{$fileName}_{$thumbnail->name()}.{$extension}";
193
        }
194
195
        return $this->finder->delete($paths);
196
    }
197
}
198