1
|
|
|
<?php |
2
|
|
|
namespace CbCaio\ImgAttacher; |
3
|
|
|
|
4
|
|
|
use CbCaio\ImgAttacher\Contracts\AttacherImageContract; |
5
|
|
|
use CbCaio\ImgAttacher\Managers\FileManager; |
6
|
|
|
use CbCaio\ImgAttacher\Managers\FilePathManager; |
7
|
|
|
use CbCaio\ImgAttacher\Processors\ImageProcessor; |
8
|
|
|
use Intervention\Image\Image; |
9
|
|
|
|
10
|
|
|
class AttacherManager |
11
|
|
|
{ |
12
|
|
|
protected $processing_styles_routine; |
13
|
|
|
protected $base_url; |
14
|
|
|
protected $path_to_save; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->processing_styles_routine = app('config')->get('img-attacher')['processing_styles_routines']; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public function writeImage(AttacherImageContract $attacherImage) |
25
|
|
|
{ |
26
|
|
|
$imageProcessor = $this->getImageProcessor(); |
27
|
|
|
|
28
|
|
|
$images_to_save = $imageProcessor->applyStyles($attacherImage, $this->getProcessingStylesRoutines()); |
29
|
|
|
|
30
|
|
|
$needToUpdate = $attacherImage->hasDifferentFileName(); |
31
|
|
|
|
32
|
|
|
if ($needToUpdate) |
33
|
|
|
{ |
34
|
|
|
$this->getFileManager()->updateMany($images_to_save, $attacherImage); |
|
|
|
|
35
|
|
|
} else |
36
|
|
|
{ |
37
|
|
|
$this->getFileManager()->saveMany($images_to_save, $attacherImage); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return TRUE; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param AttacherImageContract $attacherImage |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function deleteImages(AttacherImageContract $attacherImage, $style = NULL) |
48
|
|
|
{ |
49
|
|
|
$path = $attacherImage->getDeletePath($style); |
50
|
|
|
|
51
|
|
|
return $this->getFileManager()->delete($path); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return FileManager |
56
|
|
|
*/ |
57
|
|
|
public function getFileManager() |
58
|
|
|
{ |
59
|
|
|
return app('img-attacher.FileManager'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return ImageProcessor |
64
|
|
|
*/ |
65
|
|
|
public function getImageProcessor() |
66
|
|
|
{ |
67
|
|
|
return app('img-attacher.ImageProcessor'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return FilePathManager |
72
|
|
|
*/ |
73
|
|
|
public function getFilePathManager() |
74
|
|
|
{ |
75
|
|
|
return app('img-attacher.FilePathManager'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function getProcessingStylesRoutines() |
82
|
|
|
{ |
83
|
|
|
return $this->processing_styles_routine; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getBaseUrl() |
90
|
|
|
{ |
91
|
|
|
return $this->getFilePathManager()->getBaseUrl(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getPathToSave() |
98
|
|
|
{ |
99
|
|
|
return $this->getFilePathManager()->getPath(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: