Passed
Push — master ( 7a744e...4c30a2 )
by Matthijs
04:42 queued 17s
created

ProductCombinationService::reduceAmounts()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\Product;
4
 
5
use App\Product;
0 ignored issues
show
Bug introduced by
The type App\Product 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 Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductAttributeCombination;
7
use Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductCombinationRepository;
8
use Hideyo\Ecommerce\Framework\Services\BaseService;
9
 
10
class ProductCombinationService extends BaseService
11
{
12
	public function __construct(ProductCombinationRepository $productTagGroup)
13
	{
14
		$this->repo = $productTagGroup;
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
   public function create(array $attributes, $productId)
18
{
19
        if (isset($attributes['selected_attribute_ids'])) {
20
            $check = $this->repo->getModelAttributeCombination()->leftJoin($this->repo->getModel()->getTable(), $this->repo->getModel()->getTable().'.id', '=', $this->repo->getModelAttributeCombination()->getTable().'.product_attribute_id')
21
            ->where($this->repo->getModel()->getTable().'.product_id', '=', $productId);
22
23
            if (isset($attributes['selected_attribute_ids'])) {
24
                $check->where(function ($query) use ($attributes) {
25
                    $query->whereIn($this->repo->getModelAttributeCombination()->getTable().'.attribute_id', $attributes['selected_attribute_ids']);
26
                });
27
            }
28
29
            if ($check->get()->count()) {
30
                $newData = array();
31
                foreach ($check->get() as $row) {
32
                    $newData[$row['product_attribute_id']] = $row->productAttribute->combinations->toArray();
33
                }
34
35
                foreach ($newData as $row) {
36
                    if (count($row) == count($attributes['selected_attribute_ids'])) {
37
                        $i = 0;
38
                        foreach ($row as $newRow) {
39
                            if (in_array($newRow['attribute_id'], $attributes['selected_attribute_ids'])) {
40
                                $i++;
41
                            }
42
                        }
43
                        
44
                        if (count($row) == $i) {
45
                            return false;
46
                        }
47
                    }
48
                }
49
            }
50
51
            $data = $attributes;
52
            $data['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...
53
            $data['product_id'] = $productId;
54
55
            $new = $this->repo->getModel();
56
            $new->fill($data);
57
            $new->save();
58
59
            if (isset($attributes['selected_attribute_ids'])) {
60
                foreach ($attributes['selected_attribute_ids'] as $row) {
61
                    $newData = array(
62
                        'attribute_id' => $row,
63
                        'product_attribute_id' => $new->id,
64
65
                    );
66
67
                    $ok = new ProductAttributeCombination;
68
                    $ok->fill($newData);
69
                    $ok->save();
70
                }
71
            }
72
73
            return $new;
74
        }
75
    }
76
77
    public function updateById(array $attributes, $productId, $productAttributeId)
78
    {
79
80
        $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...
81
        $attributes['product_id'] = $productId;
82
        $model = $this->find($productAttributeId);
83
84
        if (count($attributes) > 0) {
85
            $model->fill($attributes);
86
            $model->save();
87
88
            $model->combinations()->delete();
89
90
91
            $check = $this->repo->getModelAttributeCombination()->leftJoin($this->repo->getModel()->getTable(), $this->repo->getModel()->getTable().'.id', '=', $this->repo->getModelAttributeCombination()->getTable().'.product_attribute_id')
92
            ->where($this->repo->getModel()->getTable().'.product_id', '=', $attributes['product_id']);
93
94
            if (isset($attributes['selected_attribute_ids'])) {
95
                $check->where(function ($query) use ($attributes) {
96
                    $query->whereIn($this->repo->getModelAttributeCombination()->getTable().'.attribute_id', $attributes['selected_attribute_ids']);
97
                });
98
            }
99
100
            if ($check->get()->count()) {
101
                $newData = array();
102
103
                foreach ($check->get() as $row) {
104
                    $newData[$row['product_attribute_id']] = $row->productAttribute->combinations->toArray();
105
                }
106
107
                if(isset($attributes['selected_attribute_ids'])) {
108
                foreach ($newData as $row) {
109
                    if (count($row) == count($attributes['selected_attribute_ids'])) {
110
                        $i = 0;
111
                        foreach ($row as $newRow) {
112
                            if (in_array($newRow['attribute_id'], $attributes['selected_attribute_ids'])) {
113
                                $i++;
114
                            }
115
                        }
116
                        
117
                        if (count($row) == $i) {
118
                            return false;
119
                        }
120
                    }
121
                }
122
            }
123
            }
124
125
126
            if (isset($attributes['selected_attribute_ids'])) {
127
128
129
                foreach ($attributes['selected_attribute_ids'] as $row) {
130
                    $newData = array(
131
                        'attribute_id' => $row,
132
                        'product_attribute_id' => $model->id,
133
134
                    );
135
136
                    $ok = new ProductAttributeCombination;
137
                    $ok->fill($newData);
138
                    $ok->save();
139
                    $ok->refresh();
140
                }
141
            }
142
        }
143
144
        return $model;
145
    }
146
147
148
    public function increaseAmounts($products)
149
    {
150
        if ($products->count()) {
151
            foreach ($products as $product) {
152
                if ($product->product_attribute_id) {
153
                    $model = $this->find($product->product_attribute_id);
154
                    $attributes = array(
155
                        'amount' => $model->amount + $product->amount
156
                    );
157
158
                    $model->fill($attributes);
159
                    $model->save();
160
                }
161
            }
162
        }
163
    }
164
165
    public function reduceAmounts($products)
166
    {
167
        if ($products->count()) {
168
            foreach ($products as $product) {
169
                if ($product->product_attribute_id) {
170
                    $model = $this->find($product->product_attribute_id);
171
                    $attributes = array(
172
                        'amount' => $model->amount - $product->amount
173
                    );
174
175
                    $model->fill($attributes);
176
                    $model->save();
177
                }
178
            }
179
        }
180
    }
181
182
183
	public function selectAllByProductCategoryId($productCategoryId, $shopId) {
184
		return $this->repo->selectAllByProductCategoryId($productCategoryId, $shopId);
185
	}
186
187
    public function generatePulldowns($product, $productAttributeId, $attributeLeadingGroup = false, $secondAttributeId = false) 
188
    {
189
        $defaultOption = array();
190
        $check = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $check is dead and can be removed.
Loading history...
191
192
        //create all pulldowns
193
        foreach ($product->attributes as $row) {
194
            if ($row['combinations']) {
195
                foreach ($row['combinations'] as $key => $value) {
196
                    $newPullDowns[$value->attribute->attributeGroup->title][$value->attribute->id] = $value->attribute->value;
197
                }
198
            }
199
        }
200
201
        if(!$productAttributeId AND $attributeLeadingGroup AND isset($newPullDowns[$attributeLeadingGroup->title])) {
202
            $productAttributeId = key($newPullDowns[$attributeLeadingGroup->title]);
203
        }
204
205
        $productAttributeResultWithAttributeId =  $this->repo->getProductAttribute($product, $productAttributeId);   
206
207
        if ($productAttributeResultWithAttributeId->get()->first()) {
208
            foreach ($productAttributeResultWithAttributeId->get()->first()->combinations as $combination) {
209
                $defaultOption[$combination->attribute->attributeGroup->title][$combination->attribute->id] = $combination->attribute->value;
210
            }
211
        } else {
212
            $productAttributeId = false;
213
        }
214
215
        $productAttributeResultWithAttributeId = $productAttributeResultWithAttributeId->get();
216
217
        if ($productAttributeResultWithAttributeId) {
218
219
            foreach ($productAttributeResultWithAttributeId as $row) {
220
                if ($row['combinations']) {
221
                    foreach ($row['combinations'] as $key => $value) {
222
                        $defaultOption[$value->attribute->attributeGroup->title][$value->attribute->id] = $value->attribute->value;
223
                    }
224
                }
225
            }
226
        }
227
228
        $defaultPulldown = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $defaultPulldown is dead and can be removed.
Loading history...
229
        if ($attributeLeadingGroup AND isset($newPullDowns[$attributeLeadingGroup->title])) {
230
            $defaultOption[$attributeLeadingGroup->title] = $newPullDowns[$attributeLeadingGroup->title];
231
            $newPullDowns = $defaultOption;
232
        }
233
234
        if ($attributeLeadingGroup AND isset($defaultOption[$attributeLeadingGroup->title])) {
235
            $defaultPulldown = $newPullDowns[$attributeLeadingGroup->title];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $newPullDowns does not seem to be defined for all execution paths leading up to this point.
Loading history...
236
            $defaultPulldownFirstKey = key($newPullDowns[$attributeLeadingGroup->title]);
0 ignored issues
show
Unused Code introduced by
The assignment to $defaultPulldownFirstKey is dead and can be removed.
Loading history...
237
            unset($newPullDowns[$attributeLeadingGroup->title]);
238
            $newPullDowns = array_merge(array($attributeLeadingGroup->title => $defaultPulldown), $newPullDowns);
239
        }
240
241
        $productAttribute = $this->repo->getProductAttribute($product, $productAttributeId, $secondAttributeId)->first();
242
243
        return array('productAttribute' => $productAttribute, 'productAttributeId' => $productAttributeId, 'defaultOption' => $defaultOption, 'newPullDowns' => $newPullDowns);
244
    }
245
246
247
}