Passed
Push — master ( 1120e7...dcb672 )
by Jesús
02:28
created

ImageService::destroyImage()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 10
nop 1
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace OkayBueno\Images\Services\src;
4
5
use Illuminate\Support\Facades\Storage;
6
use OkayBueno\Images\Events\ImageWasCreated;
7
use OkayBueno\Images\Events\ImageWasProcessed;
8
use OkayBueno\Images\Events\ImageWasDeleted;
9
use OkayBueno\Images\Models\Image;
10
use OkayBueno\Images\Services\ImageProcessingServiceInterface;
11
use OkayBueno\Images\Services\ImageServiceInterface;
12
use OkayBueno\Images\Services\Validation\ImageValidatorInterface;
13
14
/**
15
 * Class ImageService
16
 * @package OkayBueno\Images\Services\src
17
 */
18
class ImageService implements ImageServiceInterface
19
{
20
    protected $imageValidator;
21
    protected $imageProcessingService;
22
23
    /**
24
     * @param ImageValidatorInterface $imageValidatorInterface
25
     * @param ImageProcessingServiceInterface $imageProcessingServiceInterface
26
     */
27
    public function __construct(
28
        ImageValidatorInterface $imageValidatorInterface,
29
        ImageProcessingServiceInterface $imageProcessingServiceInterface
30
    )
31
    {
32
        $this->imageValidator = $imageValidatorInterface;
33
        $this->imageProcessingService = $imageProcessingServiceInterface;
34
    }
35
36
37
    /**
38
     * @param int $imageId
39
     * @return array|Image
40
     */
41
    public function findImageById( $imageId )
42
    {
43
        $data = [
44
            'id' => $imageId
45
        ];
46
47
        if ( $this->imageValidator->with( $data )->passes( ImageValidatorInterface::EXISTS_BY_ID ) )
48
        {
49
            return Image::find( $imageId );
50
        }
51
52
        return $this->imageValidator->errors();
53
    }
54
55
56
    /**
57
     * @param mixed $imageDataOrImageURL
58
     * @param array $options
59
     * @return array|bool|Image
60
     */
61
    public function createImage( $imageDataOrImageURL, array $options = [] )
62
    {
63
        $path = @$options['path'];
64
65
        $imagePath = $this->imageProcessingService->createImageFromImageDataOrURL( $imageDataOrImageURL, $path );
66
67
        if ( $imagePath )
68
        {
69
            $filename = basename( $imagePath );
70
71
            $data = [
72
                'path' => $imagePath,
73
                'filename' => $filename,
74
                'processed' => FALSE,
75
                'type' => @$options['type'],
76
            ];
77
78 View Code Duplication
            if ( $this->imageValidator->with( $data )->passes( ImageValidatorInterface::CREATE ) )
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...
79
            {
80
                $image = Image::create( $data );
81
82
                event( new ImageWasCreated( $image, $options ) );
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
                /** @scrutinizer ignore-call */ event( new ImageWasCreated( $image, $options ) );
Loading history...
83
84
                return $image;
85
            }
86
87
            return $this->imageValidator->errors();
88
        }
89
90
        return FALSE;
91
92
    }
93
94
95
    /**
96
     * @param $imageId
97
     * @param bool|TRUE $skipValidation
98
     * @return array|bool
99
     */
100
    public function deleteImage( $imageId, $skipValidation = TRUE )
101
    {
102
        $data = [
103
            'id' => $imageId
104
        ];
105
106 View Code Duplication
        if ( $skipValidation || $this->imageValidator->with( $data )->passes( ImageValidatorInterface::EXISTS_BY_ID ) )
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...
107
        {
108
            $image = Image::find( $imageId );
109
110
            event( new ImageWasDeleted( $image ) );
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
            /** @scrutinizer ignore-call */ event( new ImageWasDeleted( $image ) );
Loading history...
111
112
            return TRUE;
113
        }
114
115
        return $this->imageValidator->errors();
116
    }
117
118
119
    /**
120
     * @param mixed $imageIdOrImage
121
     * @param array $options
122
     * @return array
123
     */
124
    public function processImage( $imageIdOrImage, array $options = [] )
125
    {
126
        $image = $this->getInstance( $imageIdOrImage );
127
128
        if ( !is_array( $image ) && !$image->processed )
129
        {
130
            $sizes = @$options['sizes'];
131
            $sizes = is_array( $sizes ) ? $sizes : [];
132
133
            $finalThumbs = [];
134
            $destinationPath = dirname( $image->path );
135
            foreach( $sizes as $sizeKey => $size )
136
            {
137
                $thumbs = $this->imageProcessingService->resizeOrCropImageToSizes( $image->path, $destinationPath, [ $size ], $options );
138
139 View Code Duplication
                if ( $thumbs && !empty( $thumbs ) )
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...
140
                {
141
                    $thumb =  array_first( $thumbs );
142
                    $finalThumbs[ $sizeKey ] = $thumb;
143
                }
144
            }
145
146
            // Update the file.
147
            $update = [
148
                'thumbnails' => json_encode( $finalThumbs ),
149
                'processed' => TRUE
150
            ];
151
152
            $image->update( $update );
153
154
            foreach( $update as $key => $value ) $image->{$key} = $value;
155
156
            event( new ImageWasProcessed( $image ) );
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
            /** @scrutinizer ignore-call */ event( new ImageWasProcessed( $image ) );
Loading history...
157
158
        }
159
160
        return $image;
161
162
    }
163
164
165
    /**
166
     * @param $imageIdOrImage
167
     * @return array|bool
168
     */
169
    public function destroyImage( $imageIdOrImage )
170
    {
171
        $isImage = $imageIdOrImage instanceof Image;
172
173
        $data = [
174
            'id' => $isImage ? $imageIdOrImage : $imageIdOrImage->id
175
        ];
176
177
        if ( $isImage || $this->imageValidator->with( $data )->passes( ImageValidatorInterface::EXISTS_BY_ID_EVEN_DELETED ) )
178
        {
179
            $image = $isImage ? $imageIdOrImage : Image::find( $imageIdOrImage )->withTrashed();
180
181
            // Remove from local disk.
182
            $this->removeImageFromDisk( $image, config( 'images.local_disk_name', 'local' ) );
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $image of OkayBueno\Images\Service...::removeImageFromDisk() does only seem to accept OkayBueno\Images\Models\Image, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

182
            $this->removeImageFromDisk( /** @scrutinizer ignore-type */ $image, config( 'images.local_disk_name', 'local' ) );
Loading history...
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
            $this->removeImageFromDisk( $image, /** @scrutinizer ignore-call */ config( 'images.local_disk_name', 'local' ) );
Loading history...
183
184
            // If it's in the cloud then remove it from the cloud too.
185
            $cloudDiskName = config( 'images.cloud_disk_name' );
186
            if ( $image->processed && $cloudDiskName )
0 ignored issues
show
Bug introduced by
The property processed does not seem to exist on Illuminate\Database\Eloquent\Builder.
Loading history...
187
            {
188
                $this->removeImageFromDisk( $image, $cloudDiskName );
189
            }
190
191
            // Destroy the entry.
192
            $image->forceDelete();
193
194
            return TRUE;
195
        }
196
197
        return $this->imageValidator->errors();
198
    }
199
200
201
    /*******************************************************************************************************************
202
     *******************************************************************************************************************
203
     ******************************************************************************************************************/
204
    /**
205
     * @param Image $image
206
     * @param $diskName
207
     */
208
    protected function removeImageFromDisk( Image $image, $diskName )
209
    {
210
        if ( $diskName )
211
        {
212
            $disk = Storage::disk( $diskName );
213
214
            // Delete all thumbnails locally.
215 View Code Duplication
            foreach( $image->thumbnails( NULL, TRUE ) as $thumb )
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...
216
            {
217
                $folderName = dirname( $thumb );
218
                if ( $disk->exists( $folderName ) ) $disk->deleteDirectory( $folderName );
219
            }
220
221
            // Also remove the main pic.
222
            if ( $disk->exists( $image->path ) ) $disk->delete( $image->path );
0 ignored issues
show
Bug introduced by
The property path does not seem to exist on OkayBueno\Images\Models\Image. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
223
        }
224
    }
225
226
    /**
227
     * @param $imageOrImageId
228
     * @return array
229
     */
230
    protected function getInstance( $imageOrImageId )
231
    {
232
        $isImage = $imageOrImageId instanceof Image;
233
234
        return $isImage ? $imageOrImageId : $this->findImageById( $imageOrImageId->id );
0 ignored issues
show
Bug Best Practice introduced by
The expression return $isImage ? $image...Id($imageOrImageId->id) also could return the type OkayBueno\Images\Models\Image which is incompatible with the documented return type array.
Loading history...
235
236
    }
237
238
}