HtmlBlockService   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 122
c 1
b 0
f 0
dl 0
loc 262
rs 9.28
wmc 39

13 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 11 2
B updateEntity() 0 60 10
A updateById() 0 11 2
A findByPosition() 0 9 2
A changeActive() 0 23 3
A selectOneByShopIdAndPosition() 0 2 1
A createCopy() 0 18 2
A destroy() 0 11 1
C create() 0 69 12
A __construct() 0 5 1
A selectAllActiveGroupsByShopId() 0 3 1
A selectByLimitAndOrderBy() 0 3 1
A selectOneBySlug() 0 3 1
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\HtmlBlock;
4
5
use Validator;
6
use File;
7
use DbView;
0 ignored issues
show
Bug introduced by
The type DbView was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Hideyo\Ecommerce\Framework\Services\HtmlBlock\Entity\HtmlBlockRepository;
9
use Hideyo\Ecommerce\Framework\Services\BaseService;
10
use Hideyo\Ecommerce\Framework\Services\Shop\ShopFacade as ShopService;
11
 
12
class HtmlBlockService extends BaseService
13
{
14
	public function __construct(HtmlBlockRepository $htmlBlock)
15
	{
16
		$this->repo = $htmlBlock;
0 ignored issues
show
Bug Best Practice introduced by
The property repo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        $this->storageImagePath = storage_path() .config('hideyo.storage_path'). "/html_block/";
0 ignored issues
show
Bug Best Practice introduced by
The property storageImagePath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
        $this->publicImagePath = public_path() .config('hideyo.public_path'). "/html_block/";
0 ignored issues
show
Bug Best Practice introduced by
The property publicImagePath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
	} 
20
21
   /**
22
     * The validation rules for the model.
23
     *
24
     * @param  integer  $htmlBlockId id attribute model    
25
     * @return array
26
     */
27
    public function rules($htmlBlockId = false, $attributes = false)
0 ignored issues
show
Unused Code introduced by
The parameter $attributes is not used and could be removed. ( Ignorable by Annotation )

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

27
    public function rules($htmlBlockId = false, /** @scrutinizer ignore-unused */ $attributes = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        $rules = array(
30
            'title'                 => 'required|between:4,65'
31
        );
32
        
33
        if ($htmlBlockId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $htmlBlockId of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
34
            $rules['title'] =   'required|between:4,65';
35
        }
36
  
37
        return $rules;
38
    }
39
  
40
    public function create(array $attributes)
41
    {
42
        $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id;
0 ignored issues
show
Bug introduced by
Accessing selected_shop_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
43
        $validator = Validator::make($attributes, $this->rules());
44
45
        if ($validator->fails()) {
46
            return $validator;
47
        }
48
49
        $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;            
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
50
      
51
        $this->model->fill($attributes);
52
        $this->model->save();
53
54
        if (isset($attributes['image'])) {
55
            $attributes['image_file_extension'] = $attributes['image']->getClientOriginalExtension();
56
            $attributes['image_file_size'] = $attributes['image']->getSize();
57
58
            $rules = array(
59
                'file'=>'image|max:1000'
60
            );
61
62
            $validator = Validator::make($attributes, $rules);
63
64
            if ($validator->fails()) {
65
                return $validator;
66
            } else {
67
                $destinationPath = $this->storageImagePath.$this->model->id;
68
                $filename =  str_replace(" ", "_", strtolower($attributes['image']->getClientOriginalName()));
69
                $uploadSuccess = $attributes['image']->move($destinationPath, $filename);
70
71
                if ($uploadSuccess) {
72
                    $attributes['image_file_name'] = $filename;
73
                    $attributes['image_file_path'] = $uploadSuccess->getRealPath();
74
75
                    $this->model->fill($attributes);
76
                    $this->model->save();
77
78
                    if ($this->model->thumbnail_height and $this->model->thumbnail_width) {
79
                        $image = Image::make($uploadSuccess->getRealPath());
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...ervices\HtmlBlock\Image was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
               
81
                        $image->resize($this->model->thumbnail_width, $this->model->thumbnail_height);
82
                        $image->interlace();
83
84
                        if (!File::exists($this->publicImagePath.$this->model->id."/")) {
85
                            File::makeDirectory($this->publicImagePath.$this->model->id."/", 0777, true);
86
                        }
87
                        $image->save($this->publicImagePath.$this->model->id."/".$filename);
88
                    }
89
                }
90
            }
91
        }
92
93
94
        if ($this->model->thumbnail_height and $this->model->thumbnail_width and $this->model->image_file_name) {
95
            File::deleteDirectory($this->publicImagePath.$this->model->id."/");
96
            $image = Image::make($this->model->image_file_path);
97
   
98
            $image->resize($this->model->thumbnail_width, $this->model->thumbnail_height);
99
            $image->interlace();
100
101
            if (!File::exists($this->publicImagePath.$this->model->id."/")) {
102
                File::makeDirectory($this->publicImagePath.$this->model->id."/", 0777, true);
103
            }
104
            $image->save($this->publicImagePath.$this->model->id."/".$this->model->image_file_name);
105
        }
106
107
   
108
        return $this->model;
109
    }
110
111
    public function createCopy(array $attributes, $htmlBlockId)
112
    {
113
        $product =  $this->find($htmlBlockId);
0 ignored issues
show
Unused Code introduced by
The assignment to $product is dead and can be removed.
Loading history...
114
        $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id;
0 ignored issues
show
Bug introduced by
Accessing selected_shop_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
115
        $validator = Validator::make($attributes, $this->rules());
116
117
        if ($validator->fails()) {
118
            return $validator;
119
        }
120
   
121
        $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
122
123
        $this->model->sluggify();
124
        $this->model->fill($attributes);
125
126
        $this->model->save();
127
    
128
        return $this->model;
129
    }
130
131
    public function changeActive($htmlBlockId)
132
    {
133
        $this->model = $this->find($htmlBlockId);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
134
135
        if ($this->model) {
136
            $active = 1;
137
            
138
            if ($this->model->active) {
139
                $active = 0;
140
            }
141
142
            $attributes = array(
143
                'active' => $active
144
            );
145
146
            $this->model->fill($attributes);
147
148
            $this->model->sluggify();
149
150
            return $this->model->save();
151
        }
152
153
        return false;
154
    }
155
156
157
158
    public function updateById(array $attributes, $htmlBlockId)
159
    {
160
        $validator = Validator::make($attributes, $this->rules($htmlBlockId, $attributes));
161
162
        if ($validator->fails()) {
163
            return $validator;
164
        }
165
166
        $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
167
        $this->model = $this->find($htmlBlockId);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
168
        return $this->updateEntity($attributes);
169
    }
170
171
    public function updateEntity(array $attributes = array())
172
    {
173
174
        $shopId = auth('hideyobackend')->user()->selected_shop_id;
0 ignored issues
show
Bug introduced by
Accessing selected_shop_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
175
        $shop = ShopService::find($shopId);
0 ignored issues
show
Unused Code introduced by
The assignment to $shop is dead and can be removed.
Loading history...
176
177
        if (count($attributes) > 0) {
178
            $this->model->fill($attributes);
179
            $this->model->save();
180
        }
181
182
183
        if (isset($attributes['image'])) {
184
            $attributes['image_file_extension'] = $attributes['image']->getClientOriginalExtension();
185
            $attributes['image_file_size'] = $attributes['image']->getSize();
186
187
188
            $rules = array(
189
                'file'=>'image|max:1000'
190
            );
191
192
            $validator = Validator::make($attributes, $rules);
193
194
            if ($validator->fails()) {
195
                return $validator;
196
            } else {
197
                $destinationPath = $this->storageImagePath.$this->model->id;
198
                $filename =  str_replace(" ", "_", strtolower($attributes['image']->getClientOriginalName()));
199
                File::deleteDirectory($destinationPath);
200
201
                $uploadSuccess = $attributes['image']->move($destinationPath, $filename);
202
203
                if ($uploadSuccess) {
204
                    $attributes['image_file_name'] = $filename;
205
                    $attributes['image_file_path'] = $uploadSuccess->getRealPath();
206
207
                    $this->model->fill($attributes);
208
                    $this->model->save();
209
                }
210
            }
211
        }
212
 
213
        if (File::exists($this->model->image_file_path) AND $this->model->thumbnail_height and $this->model->thumbnail_width and $this->model->image_file_name) {
214
            $image = Image::make($this->model->image_file_path);
215
   
216
217
            File::deleteDirectory($this->publicImagePath.$this->model->id);
218
219
220
            $image->resize($this->model->thumbnail_width, $this->model->thumbnail_height);
221
            $image->interlace();
222
223
            if (!File::exists($this->publicImagePath.$this->model->id."/")) {
224
                File::makeDirectory($this->publicImagePath.$this->model->id."/", 0777, true);
225
            }
226
            $image->save($this->publicImagePath.$this->model->id."/".$this->model->image_file_name);
227
        }
228
229
230
        return $this->model;
231
    }
232
233
    public function destroy($htmlBlockId)
234
    {
235
        $this->model = $this->find($htmlBlockId);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
236
        File::deleteDirectory($this->publicImagePath.$this->model->id);
237
238
        $destinationPath = $this->storageImagePath.$this->model->id;
239
240
        File::deleteDirectory($destinationPath);
241
        $this->model->save();
0 ignored issues
show
Bug introduced by
The method save() does not exist on null. ( Ignorable by Annotation )

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

241
        $this->model->/** @scrutinizer ignore-call */ 
242
                      save();

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...
242
243
        return $this->model->delete();
244
    }
245
246
    public function selectByLimitAndOrderBy($shopId, $limit, $orderBy)
247
    {
248
    	return $this->repo->selectByLimitAndOrderBy($shopId, $limit, $orderBy);
0 ignored issues
show
Bug introduced by
The method selectByLimitAndOrderBy() does not exist on Hideyo\Ecommerce\Framewo...ity\HtmlBlockRepository. ( Ignorable by Annotation )

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

248
    	return $this->repo->/** @scrutinizer ignore-call */ selectByLimitAndOrderBy($shopId, $limit, $orderBy);

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...
249
    }
250
251
    public function selectOneBySlug($shopId, $slug)
252
    {
253
    	return $this->repo->selectOneBySlug($shopId, $slug);
0 ignored issues
show
Bug introduced by
The method selectOneBySlug() does not exist on Hideyo\Ecommerce\Framewo...ity\HtmlBlockRepository. Did you maybe mean selectOneByShopIdAndSlug()? ( Ignorable by Annotation )

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

253
    	return $this->repo->/** @scrutinizer ignore-call */ selectOneBySlug($shopId, $slug);

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...
254
    }
255
256
    public function selectAllActiveGroupsByShopId($shopId)
257
    {
258
    	return $this->repo->selectAllActiveGroupsByShopId($shopId);
0 ignored issues
show
Bug introduced by
The method selectAllActiveGroupsByShopId() does not exist on Hideyo\Ecommerce\Framewo...ity\HtmlBlockRepository. Did you maybe mean selectAll()? ( Ignorable by Annotation )

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

258
    	return $this->repo->/** @scrutinizer ignore-call */ selectAllActiveGroupsByShopId($shopId);

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...
259
    }
260
261
    public function selectOneByShopIdAndPosition($position, $shopId) {
262
        return $this->repo->selectOneByShopIdAndPosition($position, $shopId);
263
    }
264
265
    public function findByPosition($position)
266
    {
267
        $result = $this->repo->selectOneByShopIdAndPosition($position, config()->get('app.shop_id'));
268
        
269
        if ($result) {
270
            return DbView::make($result)->with($result->toArray())->render();
271
        }
272
        
273
        return '';        
274
    }
275
}