Passed
Push — master ( 7beac6...14a552 )
by Matthijs
02:01
created

ShopService::checkByUrl()   A

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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\Shop;
4
5
use Validator;
0 ignored issues
show
Bug introduced by
The type Validator 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...
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;
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
        $this->storageImagePath = storage_path() .config('hideyo.storage_path'). "/shop/";
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

15
        $this->storageImagePath = storage_path() ./** @scrutinizer ignore-call */ config('hideyo.storage_path'). "/shop/";
Loading history...
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...
Bug introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

15
        $this->storageImagePath = /** @scrutinizer ignore-call */ storage_path() .config('hideyo.storage_path'). "/shop/";
Loading history...
16
        $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...
Bug introduced by
The function public_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

16
        $this->publicImagePath = /** @scrutinizer ignore-call */ public_path() .config('hideyo.public_path'). "/shop/";
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $shopId of type integer|false 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);
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framework\Services\Shop\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...
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
}