SpecPriceRepository::save()   B
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 7
c 0
b 0
f 0
cc 8
eloc 12
nc 128
nop 2
1
<?php
2
3
namespace App\Repositories;
4
5
use App\SpecPrice;
6
use App\Product;
7
class SpecPriceRepository extends Repository
8
{
9
    /**
10
     * @return SpecPrice
11
     */
12
    public function getModel()
13
    {
14
        return new SpecPrice();
15
    }
16
17
    /**
18
     * @param $product_id
19
     * @return static
20
     */
21
    public function createPlain($product_id)
22
    {
23
        return self::getModel()
24
            ->create([
25
                'product_id' => $product_id
26
            ]);
27
    }
28
29
    /**
30
     * Find model by id/slug.
31
     *
32
     * @param $id
33
     * @return SpecPrice
34
     */
35
    public function find($id)
36
    {
37
        return $this->getModel()
0 ignored issues
show
Documentation Bug introduced by
The method whereId does not exist on object<App\SpecPrice>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
38
            ->whereId((int) $id)
39
            ->first();
40
    }
41
    
42
    public function findKey($key)
43
    {
44
        return $this->getModel()
0 ignored issues
show
Documentation Bug introduced by
The method whereKey does not exist on object<App\SpecPrice>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
45
            ->whereKey($key)
46
            ->first();
47
    }
48
49
    /**
50
     * Delete model..
51
     * 
52
     * @param SpecPrice $model
53
     */
54
/*    public function delete(SpecPrice $model)
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
    {
56
        $model->delete();
57
    }*/
58
    public function delete($id)
59
    {
60
        return $this->find($id)->delete();
61
    }
62
63
    /**
64
     * Update suite of improved specs.
65
     * 
66
     * @param $spec
67
     * @param array $data
68
     * @return mixed
69
     */
70
71
    public function create(array $data, $product)
72
    {
73
        return self::getModel()
74
            ->create([
75
                'lot_id'     => $product->lot_id,
76
                'product_id' => $product->id,
77
                'new_price'  => (isset($data['new_price']) ? $data['new_price'] : ''),
78
                'old_price'  => (isset($data['old_price']) ? $data['old_price'] : ''),
79
                'sale'       => (isset($data['sale'])) ? $data['sale'] : 0,
80
                'name'       => (isset($data['name'])) ? $data['name'] : null
81
            ]);
82
    }
83
84
    public function save(array $data, $product)
85
    {
86
        $key = ((isset($data['key']) &&  $data['key'] != null) ? $data['key'] : null);
87
        $price = self::getModel()->firstOrNew(array('key'=>$key));
0 ignored issues
show
Documentation Bug introduced by
The method firstOrNew does not exist on object<App\SpecPrice>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
88
        $price->product_id = $product->id;
89
        $price->lot_id     = $product->lot_id;
90
        $price->new_price  = (isset($data['new_price']) ? $data['new_price'] : '');
91
        $price->old_price  = (isset($data['old_price']) ? $data['old_price'] : '');
92
        $price->sale       = (isset($data['sale'])) ? $data['sale'] : 0;
93
        $price->name       = (isset($data['name']) ? $data['name'] : ''); 
94
        $price->key        = (isset($data['key']) ? $data['key'] : '');
95
        $price->save();
96
        return $price;
97
    }
98
99 View Code Duplication
    public function update($spec, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $spec->fill([
102
            'size'       => isset($data['size']) ? $data['size'] : null,
103
            'color_hash' => isset($data['color']) ? $data['color'] : null,
104
            'amount'     => isset($data['sold']) ? $data['sold'] : null
105
        ]);
106
        
107
        $spec->save();
108
        
109
        return $spec;
110
    }
111
}