PaymentMethodService   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A selectOneByShopIdAndId() 0 8 2
A rules() 0 12 2
A create() 0 10 2
A updateById() 0 12 2
A __construct() 0 3 1
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\PaymentMethod;
4
5
use Validator;
6
use File;
7
use Hideyo\Ecommerce\Framework\Services\PaymentMethod\Entity\PaymentMethodRepository;
8
use Hideyo\Ecommerce\Framework\Services\BaseService;
9
 
10
class PaymentMethodService extends BaseService
11
{
12
	public function __construct(PaymentMethodRepository $paymentMethod)
13
	{
14
		$this->repo = $paymentMethod;
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 selectOneByShopIdAndId($shopId, $paymentMethodId)
18
    {
19
    	$result = $this->repo->selectOneByShopIdAndId($shopId, $paymentMethodId);
20
21
        if ($result->isEmpty()) {
22
            return false;
23
        }
24
        return $result->first();
25
26
    }
27
28
    /**
29
     * The validation rules for the model.
30
     *
31
     * @param  integer  $paymentMethodId id attribute model    
32
     * @return array
33
     */
34
    private function rules($paymentMethodId = false)
35
    {
36
        $rules = array(
37
            'title' => 'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id',
38
            'price'  => 'numeric|required'
39
        );
40
        
41
        if ($paymentMethodId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $paymentMethodId 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...
42
            $rules['title'] =   $rules['title'].','.$paymentMethodId.' = id';
43
        }
44
45
        return $rules;
46
    }
47
  
48
    public function create(array $attributes)
49
    {
50
        $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...
51
        $validator = Validator::make($attributes, $this->rules());
52
53
        if ($validator->fails()) {
54
            return $validator;
55
        }
56
        $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...
57
        return $this->updateOrAddModel($this->repo->getModel(), $attributes);
58
    }
59
60
    public function updateById(array $attributes, $paymentMethodId)
61
    {
62
        $model = $this->find($paymentMethodId);
63
        $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...
64
        $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...
65
        $validator = Validator::make($attributes, $this->rules($paymentMethodId));
66
67
        if ($validator->fails()) {
68
            return $validator;
69
        }
70
71
        return $this->updateOrAddModel($model, $attributes);
72
    }
73
}