AttributeService   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 148
rs 10
wmc 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A selectAllGroups() 0 3 1
A create() 0 15 2
A updateById() 0 17 2
A rules() 0 11 2
A rulesGroup() 0 11 2
A createGroup() 0 13 2
A getGroupModel() 0 3 1
A updateGroupById() 0 15 2
A findGroup() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\Attribute;
4
5
use Validator;
6
use Hideyo\Ecommerce\Framework\Services\Attribute\Entity\AttributeRepository;
7
use Hideyo\Ecommerce\Framework\Services\Attribute\Entity\AttributeGroupRepository;
8
use Hideyo\Ecommerce\Framework\Services\BaseService;
9
 
10
class AttributeService extends BaseService
11
{
12
	public function __construct(AttributeRepository $attribute, AttributeGroupRepository $attributeGroup)
13
	{
14
		$this->repo = $attribute;
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->repoGroup = $attributeGroup;		
0 ignored issues
show
Bug Best Practice introduced by
The property repoGroup does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16
	} 
17
18
    /**
19
     * laravel validation rules for attribute
20
     * @param  boolean $id 
21
     * @return array    
22
     */
23
    private function rules($id = false)
24
    {
25
        $rules = array(
26
            'value' => 'required|unique_with:'.$this->repo->getModel()->getTable().',attribute_group_id'
27
        );
28
29
        if ($id) {
30
            $rules['value'] = $rules['value'].','.$id;
0 ignored issues
show
Bug introduced by
Are you sure $id of type true can be used in concatenation? ( Ignorable by Annotation )

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

30
            $rules['value'] = $rules['value'].','./** @scrutinizer ignore-type */ $id;
Loading history...
31
        }
32
33
        return $rules;
34
    }
35
36
    /**
37
     * laravel validation rules for attributeGroup
38
     * @param  boolean $id 
39
     * @return array    
40
     */
41
    private function rulesGroup($id = false)
42
    {
43
        $rules = array(
44
            'title' => 'required|between:1,65|unique_with:'.$this->repoGroup->getModel()->getTable().', shop_id'
45
        );
46
        
47
        if ($id) {
48
            $rules['title'] =   $rules['title'].','.$id.' = id';
0 ignored issues
show
Bug introduced by
Are you sure $id of type true can be used in concatenation? ( Ignorable by Annotation )

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

48
            $rules['title'] =   $rules['title'].','./** @scrutinizer ignore-type */ $id.' = id';
Loading history...
49
        }
50
51
        return $rules;
52
    }
53
54
    /**
55
     * create a attribute
56
     * @param  array  $attributes       
57
     * @param  integer $attributeGroupId 
58
     * @return mixed                   
59
     */
60
    public function create(array $attributes, $attributeGroupId)
61
    {
62
        $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...
63
        $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...
64
  		$attributes['attribute_group_id'] = $attributeGroupId;
65
66
        $validator = Validator::make($attributes, $this->rules());
67
68
        if ($validator->fails()) {
69
            return $validator;
70
        }
71
72
        $this->repo->getModel()->fill($attributes);
73
        $this->repo->getModel()->save();
74
        return $this->repo->getModel();
75
    }
76
77
    /**
78
     * create a attribute group
79
     * @param  array  $attributes        
80
     * @return mixed                   
81
     */    
82
    public function createGroup(array $attributes)
83
    {
84
        $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...
85
        $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...
86
        $validator = Validator::make($attributes, $this->rulesGroup());
87
88
        if ($validator->fails()) {
89
            return $validator;
90
        }
91
92
        $this->repoGroup->getModel()->fill($attributes);
93
        $this->repoGroup->getModel()->save();
94
        return $this->repoGroup->getModel();
95
    }
96
97
    public function updateById(array $attributes, $attributeGroupId, $id)
98
    {
99
        $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...
100
        $attributes['attribute_group_id'] = $attributeGroupId;
101
        $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...
102
103
        $validator = Validator::make($attributes, $this->rules($id));
104
        
105
        if ($validator->fails()) {
106
            return $validator;
107
        }
108
109
        $model = $this->find($id);
110
        $model->fill($attributes);
111
        $model->save();
112
113
        return $model;    
114
    } 
115
116
    public function updateGroupById(array $attributes, $id)
117
    {
118
        $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...
119
        $validator = Validator::make($attributes, $this->rulesGroup($id));
120
        if ($validator->fails()) {
121
            return $validator;
122
        }
123
124
        $model = $this->findGroup($id);
125
        $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...
126
127
        $model->fill($attributes);
128
        $model->save();
129
130
        return $model;    
131
    } 
132
133
    /**
134
     * select all attribute Groups
135
     * @return object
136
     */
137
    public function selectAllGroups()
138
    {
139
        return $this->repoGroup->selectAll();
140
    }
141
142
    /**
143
     * get attribute group model
144
     * @return object
145
     */
146
    public function getGroupModel()
147
    {
148
        return $this->repoGroup->getModel();
149
    }
150
151
    /**
152
     * find a attribute group model
153
     * @return object
154
     */
155
    public function findGroup($groupId)
156
    {
157
        return $this->repoGroup->find($groupId);
158
    }
159
}