Passed
Push — master ( 2c1581...8147ed )
by Matthijs
02:08
created

ProductAmountSeriesService::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 17
rs 9.9332
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 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...
7
use Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductAmountSeriesRepository;
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 ProductAmountSeriesService extends BaseService
12
{
13
	public function __construct(ProductAmountSeriesRepository $productAmountSeries)
14
	{
15
		$this->repo = $productAmountSeries;
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
19
 /**
20
     * The validation rules for the model.
21
     *
22
     * @param  integer  $id id attribute model    
23
     * @return array
24
     */
25
    private function rules($id = false)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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

25
    private function rules(/** @scrutinizer ignore-unused */ $id = 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...
26
    {
27
        $rules = array(
28
            'series_value' => 'required',
29
            'series_max' => 'required',
30
        );
31
        
32
        return $rules;
33
    }
34
35
    public function create(array $attributes, $productId)
36
    {
37
        $product = ProductService::find($productId);
38
        $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id;
0 ignored issues
show
Bug introduced by
The function auth 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

38
        $attributes['shop_id'] = /** @scrutinizer ignore-call */ auth('hideyobackend')->user()->selected_shop_id;
Loading history...
39
        $attributes['product_id'] = $product->id;
40
        $validator = Validator::make($attributes, $this->rules());
41
42
        if ($validator->fails()) {
43
            return $validator;
44
        }
45
46
        $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;
47
            
48
        $this->repo->getModel()->fill($attributes);
49
        $this->repo->getModel()->save();
50
   
51
        return $this->repo->getModel();
52
    }
53
54
    public function updateById(array $attributes, $productId, $id)
55
    {
56
        $model = $this->find($id);
57
        $attributes['product_id'] = $productId;
58
        $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id;
0 ignored issues
show
Bug introduced by
The function auth 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

58
        $attributes['shop_id'] = /** @scrutinizer ignore-call */ auth('hideyobackend')->user()->selected_shop_id;
Loading history...
59
        $validator = \Validator::make($attributes, $this->rules($id));
60
61
        if ($validator->fails()) {
62
            return $validator;
63
        }
64
        $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;
65
66
67
        $model->fill($attributes);
68
        $model->save();
69
70
        return $model;
71
    }
72
}