InvolvedRepository::update()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Product;
6
use App\Involved;
7
use App\Lot;
8
use Carbon;
9
use Illuminate\Support\Facades\Auth;
10
11
class InvolvedRepository extends Repository
12
{
13
    /**
14
     * @return Involved
15
     */
16
    public function getModel()
17
    {
18
        return new Involved();
19
    }
20
21
    /**
22
     * Involve the product offer
23
     *
24
     * @param array $data
25
     * @param Product $product
26
     * @return Involved
27
     */
28
29
    public function create(array $data, $product)
30
    {
31
        return $this->getModel()
32
            ->create([
33
                'user_id' => \Auth::id(),
34
                'product_id' => $product->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Product>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
35
                'lot_id' => $product->lot_id,
0 ignored issues
show
Documentation introduced by
The property lot_id does not exist on object<App\Product>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
36
                'price_id' => $data['select_product'],
37
                'color_id' => $data['color_product'],
38
                'size_id'=>$data['sizes_product'],
39
                'count' => isset($data['count']) ? $data['count'] : 1
40
            ]);
41
    }
42
43
    /**
44
     * Update involve status.
45
     * 
46
     * @param Involved $involve
47
     * @param array $data
48
     * @return $this
49
     */
50
    public function update(Involved $involve, array $data)
51
    {
52
        $involved = $involve->fill([
53
            'active' => (isset($data['active'])) ? $data['active'] : $involve->active,
0 ignored issues
show
Documentation introduced by
The property active does not exist on object<App\Involved>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
            'count' => (isset($data['count'])) ? $data['count'] : $involve->count
0 ignored issues
show
Documentation introduced by
The property count does not exist on object<App\Involved>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
        ]);
56
57
        $involved->save();
58
59
        return $involved;
60
    }
61
62
    /**
63
     * Check if auth-cate user is involved to this product.
64
     * 
65
     * @param Product $product
66
     * @return bool
67
     */
68
    public function checkIfAuthInvolved(Product $product)
69
    {
70
        $model = $this->getModelByUserAndProduct($product);
71
72
        return (bool) $model;
73
    }
74
75
    /**
76
     * @param array $data
77
     * @param $product
78
     * @return Involved|InvolvedRepository
79
     */
80
    public function createOrUpdate(array $data, $product)
81
    {
82
        if($this->checkIfAuthInvolved($product))
83
            return $this->update($product, $data);
84
        
85
        return $this->create($data, $product);
86
    }
87
88
    /**
89
     * @param $product
90
     * @param null $user
91
     * @param bool $active
92
     * @return mixed
93
     */
94 View Code Duplication
    public function getModelByUserAndProduct($product, $user = null, $active = true)
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...
95
    {
96
        return $this->getModel()
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Involved>? 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...
97
            ->where('user_id', ($user) ? $user : Auth::id())
98
            ->where('product_id', $product->id)
99
            ->active($active)
100
            ->first();
101
    }
102
103
    /**
104
     * @param $product
105
     * @param bool $active
106
     * @return mixed
107
     */
108 View Code Duplication
    public function getModelsByUserAndProduct($product, $active = true)
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...
109
    {
110
        return $this->getModel()
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Involved>? 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...
111
            ->where('user_id', Auth::id())
112
            ->where('product_id', $product->id)
113
            ->active($active)
114
            ->get();
115
    }
116
117
    /**
118
     * Count rules.
119
     * 
120
     * @return string
121
     */
122
    public function countRules()
123
    {
124
        return 'numeric|min:1';
125
    }
126
127
    /**
128
     * @param $product
129
     * @return int
130
     */
131
    public function getInvolveTimesProduct($product)
132
    {
133
        return count($this->getModelsByUserAndProduct($product, true));
134
    }
135
136
    public function getCountSelled($id)
137
    {
138
        return $this->getModel()
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Involved>? 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...
139
            ->where('product_id', $id)
140
            ->active()
141
            ->sum('count');
142
    }
143
144
    public function getInvolvedProductforUser() {
145
146
        return  $this->getModel()
0 ignored issues
show
Bug introduced by
The method active() does not exist on App\Involved. Did you maybe mean scopeActive()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
147
                ->active()
148
                ->where('user_id',\Auth::user()->id)
149
                ->get();
150
151
152
    }
153
154
}