ProductExtraFieldValueService::create()   B
last analyzed

Complexity

Conditions 10
Paths 2

Size

Total Lines 46
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 32
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 46
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
7
use Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductExtraFieldValueRepository;
8
use Hideyo\Ecommerce\Framework\Services\Product\ProductFacade as ProductService;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Hideyo\Ecommerce\Framewo...\Product\ProductService. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use Hideyo\Ecommerce\Framework\Services\BaseService;
10
 
11
class ProductExtraFieldValueService extends BaseService
12
{
13
	public function __construct(ProductExtraFieldValueRepository $productExtraFieldValue)
14
	{
15
		$this->repo = $productExtraFieldValue;
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...
16
	} 
17
18
    public function selectAllByProductId($productId)
19
    {
20
        return $this->repo->selectAllByProductId($productId);
21
    }
22
23
    public function selectAllByProductCategoryId($productCategoryId, $shopId)
24
    {
25
        return $this->repo->selectAllByProductCategoryId($productCategoryId, $shopId);
26
    }
27
28
    public function create(array $attributes, $productId)
29
    {
30
        $product = ProductService::find($productId);
31
        $remove  = $this->repo->getModel()->where('product_id', '=', $productId)->delete();
0 ignored issues
show
Unused Code introduced by
The assignment to $remove is dead and can be removed.
Loading history...
32
                
33
        $result = false;
34
        if (isset($attributes['rows'])) {
35
            foreach ($attributes['rows'] as $row) {
36
                $data = array();
37
38
                $check  = $this->repo->getModel()->where('extra_field_id', '=', $row['extra_field_id'])->where('product_id', '=', $productId)->first();
39
                $data['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...
40
                if (!empty($row['extra_field_default_value_id']) or !empty($row['value'])) {
41
                    if ($check) {
42
                        $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...
43
                        $data['extra_field_id'] = $row['extra_field_id'];
44
                        $data['product_id'] = $product->id;
45
                        if (isset($row['extra_field_default_value_id']) and $row['extra_field_default_value_id']) {
46
                            $data['extra_field_default_value_id'] = $row['extra_field_default_value_id'];
47
                        } else {
48
                            $data['extra_field_default_value_id'] = null;
49
                        }
50
               
51
                        $data['value'] = $row['value'];
52
53
                        $result = $this->repo->getModel()->find($check->id);
54
                        $result->fill($data);
55
                        $result->save();
56
                    } else {
57
                        $data['modified_by_user_id'] = auth('hideyobackend')->user()->id;
58
                        $data['extra_field_id'] = $row['extra_field_id'];
59
                        $data['product_id'] = $product->id;
60
                        if (isset($row['extra_field_default_value_id']) and $row['extra_field_default_value_id']) {
61
                            $data['extra_field_default_value_id'] = $row['extra_field_default_value_id'];
62
                        }
63
                        $data['value'] = $row['value'];
64
65
                        $result = $this->repo->getModel();
66
                        $result->fill($data);
67
                        $result->save();
68
                    }
69
                }
70
            }
71
        }
72
73
        return $result;
74
    }
75
76
77
}