HasStatus::scopeWhereHasAnyStatus()   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\Status;
6
use Illuminate\Database\Eloquent\Builder;
7
8 View Code Duplication
trait HasStatus
9
{
10
    /**
11
     * Check the status of the given model.
12
     *
13
     * @param  string|int|\Chriscreates\Projects\Models\Status $status
14
     * @return bool
15
     */
16
    public function hasStatus($status) : bool
17
    {
18
        if (is_null($this->status)) {
0 ignored issues
show
Bug introduced by
The property status 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($status) || is_string($status)) {
23
            $status = Status::findByIdOrName($status);
24
        }
25
26
        if (is_null($status)) {
27
            return false;
28
        }
29
30
        if ($status instanceof Status) {
31
            $status = $status->id;
32
        }
33
34
        return $this->status_id === $status;
0 ignored issues
show
Bug introduced by
The property status_id does not seem to exist. Did you mean status?

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 status.
39
     *
40
     * @param  array                  $statuses
41
     * @return boolean
42
     */
43
    public function hasAnyStatus(array $statuses) : bool
44
    {
45
        return ! collect($statuses)->reject(function ($status, $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->hasStatus($status);
47
        })->isEmpty();
48
    }
49
50
    /**
51
     * Set the status of the given model.
52
     *
53
     * @param  void                  $status
54
     */
55
    public function setStatus($status) : void
56
    {
57
        if (is_int($status) || is_string($status)) {
58
            $status = Status::findByIdOrName($status);
59
        }
60
61
        if (is_null($status)) {
62
            return;
63
        }
64
65
        $this->status()->save($status);
0 ignored issues
show
Bug introduced by
The method status() does not exist on Chriscreates\Projects\Traits\HasStatus. Did you maybe mean hasStatus()?

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