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

Crawlers::activate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
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