Completed
Pull Request — master (#23)
by
unknown
02:30
created

Qwatcher::filterByProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php namespace Maqe\Qwatcher;
2
3
use Illuminate\Database\Eloquent\Builder;
4
use Illuminate\Database\Eloquent\ModelNotFoundException;
5
use Maqe\Qwatcher\Tracks\TracksInterface;
6
use Maqe\Qwatcher\Tracks\Tracks;
7
use Carbon\Carbon;
8
9
class Qwatcher
10
{
11
    public function __construct() {}
12
13
    /**
14
     * Insert or update Track table depand on TracksInterface sub class
15
     *
16
     * @param  TracksInterface $tracks      Sub class that implements TracksInterface
17
     * @return mixed
18
     */
19
    public function tracks(TracksInterface $tracks)
20
    {
21
        return $tracks;
22
    }
23
24
    /**
25
     * Get the list of the queue track
26
     *
27
     * @return collection
28
     */
29
    public function all()
30
    {
31
        return $this->transforms(Tracks::all());
0 ignored issues
show
Documentation introduced by
\Maqe\Qwatcher\Tracks\Tracks::all() is of type object<Illuminate\Databa...base\Eloquent\Builder>>, but the function expects a object<Maqe\Qwatcher\Collection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
    }
33
34
    /**
35
     * Get the track record by id
36
     *
37
     * @param  $id          The track id
38
     * @return object
39
     */
40
    public function getById($id)
41
    {
42
        return $this->transform(Tracks::where('id', $id)->firstOrFail());
43
    }
44
45
    /**
46
     * Get the track record by current status
47
     *
48
     * @param  $status      The track status
49
     * @return collection
50
     */
51
    public function getByStatus($status)
52
    {
53
        $builder = Tracks::where("queue_at", '>', '0000-00-00 00:00:00');
54
        $methodName = 'filterBy'.ucfirst($status);
55
56
        if (method_exists($this, $methodName)) {
57
            $this->{$methodName}($builder);
58
        } else {
59
            throw new \Exception('"'.$status.'" doesn\'t not exist');
60
        }
61
62
        return $this->transforms($builder->get());
63
    }
64
65
    /**
66
     * Get the track record by job name
67
     *
68
     * @param  $name        The job name
69
     * @return collection
70
     */
71
    public function getByJobName($name)
72
    {
73
        $collecName = str_replace('\\', '%',$name);
74
        $condition = "`tracks`.`meta` LIKE '%\"job_name\":\"{$collecName}\"%'";
75
76
        return $this->transforms(Tracks::whereRaw($condition)->get());
77
    }
78
79
    /**
80
     * Transform records in tracks collection
81
     *
82
     * @param  Collection $tracks   The tracks collection
0 ignored issues
show
Documentation introduced by
There is no parameter named $tracks. Did you maybe mean $track?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
83
     * @return Collection           The tracks collection after transform
84
     */
85
    protected function transform($track)
86
    {
87
        $track->meta = ($track->meta) ? json_decode($track->meta) : null;
88
        $track->payload = ($track->payload) ? json_decode($track->payload) : null;
89
        $track->payload->data->command = ($track->payload->data->command) ? unserialize($track->payload->data->command) : null;
90
91
        return $track;
92
    }
93
94
    /**
95
     * Transform records in tracks collection
96
     *
97
     * @param  Collection $tracks   The tracks collection
98
     * @return Collection           The tracks collection after transform
99
     */
100
    protected function transforms($tracks)
101
    {
102
        return $tracks->each(function($track) {
103
            $track = $this->transform($track);
0 ignored issues
show
Unused Code introduced by
$track is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
104
        });
105
    }
106
107
    /**
108
     * Filter by process date is not null
109
     * - succeed and failed must be null
110
     *
111
     * @param  Builder $builder The tracks builder
112
     * @return Builder          The query builder with filter applied.
113
     */
114
    protected function filterByProcess(Builder $builder)
115
    {
116
        return $builder->where("process_at", '>', '0000-00-00 00:00:00')
117
            ->where('succeed_at', '=', '0000-00-00 00:00:00')
118
            ->where('failed_at', '=', '0000-00-00 00:00:00');
119
    }
120
121
    /**
122
     * Filter by succeed date is not null
123
     * - process is not null
124
     * - failed is null
125
     *
126
     * @param  Builder $builder The tracks builder
127
     * @return Builder          The query builder with filter applied.
128
     */
129
    protected function filterBySucceed(Builder $builder)
130
    {
131
        return $builder
132
            ->where('succeed_at', '>', '0000-00-00 00:00:00')
133
            ->where('failed_at', '=', '0000-00-00 00:00:00');
134
    }
135
136
    /**
137
     * Filter by failed date is not null
138
     * - succeed is null
139
     *
140
     * @param  Builder $builder The tracks builder
141
     * @return Builder          The query builder with filter applied.
142
     */
143
    protected function filterByFailed(Builder $builder)
144
    {
145
        return $builder->where('succeed_at', '=', '0000-00-00 00:00:00')
146
            ->where('failed_at', '>', '0000-00-00 00:00:00');
147
    }
148
}
149