Ping::getOverdueDateAttribute()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 7.1428
cc 8
eloc 16
nc 8
nop 0
1
<?php
2
3
namespace App;
4
5
use App\Events\PingFailure;
6
use Carbon\Carbon;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\SoftDeletes;
9
10
/**
11
 * @property int    id
12
 * @property array  tags
13
 * @property string name
14
 * @property bool   overdue
15
 * @property bool   error
16
 * @property Carbon last_ping
17
 * @property int    frequency_value
18
 * @property string frequency
19
 * @property Carbon overdueDate
20
 */
21
class Ping extends Model
22
{
23
    use SoftDeletes;
24
25
    protected $dates = ['last_ping', 'deleted_at'];
26
27
    protected $appends = ['ping_url'];
28
29
    protected $casts = [
30
        'active'           => 'boolean',
31
        'error'            => 'boolean',
32
        'frequency_value'  => 'integer',
33
        'created_by'       => 'integer',
34
        'updated_by'       => 'integer',
35
    ];
36
37
38
    /**
39
     * Attributes
40
     */
41
42 View Code Duplication
    public function getTagsAttribute()
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...
43
    {
44
        if (empty($this->attributes['tags'])) {
45
            return [];
46
        }
47
        $tagArray = explode(',', $this->attributes['tags']);
48
49
        return $this->trimArrayItems($tagArray);
50
    }
51
52
53 View Code Duplication
    public function setTagsAttribute($tags)
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...
54
    {
55
        if (is_array($tags)) {
56
            $this->attributes['tags'] = implode(',', $this->trimArrayItems($tags));
57
        } else {
58
            $this->attributes['tags'] = trim($tags);
59
        }
60
    }
61
62
    public function getLastPingAttribute()
63
    {
64
        if ($this->attributes['last_ping'] == '0000-00-00') {
65
            return null;
66
        }
67
68
        return $this->attributes['last_ping'];
69
    }
70
71
    public function getPingUrlAttribute()
72
    {
73
        return route('ping_url', $this->attributes['name']);
74
    }
75
76
77
    /**
78
     * The date when the check becaomes overdue
79
     *
80
     * @return Carbon
81
     */
82
    public function getOverdueDateAttribute()
83
    {
84
        if (is_null($this->last_ping)) {
85
            return new Carbon();
86
        }
87
88
        switch ($this->frequency) {
89
            case 'minute':
90
                return (new Carbon($this->last_ping))->addMinutes($this->frequency_value);
91
            case 'hour':
92
                return (new Carbon($this->last_ping))->addHours($this->frequency_value);
93
            case 'day':
94
                return (new Carbon($this->last_ping))->addDays($this->frequency_value);
95
            case 'week':
96
                return (new Carbon($this->last_ping))->addWeeks($this->frequency_value);
97
            case 'month':
98
                return (new Carbon($this->last_ping))->addMonths($this->frequency_value);
99
            case 'year':
100
                return (new Carbon($this->last_ping))->addYear($this->frequency_value);
101
        }
102
    }
103
104
105
    /**
106
     * This this ping overdue?
107
     *
108
     * @return bool
109
     */
110
    public function getOverdueAttribute()
111
    {
112
        return ($this->overdue_date->lt(new Carbon()));
0 ignored issues
show
Bug introduced by
The property overdue_date does not seem to exist. Did you mean overdue?

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...
113
    }
114
115
116
117
    /**
118
     * Scopes
119
     */
120
121
    /**
122
     * Scope a query to only include active pings.
123
     *
124
     * @return \Illuminate\Database\Eloquent\Builder
125
     */
126
    public function scopeActive($query)
127
    {
128
        return $query->where('active', 1);
129
    }
130
131
132
    /**
133
     * Methods
134
     */
135
136
    /**
137
     * Locate an existing ping or create a new one
138
     *
139
     * @param string $name
140
     *
141
     * @return Ping
142
     */
143
    public static function findOrNewFromName($name)
144
    {
145
        $ping = self::where('name', $name)->first();
146
147
        if (!$ping) {
148
            $ping = self::createDefaultPing($name, true);
149
        }
150
151
        return $ping;
152
    }
153
154
    /**
155
     * Create a new ping object with the default starting params
156
     *
157
     * @param string $name
158
     * @param bool   $active
159
     * @param null   $createdBy
160
     *
161
     * @return Ping
162
     */
163
    public static function createDefaultPing($name, $active, $createdBy = null)
164
    {
165
        $ping       = new Ping;
166
        $ping->name = $name;
167
        if ($createdBy) {
168
            $ping->created_by = $createdBy;
0 ignored issues
show
Documentation introduced by
The property created_by does not exist on object<App\Ping>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
169
        }
170
        $ping->active          = $active;
0 ignored issues
show
Documentation introduced by
The property active does not exist on object<App\Ping>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
171
        $ping->description     = '';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<App\Ping>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
172
        $ping->tags            = [];
173
        $ping->error           = false;
174
        $ping->frequency       = 'day';
175
        $ping->frequency_value = '1';
176
        $ping->save();
177
178
        return $ping;
179
    }
180
181
    /**
182
     * Set the error state for the ping
183
     */
184
    public function setError()
185
    {
186
        $broadcastEvent = false;
187
        if ($this->error == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
188
            $broadcastEvent = true;
189
        }
190
        $this->error = true;
191
        $this->save();
192
193
        if ($broadcastEvent) {
194
            event(new PingFailure($this));
195
        }
196
    }
197
198
    /**
199
     * Clear the error state for the ping
200
     */
201
    public function clearError()
202
    {
203
        $this->error = false;
204
        $this->save();
205
    }
206
207
    public function recordPingUpdate()
208
    {
209
        $this->last_ping = new Carbon();
210
        $this->error     = false;
211
        $this->save();
212
    }
213
214
215
    /**
216
     * Static
217
     */
218
219
220
    /**
221
     * The base url needed to make a ping url
222
     *
223
     * @return string
224
     */
225
    public static function baseUrl()
226
    {
227
        return route('ping_url', '');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

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...
228
    }
229
230
    /**
231
     * Trim each item in the array
232
     *
233
     * @param array $tags
234
     *
235
     * @return array
236
     */
237
    private function trimArrayItems(array $tags)
238
    {
239
        return array_map(function($tag) {
240
            return trim($tag);
241
        }, $tags);
242
    }
243
}
244