HasPriority::scopeWhereHasAnyPriority()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 6
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Chriscreates\Projects\Traits;
4
5
use Chriscreates\Projects\Models\Priority;
6
use Illuminate\Database\Eloquent\Builder;
7
8 View Code Duplication
trait HasPriority
9
{
10
    /**
11
     * Check the priority of the given model.
12
     *
13
     * @param  string|int|\Chriscreates\Projects\Models\Priority $priority
14
     * @return bool
15
     */
16
    public function hasPriority($priority) : bool
17
    {
18
        if (is_null($this->priority)) {
0 ignored issues
show
Bug introduced by
The property priority does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
            return false;
20
        }
21
22
        if (is_int($priority) || is_string($priority)) {
23
            $priority = Priority::findByIdOrName($priority);
24
        }
25
26
        if (is_null($priority)) {
27
            return false;
28
        }
29
30
        if ($priority instanceof Priority) {
31
            $priority = $priority->id;
32
        }
33
34
        return $this->priority_id === $priority;
0 ignored issues
show
Bug introduced by
The property priority_id does not seem to exist. Did you mean priority?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
35
    }
36
37
    /**
38
     * Check if the given model has any priority.
39
     *
40
     * @param  array                  $priorities
41
     * @return boolean
42
     */
43
    public function hasAnyPriority(array $priorities) : bool
44
    {
45
        return ! collect($priorities)->reject(function ($priority, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
            return ! $this->hasPriority($priority);
47
        })->isEmpty();
48
    }
49
50
    /**
51
     * Set the priority of the given model.
52
     *
53
     * @param  void                  $priority
54
     */
55
    public function setPriority($priority) : void
56
    {
57
        if (is_int($priority) || is_string($priority)) {
58
            $priority = Priority::findByIdOrName($priority);
59
        }
60
61
        if (is_null($priority)) {
62
            return;
63
        }
64
65
        $this->priority()->save($priority);
0 ignored issues
show
Bug introduced by
The method priority() does not exist on Chriscreates\Projects\Traits\HasPriority. Did you maybe mean hasPriority()?

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...
66
    }
67
68
    /**
69
     * Scope - return any model where priority.
70
     *
71
     * @param  \Illuminate\Database\Eloquent\Builder $query
72
     * @param  string|int|\Chriscreates\Projects\Models\Priority $priority
73
     * @return \Illuminate\Database\Eloquent\Builder
74
     */
75
    public function scopeWhereHasPriority(Builder $query, $priority)
76
    {
77
        $key = ! is_string($priority) ? 'id' : 'name';
78
79
        $priority = $priority instanceof Priority ? $priority->id : $priority;
80
81
        return $query->whereHas('priority', function ($query) use ($key, $priority) {
82
            return $query->where($key, $priority);
83
        });
84
    }
85
86
    /**
87
     * Scope - return any model whereIn priorities.
88
     *
89
     * @param  \Illuminate\Database\Eloquent\Builder $query
90
     * @param  string                  $key
91
     * @param  array                   $priorities
92
     * @return \Illuminate\Database\Eloquent\Builder
93
     */
94
    public function scopeWhereHasAnyPriority(Builder $query, string $key, array $priorities)
95
    {
96
        return $query->whereHas('priority', function ($query) use ($key, $priorities) {
97
            return $query->whereIn($key, $priorities);
98
        });
99
    }
100
}
101