|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hideyo\Ecommerce\Framework\Services\Shop; |
|
4
|
|
|
|
|
5
|
|
|
use Validator; |
|
|
|
|
|
|
6
|
|
|
use File; |
|
7
|
|
|
use Hideyo\Ecommerce\Framework\Services\Shop\Entity\ShopRepository; |
|
8
|
|
|
use Hideyo\Ecommerce\Framework\Services\BaseService; |
|
9
|
|
|
|
|
10
|
|
|
class ShopService extends BaseService |
|
11
|
|
|
{ |
|
12
|
|
|
public function __construct(ShopRepository $shop) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->repo = $shop; |
|
|
|
|
|
|
15
|
|
|
$this->storageImagePath = storage_path() .config('hideyo.storage_path'). "/shop/"; |
|
|
|
|
|
|
16
|
|
|
$this->publicImagePath = public_path() .config('hideyo.public_path'). "/shop/"; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The validation rules for the model. |
|
22
|
|
|
* |
|
23
|
|
|
* @param integer $shopId id attribute model |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
private function rules($shopId = false) |
|
27
|
|
|
{ |
|
28
|
|
|
$rules = array( |
|
29
|
|
|
'title' => 'required|between:4,65|unique:'.$this->repo->getModel()->getTable(), |
|
30
|
|
|
'active' => 'required' |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
if ($shopId) { |
|
|
|
|
|
|
34
|
|
|
$rules['title'] = $rules['title'].',title,'.$shopId; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $rules; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function create(array $attributes) |
|
41
|
|
|
{ |
|
42
|
|
|
$validator = Validator::make($attributes, $this->rules()); |
|
43
|
|
|
|
|
44
|
|
|
if ($validator->fails()) { |
|
45
|
|
|
return $validator; |
|
46
|
|
|
} |
|
47
|
|
|
$model = $this->updateOrAddModel($this->repo->getModel(), $attributes); |
|
48
|
|
|
|
|
49
|
|
|
if (isset($attributes['logo'])) { |
|
50
|
|
|
$destinationPath = $this->storageImagePath.$model->id; |
|
51
|
|
|
$filename = str_replace(" ", "_", strtolower($attributes['logo']->getClientOriginalName())); |
|
52
|
|
|
$upload_success = $attributes['logo']->move($destinationPath, $filename); |
|
53
|
|
|
|
|
54
|
|
|
$attributes['logo_file_name'] = $filename; |
|
55
|
|
|
$attributes['logo_file_path'] = $upload_success->getRealPath(); |
|
56
|
|
|
$model = $this->updateOrAddModel($model, $attributes); |
|
57
|
|
|
|
|
58
|
|
|
if (File::exists($model->logo_file_path)) { |
|
59
|
|
|
if (!File::exists($this->publicImagePath.$model->id)) { |
|
60
|
|
|
File::makeDirectory($this->publicImagePath.$model->id, 0777, true); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!File::exists($this->publicImagePath.$model->id."/".$model->logo_file_name)) { |
|
64
|
|
|
$image = Image::make($model->logo_file_path); |
|
|
|
|
|
|
65
|
|
|
$image->interlace(); |
|
66
|
|
|
$image->save($this->publicImagePath.$model->id."/".$model->logo_file_name); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $model; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function updateById(array $attributes, $shopId) |
|
75
|
|
|
{ |
|
76
|
|
|
$validator = Validator::make($attributes, $this->rules($shopId)); |
|
77
|
|
|
|
|
78
|
|
|
if ($validator->fails()) { |
|
79
|
|
|
return $validator; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$model = $this->find($shopId); |
|
83
|
|
|
return $this->updateEntity($model, $attributes); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function updateEntity($model, array $attributes = array()) |
|
87
|
|
|
{ |
|
88
|
|
|
if (count($attributes) > 0) { |
|
89
|
|
|
if (isset($attributes['logo'])) { |
|
90
|
|
|
File::delete($model->logo_file_path); |
|
91
|
|
|
$destinationPath = $this->storageImagePath.$model->id; |
|
92
|
|
|
|
|
93
|
|
|
$filename = str_replace(" ", "_", strtolower($attributes['logo']->getClientOriginalName())); |
|
94
|
|
|
$upload_success = $attributes['logo']->move($destinationPath, $filename); |
|
95
|
|
|
|
|
96
|
|
|
$attributes['logo_file_name'] = $filename; |
|
97
|
|
|
$attributes['logo_file_path'] = $upload_success->getRealPath(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
$model->slug = null; |
|
102
|
|
|
$model->fill($attributes); |
|
103
|
|
|
$model->save(); |
|
104
|
|
|
|
|
105
|
|
|
if (File::exists($model->logo_file_path)) { |
|
106
|
|
|
if (!File::exists($this->publicImagePath.$model->id)) { |
|
107
|
|
|
File::makeDirectory($this->publicImagePath.$model->id, 0777, true); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (!File::exists($this->publicImagePath.$model->id."/".$model->logo_file_name)) { |
|
111
|
|
|
$image = Image::make($model->logo_file_path); |
|
112
|
|
|
$image->interlace(); |
|
113
|
|
|
$image->save($this->publicImagePath.$model->id."/".$model->logo_file_name); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $model; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function destroy($shopId) |
|
122
|
|
|
{ |
|
123
|
|
|
$model = $this->find($shopId); |
|
124
|
|
|
|
|
125
|
|
|
File::deleteDirectory($this->publicImagePath.$model->id); |
|
126
|
|
|
$destinationPath = $this->storageImagePath.$model->id; |
|
127
|
|
|
File::deleteDirectory($destinationPath); |
|
128
|
|
|
|
|
129
|
|
|
$model->save(); |
|
130
|
|
|
|
|
131
|
|
|
return $model->delete(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function findUrl($shopUrl) |
|
135
|
|
|
{ |
|
136
|
|
|
$result = $this->repo->findUrl($shopUrl); |
|
137
|
|
|
|
|
138
|
|
|
if (isset($result->id)) { |
|
139
|
|
|
return $result; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths