NotificationDriver   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 45
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A audit() 0 25 2
A prune() 0 4 1
1
<?php
2
3
namespace Andreshg112\LaravelAuditingNotifications;
4
5
use Illuminate\Support\Arr;
6
use OwenIt\Auditing\Contracts\Audit;
7
use Illuminate\Notifications\Notifiable;
8
use OwenIt\Auditing\Contracts\Auditable;
9
use OwenIt\Auditing\Contracts\AuditDriver;
10
use OwenIt\Auditing\Exceptions\AuditingException;
11
12
class NotificationDriver implements AuditDriver
13
{
14
    /**
15
     * Perform an audit.
16
     *
17
     * @param \OwenIt\Auditing\Contracts\Auditable|\Illuminate\Notifications\Notifiable $model
18
     * @return \OwenIt\Auditing\Contracts\Audit
19
     */
20 1
    public function audit(Auditable $model): Audit
21
    {
22 1
        throw_unless(
23 1
            in_array(Notifiable::class, class_uses_recursive($model)),
24 1
            new AuditingException(
25 1
                "The model doesn't use the \Illuminate\Notifications\Notifiable trait."
26
            )
27
        );
28
29 1
        $audit = new \OwenIt\Auditing\Models\Audit($model->toAudit());
30
31
        /** @var string[] $auditEventClasses */
32 1
        $auditEventClasses = Arr::wrap(
33 1
            config('audit.notification-driver.notifications', AuditSns::class)
34
        );
35
36 1
        foreach ($auditEventClasses as $auditEventClass) {
37
            /** @var \Illuminate\Notifications\Notification|AuditSns $notification */
38 1
            $notification = new $auditEventClass($audit->toArray());
39
40 1
            $model->notify($notification);
0 ignored issues
show
Bug introduced by Andrés Herrera García
The method notify() does not seem to exist on object<OwenIt\Auditing\Contracts\Auditable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
        }
42
43 1
        return $audit;
44
    }
45
46
    /**
47
     * Remove older audits that go over the threshold.
48
     *
49
     * @param \OwenIt\Auditing\Contracts\Auditable $model
50
     * @return bool
51
     */
52 1
    public function prune(Auditable $model): bool
53
    {
54 1
        return false;
55
    }
56
}
57