Webhook::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Mpociot\CaptainHook;
4
5
use Illuminate\Support\Facades\Cache;
6
use Illuminate\Database\Eloquent\Model as Eloquent;
7
8
/**
9
 * This file is part of CaptainHook arrrrr.
10
 *
11
 * @property integer id
12
 * @property integer tenant_id
13
 * @property string  event
14
 * @property string  url
15
 * @license MIT
16
 */
17
class Webhook extends Eloquent
18
{
19
    /**
20
     * Cache key to use to store loaded webhooks.
21
     */
22
    const CACHE_KEY = 'mpociot.captainhook.hooks';
23
24
    /**
25
     * Make all fields fillable.
26
     * @var array
27
     */
28
    public $fillable = ['id', 'url', 'event', 'tenant_id'];
29
30
    /**
31
     * Boot the model
32
     * Whenever a new Webhook get's created the cache get's cleared.
33
     */
34
    public static function boot()
35
    {
36
        parent::boot();
37
38
        static::created(function ($results) {
0 ignored issues
show
Unused Code introduced by
The parameter $results is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
            Cache::forget(self::CACHE_KEY);
40
        });
41
42
        static::updated(function ($results) {
0 ignored issues
show
Unused Code introduced by
The parameter $results is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
            Cache::forget(self::CACHE_KEY);
44
        });
45
46
        static::deleted(function ($results) {
0 ignored issues
show
Unused Code introduced by
The parameter $results is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
            Cache::forget(self::CACHE_KEY);
48
        });
49
    }
50
51
    /**
52
     * Retrieve the logs for a given hook.
53
     *
54
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
55
     */
56
    public function logs()
57
    {
58
        return $this->hasMany(WebhookLog::class);
59
    }
60
61
    /**
62
     * Retrieve the logs for a given hook.
63
     *
64
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
65
     */
66
    public function lastLog()
67
    {
68
        return $this->hasOne(WebhookLog::class)->orderBy('created_at', 'DESC');
69
    }
70
}
71