ContentService   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 120
c 1
b 0
f 0
dl 0
loc 263
rs 9.2
wmc 40

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getImageModel() 0 3 1
A selectAllGroups() 0 3 1
A findGroup() 0 3 1
A updateGroupById() 0 11 2
A rulesGroup() 0 12 2
A destroy() 0 15 3
B createImage() 0 53 7
A updateImageEntity() 0 8 2
A destroyGroup() 0 6 1
A destroyImage() 0 20 5
A createGroup() 0 15 2
A updateGroupEntity() 0 8 2
A updateImageById() 0 5 1
A getGroupModel() 0 3 1
A rules() 0 17 3
A create() 0 13 2
A updateById() 0 16 3
A __construct() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like ContentService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ContentService, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\Content;
4
5
use Validator;
6
use File;
7
use Hideyo\Ecommerce\Framework\Services\Content\Entity\ContentRepository;
8
use Hideyo\Ecommerce\Framework\Services\BaseService;
9
 
10
class ContentService extends BaseService
11
{
12
	public function __construct(ContentRepository $taxRate)
13
	{
14
		$this->repo = $taxRate;
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...
15
	} 
16
17
    /**
18
     * The validation rules for the model.
19
     *
20
     * @param  integer  $id id attribute model    
21
     * @return array
22
     */
23
    public function rules($contentId = false, $attributes = false)
24
    {
25
        if (isset($attributes['seo'])) {
26
            $rules = array(
27
                'meta_title'                 => 'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id'
28
            );
29
        } else {
30
            $rules = array(
31
                'title'                 => 'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id'
32
            );
33
            
34
            if ($contentId) {
35
                $rules['title'] =   'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id, '.$contentId.' = id';
36
            }
37
        }
38
39
        return $rules;
40
    }
41
42
    public function create(array $attributes)
43
    {
44
        $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...
45
        $validator = Validator::make($attributes, $this->rules());
46
47
        if ($validator->fails()) {
48
            return $validator;
49
        }
50
51
        $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...
52
        $this->repo->getModel()->fill($attributes);
53
        $this->repo->getModel()->save();
54
        return $this->repo->getModel();
55
    }
56
57
    public function updateById(array $attributes, $id)
58
    {
59
        $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...
60
        $validator = Validator::make($attributes, $this->rules($id));
61
        if ($validator->fails()) {
62
            return $validator;
63
        }
64
65
        $model = $this->find($id);
66
        $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...
67
68
        if (count($attributes) > 0) {
69
            $model->fill($attributes);
70
            $model->save();
71
        }
72
        return $model;  
73
    } 
74
75
    public function destroy($newsId)
76
    {
77
        $this->model = $this->find($newsId);
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...
78
        $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

78
        $this->model->/** @scrutinizer ignore-call */ 
79
                      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...
79
80
        if ($this->model->contentImages()->count()) {
81
            foreach ($this->model->contentImages()->get() as $image) {
82
                $this->contentImage->destroy($image->id);
0 ignored issues
show
Bug Best Practice introduced by
The property contentImage does not exist on Hideyo\Ecommerce\Framewo...\Content\ContentService. Did you maybe forget to declare it?
Loading history...
83
            }
84
        }
85
86
        $directory = app_path() . "/storage/files/".$this->model->shop_id."/content/".$this->model->id;
87
        File::deleteDirectory($directory);
88
89
        return $this->model->delete();
90
    }
91
92
    /**
93
     * The validation rules for the modelGroup.
94
     *
95
     * @return array
96
     */
97
    public function rulesGroup($contentGroupId = 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

97
    public function rulesGroup($contentGroupId = 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...
98
    {
99
        $rules = array(
100
            'title'                 => 'required|between:4,65|unique:'.$this->repo->getGroupModel()->getTable().''
101
        );
102
        
103
        if ($contentGroupId) {
104
            $rules['title'] =   'required|between:4,65|unique:'.$this->repo->getGroupModel()->getTable().',title,'.$contentGroupId;
105
        }
106
107
108
        return $rules;
109
    }
110
111
112
    public function createGroup(array $attributes)
113
    {
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->rulesGroup());
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->repo->getGroupModel()->fill($attributes);
124
        $this->repo->getGroupModel()->save();
125
   
126
        return $this->repo->getGroupModel();
127
    }
128
129
    public function createImage(array $attributes, $contentId)
130
    {
131
        $userId = 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...
132
        $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...
133
        $shop = ShopService::find($shopId);
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...ces\Content\ShopService 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...
134
135
        $rules = array(
136
            'file'=>'required|image|max:1000',
137
            'rank' => 'required'
138
        );
139
140
        $validator = Validator::make($attributes, $rules);
141
142
        if ($validator->fails()) {
143
            return $validator;
144
        }
145
146
        $attributes['modified_by_user_id'] = $userId;
147
148
        $destinationPath = storage_path() . "/app/files/content/".$contentId;
149
        $attributes['user_id'] = $userId;
150
        $attributes['content_id'] = $contentId;
151
        $attributes['extension'] = $attributes['file']->getClientOriginalExtension();
152
        $attributes['size'] = $attributes['file']->getSize();
153
        
154
        $filename =  str_replace(" ", "_", strtolower($attributes['file']->getClientOriginalName()));
155
        $uploadSuccess = $attributes['file']->move($destinationPath, $filename);
156
157
        if ($uploadSuccess) {
158
            $attributes['file'] = $filename;
159
            $attributes['path'] = $uploadSuccess->getRealPath();
160
     
161
            $this->modelImage->fill($attributes);
162
            $this->modelImage->save();
163
164
            if ($shop->square_thumbnail_sizes) {
165
                $sizes = explode(',', $shop->square_thumbnail_sizes);
166
                if ($sizes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sizes of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
167
                    foreach ($sizes as $valueSize) {
168
                        $image = Image::make($uploadSuccess->getRealPath());
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framework\Services\Content\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...
169
                        $explode = explode('x', $valueSize);
170
                        $image->resize($explode[0], $explode[1]);
171
                        $image->interlace();
172
173
                        if (!File::exists(public_path() . "/files/content/".$valueSize."/".$contentId."/")) {
174
                            File::makeDirectory(public_path() . "/files/content/".$valueSize."/".$contentId."/", 0777, true);
175
                        }
176
                        $image->save(public_path() . "/files/content/".$valueSize."/".$contentId."/".$filename);
177
                    }
178
                }
179
            }
180
            
181
            return $this->modelImage;
182
        }
183
    }
184
185
    public function updateGroupById(array $attributes, $newsGroupId)
186
    {
187
        $validator = Validator::make($attributes, $this->rulesGroup($newsGroupId, $attributes));
188
189
        if ($validator->fails()) {
190
            return $validator;
191
        }
192
193
        $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...
194
        $this->modelGroup = $this->findGroup($newsGroupId);
0 ignored issues
show
Bug Best Practice introduced by
The property modelGroup does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
195
        return $this->updateGroupEntity($attributes);
196
    }
197
198
    private function updateGroupEntity(array $attributes = array())
199
    {
200
        if (count($attributes) > 0) {
201
            $this->modelGroup->fill($attributes);
202
            $this->modelGroup->save();
203
        }
204
205
        return $this->modelGroup;
206
    }
207
208
    public function updateImageById(array $attributes, $contentId, $newsImageId)
0 ignored issues
show
Unused Code introduced by
The parameter $contentId 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

208
    public function updateImageById(array $attributes, /** @scrutinizer ignore-unused */ $contentId, $newsImageId)

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...
209
    {
210
        $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...
211
        $this->modelImage = $this->find($newsImageId);
0 ignored issues
show
Bug Best Practice introduced by
The property modelImage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
212
        return $this->updateImageEntity($attributes);
213
    }
214
215
    private function updateImageEntity(array $attributes = array())
216
    {
217
        if (count($attributes) > 0) {
218
            $this->modelImage->fill($attributes);
219
            $this->modelImage->save();
220
        }
221
222
        return $this->modelImage;
223
    }
224
225
    public function destroyImage($newsImageId)
226
    {
227
        $this->modelImage = $this->findImage($newsImageId);
0 ignored issues
show
Bug introduced by
The method findImage() does not exist on Hideyo\Ecommerce\Framewo...\Content\ContentService. ( Ignorable by Annotation )

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

227
        /** @scrutinizer ignore-call */ 
228
        $this->modelImage = $this->findImage($newsImageId);

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...
Bug Best Practice introduced by
The property modelImage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
228
        $filename = storage_path() ."/app/files/content/".$this->modelImage->content_id."/".$this->modelImage->file;
229
        $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...
230
        $shop = ShopService::find($shopId);
231
232
        if (File::exists($filename)) {
233
            File::delete($filename);
234
            if ($shop->square_thumbnail_sizes) {
235
                $sizes = explode(',', $shop->square_thumbnail_sizes);
236
                if ($sizes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sizes of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
237
                    foreach ($sizes as $valueSize) {
238
                        File::delete(public_path() . "/files/content/".$valueSize."/".$this->modelImage->content_id."/".$this->modelImage->file);
239
                    }
240
                }
241
            }
242
        }
243
244
        return $this->modelImage->delete();
245
    }
246
247
    public function destroyGroup($newsGroupId)
248
    {
249
        $this->modelGroup = $this->findGroup($newsGroupId);
0 ignored issues
show
Bug Best Practice introduced by
The property modelGroup does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
250
        $this->modelGroup->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

250
        $this->modelGroup->/** @scrutinizer ignore-call */ 
251
                           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...
251
252
        return $this->modelGroup->delete();
253
    }
254
255
    public function getGroupModel()
256
    {
257
        return $this->repo->getGroupModel();
258
    }
259
260
    public function findGroup($id)
261
    {
262
        return $this->repo->findGroup($id);
263
    }
264
265
    public function selectAllGroups()
266
    {
267
        return $this->repo->selectAllGroups();
268
    }
269
270
    public function getImageModel()
271
    {
272
        return $this->repo->getImageModel();
273
    }
274
}