Listener   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 116
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onCreated() 0 4 1
A onDeleted() 0 4 1
A onRestored() 0 4 1
A getCurrentUser() 0 4 1
A onUpdated() 0 6 2
B log() 0 33 6
1
<?php namespace Sofa\Revisionable\Laravel;
2
3
use Sofa\Revisionable\Listener as ListenerInterface;
4
use Sofa\Revisionable\UserProvider;
5
use Sofa\Revisionable\Revisionable;
6
7
class Listener implements ListenerInterface
8
{
9
    /**
10
     * User provider instance.
11
     *
12
     * @var mixed
13
     */
14
    protected $userProvider;
15
16
    /**
17
     * Create new listener.
18
     *
19
     * @param UserProvider $userProvider
20
     */
21
    public function __construct(UserProvider $userProvider)
22
    {
23
        $this->userProvider = $userProvider;
24
    }
25
26
    /**
27
     * Handle created event.
28
     *
29
     * @param  \Sofa\Revisionable\Revisionable $revisioned
30
     * @return null
31
     */
32
    public function onCreated(Revisionable $revisioned)
33
    {
34
        $this->log('created', $revisioned);
35
    }
36
37
    /**
38
     * Handle updated event.
39
     *
40
     * @param  \Sofa\Revisionable\Revisionable $revisioned
41
     * @return null
42
     */
43
    public function onUpdated(Revisionable $revisioned)
44
    {
45
        if (count($revisioned->getDiff())) {
46
            $this->log('updated', $revisioned);
47
        }
48
    }
49
50
    /**
51
     * Handle deleted event.
52
     *
53
     * @param  \Sofa\Revisionable\Revisionable $revisioned
54
     * @return null
55
     */
56
    public function onDeleted(Revisionable $revisioned)
57
    {
58
        $this->log('deleted', $revisioned);
59
    }
60
61
    /**
62
     * Handle restored event.
63
     *
64
     * @param  \Sofa\Revisionable\Revisionable $revisioned
65
     * @return null
66
     */
67
    public function onRestored(Revisionable $revisioned)
68
    {
69
        $this->log('restored', $revisioned);
70
    }
71
72
    /**
73
     * Get currently logged in user.
74
     *
75
     * @return string|null
76
     */
77
    public function getCurrentUser()
78
    {
79
        return $this->userProvider->getUser();
80
    }
81
82
    /**
83
     * Log the revision.
84
     *
85
     * @param  string $action
86
     * @param  \Sofa\Revisionable\Revisionable $revisioned
87
     * @return null
88
     */
89
    protected function log($action, Revisionable $revisioned)
90
    {
91
        if (!$revisioned->isRevisioned()) {
92
            return;
93
        }
94
95
        $table = $revisioned->getTable();
0 ignored issues
show
Bug introduced by
The method getTable() does not seem to exist on object<Sofa\Revisionable\Revisionable>.

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...
96
        $id    = $revisioned->getKey();
0 ignored issues
show
Bug introduced by
The method getKey() does not seem to exist on object<Sofa\Revisionable\Revisionable>.

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...
97
        $user  = $this->getCurrentUser();
98
        $old   = [];
99
        $new   = [];
100
101
        switch ($action) {
102
            case 'created':
103
                $new = $revisioned->getNewAttributes();
104
                break;
105
            case 'deleted':
106
                $old = $revisioned->getOldAttributes();
107
                break;
108
            case 'updated':
109
                $old = $revisioned->getOldAttributes();
110
                $new = $revisioned->getNewAttributes();
111
                break;
112
        }
113
114
        $logger = $revisioned->getRevisionableLogger();
0 ignored issues
show
Bug introduced by
The method getRevisionableLogger() does not exist on Sofa\Revisionable\Revisionable. Did you maybe mean getRevisionable()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
115
116
        if ($connection = $revisioned->getRevisionableConnection()) {
0 ignored issues
show
Bug introduced by
The method getRevisionableConnection() does not exist on Sofa\Revisionable\Revisionable. Did you maybe mean getRevisionable()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
117
            $logger->on($connection);
118
        }
119
120
        $logger->revisionLog($action, $table, $id, $old, $new, $user);
121
    }
122
}
123