TaxRateService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 62
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A rules() 0 12 2
A create() 0 11 2
A updateById() 0 11 2
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\TaxRate;
4
5
use Validator;
6
use Hideyo\Ecommerce\Framework\Services\TaxRate\Entity\TaxRateRepository;
7
use Hideyo\Ecommerce\Framework\Services\BaseService;
8
 
9
class TaxRateService extends BaseService
10
{
11
	public function __construct(TaxRateRepository $taxRate)
12
	{
13
		$this->repo = $taxRate;
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...
14
	} 
15
16
    /**
17
     * The validation rules for the model.
18
     *
19
     * @param  integer  $taxRateId id attribute model    
20
     * @return array
21
     */
22
    public function rules($taxRateId = false)
23
    {
24
        $rules = array(
25
            'title' => 'required|between:2,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id',
26
            'rate'  => 'numeric|required'
27
        );
28
        
29
        if($taxRateId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $taxRateId of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
30
            $rules['title'] =   $rules['title'].','.$taxRateId.' = id';
31
        }
32
33
        return $rules;
34
    } 
35
36
    /**
37
     * create model
38
     * @param  array  $attributes 
39
     * @return mixed         
40
     */
41
    public function create(array $attributes)
42
    {
43
        $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...
44
        $validator = Validator::make($attributes, $this->rules());
45
46
        if ($validator->fails()) {
47
            return $validator;
48
        }
49
50
        $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...
51
        return $this->updateOrAddModel($this->repo->getModel(), $attributes);
52
    }
53
54
    /**
55
     * update model by id
56
     * @param  array  $attributes 
57
     * @param  integer $taxRateId         
58
     * @return mixed            
59
     */
60
    public function updateById(array $attributes, $taxRateId)
0 ignored issues
show
Unused Code introduced by
The parameter $taxRateId 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

60
    public function updateById(array $attributes, /** @scrutinizer ignore-unused */ $taxRateId)

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...
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
        $validator = Validator::make($attributes, $this->rules($id));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
64
        if ($validator->fails()) {
65
            return $validator;
66
        }
67
68
        $model = $this->find($id);
69
        $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...
70
        return $this->updateOrAddModel($model, $attributes);
71
    } 
72
	
73
}