ShopService   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 63
c 4
b 0
f 0
dl 0
loc 148
rs 10
wmc 20

7 Methods

Rating   Name   Duplication   Size   Complexity  
A findUrl() 0 9 2
A create() 0 32 6
A destroy() 0 11 1
A __construct() 0 5 1
A updateEntity() 0 33 6
A rules() 0 12 2
A updateById() 0 10 2
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\Shop;
4
5
use Validator;
6
use File;
7
use Image;
8
use Hideyo\Ecommerce\Framework\Services\Shop\Entity\ShopRepository;
9
use Hideyo\Ecommerce\Framework\Services\BaseService;
10
 
11
class ShopService extends BaseService
12
{
13
    public function __construct(ShopRepository $shop)
14
    {
15
        $this->repo = $shop;
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...
16
        $this->storageImagePath = storage_path() .config('hideyo.storage_path'). "/shop/";
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...
17
        $this->publicImagePath = public_path() .config('hideyo.public_path'). "/shop/";
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...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $shopId 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'] =   $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
    /**
75
     * update model by id
76
     * @param  array  $attributes 
77
     * @param  integer $shopId     
78
     * @return midex             
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framework\Services\Shop\midex 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...
79
     */
80
    public function updateById(array $attributes, $shopId)
81
    {
82
        $validator = Validator::make($attributes, $this->rules($shopId));
83
84
        if ($validator->fails()) {
85
            return $validator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $validator returns the type Illuminate\Contracts\Validation\Validator which is incompatible with the documented return type Hideyo\Ecommerce\Framework\Services\Shop\midex.
Loading history...
86
        }
87
88
        $model = $this->find($shopId);
89
        return $this->updateEntity($model, $attributes);
90
    }
91
92
    public function updateEntity($model, array $attributes = array())
93
    {
94
        if (count($attributes) > 0) {
95
            if (isset($attributes['logo'])) {
96
                File::delete($model->logo_file_path);
97
                $destinationPath = $this->storageImagePath.$model->id;
98
99
                $filename =  str_replace(" ", "_", strtolower($attributes['logo']->getClientOriginalName()));
100
                $upload_success = $attributes['logo']->move($destinationPath, $filename);
101
102
                $attributes['logo_file_name'] = $filename;
103
                $attributes['logo_file_path'] = $upload_success->getRealPath();
104
            }
105
106
107
            $model->slug = null;
108
            $model->fill($attributes);
109
            $model->save();
110
111
            if (File::exists($model->logo_file_path)) {
112
                if (!File::exists($this->publicImagePath.$model->id)) {
113
                    File::makeDirectory($this->publicImagePath.$model->id, 0777, true);
114
                }
115
116
                if (!File::exists($this->publicImagePath.$model->id."/".$model->logo_file_name)) {
117
                    $image = Image::make($model->logo_file_path);
118
                    $image->interlace();
119
                    $image->save($this->publicImagePath.$model->id."/".$model->logo_file_name);
120
                }
121
            }
122
        }
123
124
        return $model;
125
    }
126
127
    /**
128
     * destroy model
129
     * @param  integer $shopId 
130
     * @return object      
131
     */
132
    public function destroy($shopId)
133
    {
134
        $model = $this->find($shopId);
135
136
        File::deleteDirectory($this->publicImagePath.$model->id);
137
        $destinationPath = $this->storageImagePath.$model->id;
138
        File::deleteDirectory($destinationPath);
139
        
140
        $model->save();
141
142
        return $model->delete();
143
    }
144
145
    /**
146
     * find model by url
147
     * @param  string $shopUrl 
148
     * @return mixed
149
     */
150
    public function findUrl($shopUrl)
151
    {
152
        $result = $this->repo->findUrl($shopUrl);
153
154
        if (isset($result->id)) {
155
            return $result;
156
        }
157
        
158
        return false;      
159
    }
160
}