ProductRepository   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 104
dl 0
loc 201
rs 10
c 0
b 0
f 0
wmc 21

12 Methods

Rating   Name   Duplication   Size   Complexity  
A selectOneByShopIdAndId() 0 53 1
A selectAllByShopId() 0 3 1
A selectOneById() 0 6 1
A selectAllByShopIdAndProductCategoryId() 0 23 1
A selectAllExport() 0 5 1
A getImageModel() 0 3 1
A __construct() 0 5 1
A ajaxProductImages() 0 39 6
A selectAllByProductParentId() 0 3 1
A selectAllWithCombinations() 0 20 5
A selectByLimitAndOrderBy() 0 5 1
A findImage() 0 3 1
1
<?php
2
namespace Hideyo\Ecommerce\Framework\Services\Product\Entity;
3
 
4
use Hideyo\Ecommerce\Framework\Repositories\ProductImageRepository;
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...\ProductImageRepository 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...
5
use Hideyo\Ecommerce\Framework\Services\Redirect\Entity\RedirectRepository;
6
use Hideyo\Ecommerce\Framework\Services\Product\Entity\Product;
7
use Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductImage;
8
use Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductAttribute;
9
use Hideyo\Ecommerce\Framework\Services\BaseRepository;
10
11
class ProductRepository extends BaseRepository
12
{
13
14
    protected $model;
15
16
    protected $guard = 'admin';
17
18
19
    public function __construct(Product $model, ProductImage $modelImage, RedirectRepository $redirect)
20
    {
21
        $this->model = $model;
22
        $this->modelImage = $modelImage;
0 ignored issues
show
Bug Best Practice introduced by
The property modelImage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
        $this->redirect = $redirect;
0 ignored issues
show
Bug Best Practice introduced by
The property redirect does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
    }
25
26
   
27
    public function selectByLimitAndOrderBy($shopId, $limit, $orderBy)
28
    {
29
        return $this->model->with(array('productCategory', 'relatedProducts', 'productImages' => function ($query) {
30
            $query->orderBy('rank', 'asc');
31
        }))->where('shop_id', $shopId)->where('active', 1)->limit($limit)->orderBy('id', $orderBy)->get();
32
    }
33
34
    function selectAllByShopId($shopId)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
    {
36
         return $this->model->where('shop_id', $shopId)->get();
37
    }
38
    
39
    public function selectAllExport()
40
    {
41
        return $this->model->with(array('productImages' => function ($query) {
42
            $query->orderBy('rank', 'asc');
43
        }))->where('active', 1)->where('shop_id', auth('hideyobackend')->user()->selected_shop_id)->get();
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...
44
    }
45
46
    public function selectAllWithCombinations()
47
    {
48
        $result = $this->model->with(array('attributes'))->where('shop_id', auth('hideyobackend')->user()->selected_shop_id)->get();
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...
49
50
        $newResult = array();
51
        foreach ($result as $product) {
52
            $newResult[$product->id] = $product->title;
53
            if ($product->attributes->count()) {
0 ignored issues
show
Bug Best Practice introduced by
The property $attributes is declared protected in Illuminate\Database\Eloquent\Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
54
                foreach ($product->attributes as $attribute) {
55
                    $attributesArray = array();
56
                    foreach ($attribute->combinations as $combination) {
57
                        $attributesArray[] = $combination->attribute->value;
58
                    }
59
60
                    $newResult[$product->id.'-'.$attribute->id] = $product->title.' - '.implode(', ', $attributesArray);
61
                }
62
            }
63
        }
64
65
        return $newResult;
66
    }
67
68
    public function selectAllByProductParentId($productParentId)
69
    {
70
        return $this->model->where('shop_id', auth('hideyobackend')->user()->selected_shop_id)->where('product_parent_id', '=', $productParentId)->get();
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...
71
    }
72
73
    public function selectOneById($productId)
74
    {
75
        $result = $this->model->with(array('productCategory', 'relatedProducts', 'productImages' => function ($query) {
76
            $query->orderBy('rank', 'asc');
77
        }))->where('active', 1)->where('id', '=', $productId)->get()->first();
78
        return $result;
79
    }
80
81
  
82
    
83
    public function findImage($imageId)
84
    {
85
        return $this->modelImage->find($imageId);
86
    }
87
88
    public function getImageModel()
89
    {
90
        return $this->modelImage;
91
    }
92
93
    function selectAllByShopIdAndProductCategoryId($shopId, $productCategoryId, $filters = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Unused Code introduced by
The parameter $filters is not used and could be removed. ( Ignorable by Annotation )

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

93
    function selectAllByShopIdAndProductCategoryId($shopId, $productCategoryId, /** @scrutinizer ignore-unused */ $filters = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        $result = $this->model
96
        ->with(array('subcategories', 'extraFields' => function ($query) {
97
            $query->with('extraField')->orderBy('id', 'desc');
98
        }, 'taxRate', 'productCategory',  'relatedProducts' => function ($query) {
99
            $query->with('productImages')->orderBy('rank', 'asc');
100
        }, 'productImages' => function ($query) {
101
            $query->orderBy('rank', 'asc');
102
        }))
103
        ->where('shop_id', $shopId)
104
        ->where('active', 1)
105
                ->whereNotNull('product.product_category_id')
106
        ->where(function ($query) use ($productCategoryId) {
107
            $query->where('product_category_id', '=', $productCategoryId);
108
            $query->orWhereHas('subcategories', function ($query) use ($productCategoryId) {
109
                $query->where('product_category_id', '=', $productCategoryId);
110
            });
111
        });
112
113
        $result->orderBy(\DB::raw('product.rank = 0, '.'product.rank'), 'ASC');
114
115
        return $result->get();
116
    }
117
118
    public function selectOneByShopIdAndId($shopId, $productId = false, $attributeId = false)
0 ignored issues
show
Unused Code introduced by
The parameter $attributeId is not used and could be removed. ( Ignorable by Annotation )

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

118
    public function selectOneByShopIdAndId($shopId, $productId = false, /** @scrutinizer ignore-unused */ $attributeId = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
           return $this->model->with(
121
               array(
122
                'attributeGroup',
123
                'attributes' => function ($query) {
124
                    $query->with(
125
                        array(
126
                            'combinations' => function ($query) {
127
                                $query->with(
128
                                    array(
129
                                        'productAttribute',
130
                                        'attribute' => function ($query) {
131
                                            $query->with(
132
                                                array(
133
                                                    'attributeGroup'
134
                                                    )
135
                                            );
136
                                        }
137
                                        )
138
                                );
139
                            }
140
                            )
141
                    )->orderBy('default_on', 'desc');
142
                },
143
                'extraFields' => function ($query) {
144
                    $query->where(
145
                        'value',
146
                        '!=',
147
                        ''
148
                    )->orWhereNotNull('extra_field_default_value_id')->with(array('extraField',
149
                    'extraFieldDefaultValue'))->orderBy(
150
                        'id',
151
                        'desc'
152
                    );
153
                },
154
                'relatedProducts' => function ($query) {
155
                    $query->with(
156
                        'productImages',
157
                        'productCategory'
158
                    )->orderBy(
159
                        'rank',
160
                        'asc'
161
                    );
162
                },
163
                'productImages' => function ($query) {
164
                    $query->orderBy(
165
                        'rank',
166
                        'asc'
167
                    )->with(array('relatedProductAttributes',
168
                    'relatedAttributes'));
169
                })
170
           )->where('shop_id', $shopId)->where('active', 1)->whereNotNull('product_category_id')->where('id', '=', $productId)->get()->first();
171
    }
172
173
    public function ajaxProductImages($product, $combinationsIds, $productAttributeId = false) 
174
    {
175
        $images = array();
176
177
        if($product->productImages->count()) {  
178
179
            $images = $product->productImages()->has('relatedAttributes', '=', 0)->has('relatedProductAttributes', '=', 0)->orderBy('rank', '=', 'asc')->get();
180
181
            if($combinationsIds) {
182
183
                $imagesRelatedAttributes = ProductImage::
184
                whereHas('relatedAttributes', function($query) use ($combinationsIds, $product) { $query->with(array('productImage'))->whereIn('attribute_id', $combinationsIds); })
0 ignored issues
show
Unused Code introduced by
The import $product is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
185
                ->where('product_id', '=', $product->id)
186
                ->get();
187
188
                if($imagesRelatedAttributes) {
189
                    $images = $images->merge($imagesRelatedAttributes)->sortBy('rank');
190
                }
191
                
192
            }
193
194
            if($productAttributeId) {
195
196
                $imagesRelatedProductAttributes = ProductImage::
197
                whereHas('relatedProductAttributes', function($query) use ($productAttributeId, $product) { $query->where('product_attribute_id', '=', $productAttributeId); })
0 ignored issues
show
Unused Code introduced by
The import $product is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
198
                ->where('product_id', '=', $product->id)
199
                ->get();
200
201
                if($imagesRelatedProductAttributes) {
202
                    $images = $images->merge($imagesRelatedProductAttributes)->sortBy('rank');
203
                }   
204
205
                
206
            }
207
208
            $images->toArray();
209
        }
210
211
        return $images;
212
    }
213
}