BrandService::getModelImage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\Brand;
4
5
use Validator;
6
use Image;
7
use File;
8
use Hideyo\Ecommerce\Framework\Services\Brand\Entity\BrandRepository;
9
use Hideyo\Ecommerce\Framework\Services\BaseService;
10
use Hideyo\Ecommerce\Framework\Services\Shop\ShopFacade as ShopService;
11
 
12
class BrandService extends BaseService
13
{
14
	public function __construct(BrandRepository $brand)
15
	{
16
		$this->repo = $brand;
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'). "/brand/";
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'). "/brand/";
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  $brandId id attribute model    
25
     * @return array
26
     */
27
    private function rules($brandId = false, $attributes = false)
28
    {
29
        if (isset($attributes['seo'])) {
30
            $rules = array(
31
                'meta_title' => 'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id'
32
            );
33
34
            return $rules;
35
        } 
36
37
        $rules = array(
38
            'title' => 'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id',
39
            'rank'  => 'required|integer'
40
        );
41
        
42
        if ($brandId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $brandId 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...
43
            $rules['title'] =   $rules['title'].', '.$brandId.' = id';
44
        }
45
     
46
        return $rules;
47
    }  
48
  
49
    public function create(array $attributes)
50
    {
51
        $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...
52
        $validator = Validator::make($attributes, $this->rules());
53
54
        if ($validator->fails()) {
55
            return $validator;
56
        }
57
58
        $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...
59
            
60
        $this->repo->getModel()->fill($attributes);
61
        $this->repo->getModel()->save();
62
   
63
        return $this->repo->getModel();
64
    }
65
66
    public function createImage(array $attributes, $brandId)
67
    {
68
        $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...
69
        $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...
70
        $shop = ShopService::find($shopId);
71
       
72
        $rules = array(
73
            'file'=>'required|image|max:1000',
74
            'rank' => 'required'
75
        );
76
77
        $validator = Validator::make($attributes, $rules);
78
79
        if ($validator->fails()) {
80
            return $validator;
81
        }
82
83
        $attributes['modified_by_user_id'] = $userId;
84
85
        $destinationPath = $this->storageImagePath.$brandId;
86
        $attributes['user_id'] = $userId;
87
        $attributes['brand_id'] = $brandId;
88
        $attributes['extension'] = $attributes['file']->getClientOriginalExtension();
89
        $attributes['size'] = $attributes['file']->getSize();
90
91
        $filename =  str_replace(" ", "_", strtolower($attributes['file']->getClientOriginalName()));
92
        $uploadSuccess = $attributes['file']->move($destinationPath, $filename);
93
94
        if ($uploadSuccess) {
95
            $attributes['file'] = $filename;
96
            $attributes['path'] = $uploadSuccess->getRealPath();
97
     
98
            $this->repo->getModelImage()->fill($attributes);
99
            $this->repo->getModelImage()->save();
100
101
            if ($shop->thumbnail_square_sizes) {
102
                $sizes = explode(',', $shop->thumbnail_square_sizes);
103
                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...
104
                    foreach ($sizes as $valueSize) {
105
                        $image = Image::make($uploadSuccess->getRealPath());
106
                        $explode = explode('x', $valueSize);
107
                        $image->resize($explode[0], $explode[1]);
108
                        $image->interlace();
109
110
                        if (!File::exists($this->publicImagePath.$valueSize."/".$brandId."/")) {
111
                            File::makeDirectory($this->publicImagePath.$valueSize."/".$brandId."/", 0777, true);
112
                        }
113
                        $image->save($this->publicImagePath.$valueSize."/".$brandId."/".$filename);
114
                    }
115
                }
116
            }
117
            
118
            return $this->repo->getModelImage();
119
        }  
120
    }
121
122
    public function updateById(array $attributes, $brandId)
123
    {
124
        $validator = Validator::make($attributes, $this->rules($brandId, $attributes));
125
        $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...
126
        if ($validator->fails()) {
127
            return $validator;
128
        }
129
130
        $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...
131
        $model = $this->find($brandId);
132
        $model->fill($attributes);
133
        $model->save;
134
        return $model;
135
    }
136
137
    public function updateImageById(array $attributes, $brandId, $imageId)
138
    {
139
        $this->model = $this->find($brandId);
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...
140
141
        if($this->model) {
142
            $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...
143
            $image = $this->findImage($imageId);
144
            $image->fill($attributes);
145
            $image->save();
146
147
            return $image;
148
          
149
        }
150
151
        return false;
152
    }
153
154
    public function destroy($brandId)
155
    {
156
        $this->model = $this->find($brandId);
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...
157
158
        // $url = $this->model->shop->url.route('brand.item', ['slug' => $this->model->slug], null);
159
        // $newUrl = $this->model->shop->url.route('brand.overview', array(), null);
160
        // $redirectResult = $this->redirect->create(array('active' => 1, 'url' => $url, 'redirect_url' => $newUrl, 'shop_id' => $this->model->shop_id));
161
162
        $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

162
        $this->model->/** @scrutinizer ignore-call */ 
163
                      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...
163
        return $this->model->delete();
164
    }
165
166
    public function destroyImage($imageId)
167
    {
168
        $image = $this->findImage($imageId);
169
        $filename = $this->storageImagePath.$image->brand_id."/".$image->file;
170
        $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...
171
        $shop = ShopService::find($shopId);
172
173
        if (File::exists($filename)) {
174
            File::delete($filename);
175
            if ($shop->thumbnail_square_sizes) {
176
                $sizes = explode(',', $shop->thumbnail_square_sizes);
177
                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...
178
                    foreach ($sizes as $valueSize) {
179
                        File::delete($this->publicImagePath.$valueSize."/".$image->brand_id."/".$image->file);
180
                    }
181
                }
182
            }
183
        }
184
185
        return $image->delete();
186
    }
187
188
189
    public function findImage($imageId)
190
    {
191
        return $this->repo->findImage($imageId);
192
    }
193
194
    public function getModelImage()
195
    {
196
        return $this->repo->getModelImage();
197
    } 
198
199
    public function refactorAllImagesByShopId($shopId)
200
    {
201
        $result = $this->repo->getModelImage()->get();
202
        $shop = ShopService::find($shopId);
203
        if($result AND $shop->thumbnail_square_sizes) {
204
            foreach ($result as $productImage) {
205
                if ($shop->thumbnail_square_sizes) {
206
                    $sizes = explode(',', $shop->thumbnail_square_sizes);
207
                    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...
208
                        foreach ($sizes as $valueSize) {
209
                            if (!File::exists($this->publicImagePath.$valueSize."/".$productImage->brand_id."/")) {
210
                                File::makeDirectory($this->publicImagePath.$valueSize."/".$productImage->brand_id."/", 0777, true);
211
                            }
212
213
                            if (!File::exists($this->publicImagePath.$valueSize."/".$productImage->brand_id."/".$productImage->file)) {
214
                                if (File::exists($this->storageImagePath.$productImage->brand_id."/".$productImage->file)) {
215
                                    $image = Image::make($this->storageImagePath.$productImage->brand_id."/".$productImage->file);
216
                                    $explode = explode('x', $valueSize);
217
                                    $image->fit($explode[0], $explode[1]);
218
                                    $image->interlace();
219
                                    $image->save($this->publicImagePath.$valueSize."/".$productImage->brand_id."/".$productImage->file);
220
                                }
221
                            }
222
                        }
223
                    }
224
                }
225
            }
226
        }
227
    }
228
}