Crawlers::runs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Famdirksen\LaravelJobHandler\Models;
4
5
use Famdirksen\LaravelJobHandler\Exceptions\CrawlerAlreadyActivatedException;
6
use Famdirksen\LaravelJobHandler\Exceptions\CrawlerAlreadyDeactivatedException;
7
use Illuminate\Database\Eloquent\Model;
8
9
class Crawlers extends Model
10
{
11
    protected $table = 'crawlers';
12
13
    protected $fillable = [
14
       'name',
15
       'description'
16
    ];
17
18
19
20
    public function runs()
21
    {
22
        return $this->hasMany('Famdirksen\LaravelJobHandler\Models\CrawlerStatus', 'crawler_id', 'id');
23
    }
24
    public function last_run()
25
    {
26
        return $this->hasOne('Famdirksen\LaravelJobHandler\Models\CrawlerStatus', 'crawler_id', 'id')
27
            ->orderBy('created_at', 'DESC');
28
    }
29
30
31
    public function getLastRunnedAtAttribute()
32
    {
33
        if ($this->last_run) {
34
            return $this->last_run->created_at;
35
        }
36
37
        return null;
38
    }
39
40
    public function activate()
41
    {
42
        if ($this->enabled) {
43
            throw new CrawlerAlreadyActivatedException();
44
        }
45
46
        $this->enabled = true;
47
48
        return $this->save();
49
    }
50
    public function deactivate()
51
    {
52
        if (!$this->enabled) {
53
            throw new CrawlerAlreadyDeactivatedException();
54
        }
55
56
        $this->enabled = false;
57
58
        return $this->save();
59
    }
60
}
61