Passed
Push — master ( 031d2e...54d408 )
by Robin
03:05
created

Crawlers   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 20
dl 0
loc 49
rs 10
c 0
b 0
f 0

5 Methods

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