Completed
Push — master ( 2efb06...139560 )
by Tom
07:59 queued 04:02
created

ActivityLogger   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 96.25%

Importance

Changes 0
Metric Value
dl 0
loc 188
ccs 77
cts 80
cp 0.9625
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A setLogStatus() 0 6 1
A performedOn() 0 6 1
A on() 0 4 1
A causedBy() 0 12 2
A by() 0 4 1
A withProperties() 0 6 1
A withProperty() 0 6 1
A useLog() 0 6 1
A inLog() 0 4 1
A tap() 0 6 1
A enableLogging() 0 6 1
A disableLogging() 0 6 1
A log() 0 19 2
A normalizeCauser() 0 16 5
A replacePlaceholders() 0 24 3
A getActivity() 0 12 2
1
<?php
2
3
namespace Spatie\Activitylog;
4
5
use Spatie\String\Str;
6
use Illuminate\Support\Arr;
7
use Illuminate\Auth\AuthManager;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Traits\Macroable;
10
use Illuminate\Contracts\Config\Repository;
11
use Spatie\Activitylog\Exceptions\CouldNotLogActivity;
12
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
13
14
class ActivityLogger
15
{
16
    use Macroable;
17
18
    /** @var \Illuminate\Auth\AuthManager */
19
    protected $auth;
20
21
    protected $defaultLogName = '';
22
23
    /** @var string */
24
    protected $authDriver;
25
26
    /** @var \Spatie\Activitylog\ActivityLogStatus */
27
    protected $logStatus;
28
29
    /** @var \Spatie\Activitylog\Contracts\Activity */
30
    protected $activity;
31
32 296
    public function __construct(AuthManager $auth, Repository $config, ActivityLogStatus $logStatus)
33
    {
34 296
        $this->auth = $auth;
35
36 296
        $this->authDriver = $config['activitylog']['default_auth_driver'] ?? $auth->getDefaultDriver();
37
38 296
        $this->defaultLogName = $config['activitylog']['default_log_name'];
39
40 296
        $this->logStatus = $logStatus;
41 296
    }
42
43 124
    public function setLogStatus(ActivityLogStatus $logStatus)
44
    {
45 124
        $this->logStatus = $logStatus;
46
47 124
        return $this;
48
    }
49
50 196
    public function performedOn(Model $model)
51
    {
52 196
        $this->getActivity()->subject()->associate($model);
53
54 196
        return $this;
55
    }
56
57 16
    public function on(Model $model)
58
    {
59 16
        return $this->performedOn($model);
60
    }
61
62 296
    public function causedBy($modelOrId)
63
    {
64 296
        if ($modelOrId === null) {
65 292
            return $this;
66
        }
67
68 40
        $model = $this->normalizeCauser($modelOrId);
69
70 40
        $this->getActivity()->causer()->associate($model);
71
72 40
        return $this;
73
    }
74
75 20
    public function by($modelOrId)
76
    {
77 20
        return $this->causedBy($modelOrId);
78
    }
79
80 296
    public function withProperties($properties)
81
    {
82 296
        $this->getActivity()->properties = collect($properties);
0 ignored issues
show
Bug introduced by
Accessing properties on the interface Spatie\Activitylog\Contracts\Activity suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
83
84 296
        return $this;
85
    }
86
87 4
    public function withProperty(string $key, $value)
88
    {
89 4
        $this->getActivity()->properties = $this->getActivity()->properties->put($key, $value);
0 ignored issues
show
Bug introduced by
Accessing properties on the interface Spatie\Activitylog\Contracts\Activity suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
90
91 4
        return $this;
92
    }
93
94 296
    public function useLog(string $logName)
95
    {
96 296
        $this->getActivity()->log_name = $logName;
0 ignored issues
show
Bug introduced by
Accessing log_name on the interface Spatie\Activitylog\Contracts\Activity suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
97
98 296
        return $this;
99
    }
100
101
    public function inLog(string $logName)
102
    {
103
        return $this->useLog($logName);
104
    }
105
106 12
    public function tap(callable $callback, string $eventName = null)
107
    {
108 12
        call_user_func($callback, $this->getActivity(), $eventName);
109
110 12
        return $this;
111
    }
112
113 4
    public function enableLogging()
114
    {
115 4
        $this->logStatus->enable();
116
117 4
        return $this;
118
    }
119
120 4
    public function disableLogging()
121
    {
122 4
        $this->logStatus->disable();
123
124 4
        return $this;
125
    }
126
127 296
    public function log(string $description)
128
    {
129 296
        if ($this->logStatus->disabled()) {
130 8
            return;
131
        }
132
133 288
        $activity = $this->activity;
134
135 288
        $activity->description = $this->replacePlaceholders(
0 ignored issues
show
Bug introduced by
Accessing description on the interface Spatie\Activitylog\Contracts\Activity suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
136 288
            $activity->description ?? $description,
0 ignored issues
show
Bug introduced by
Accessing description on the interface Spatie\Activitylog\Contracts\Activity suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
137 144
            $activity
138
        );
139
140 288
        $activity->save();
141
142 288
        $this->activity = null;
143
144 288
        return $activity;
145
    }
146
147 40
    protected function normalizeCauser($modelOrId): Model
148
    {
149 40
        if ($modelOrId instanceof Model) {
150 36
            return $modelOrId;
151
        }
152
153 4
        $guard = $this->auth->guard($this->authDriver);
154 4
        $provider = method_exists($guard, 'getProvider') ? $guard->getProvider() : null;
155 4
        $model = method_exists($provider, 'retrieveById') ? $provider->retrieveById($modelOrId) : null;
156
157 4
        if ($model instanceof Model) {
158 4
            return $model;
159
        }
160
161
        throw CouldNotLogActivity::couldNotDetermineUser($modelOrId);
162
    }
163
164 288
    protected function replacePlaceholders(string $description, ActivityContract $activity): string
165
    {
166
        return preg_replace_callback('/:[a-z0-9._-]+/i', function ($match) use ($activity) {
167 12
            $match = $match[0];
168
169 12
            $attribute = (string) (new Str($match))->between(':', '.');
170
171 12
            if (! in_array($attribute, ['subject', 'causer', 'properties'])) {
172 4
                return $match;
173
            }
174
175 8
            $propertyName = substr($match, strpos($match, '.') + 1);
176
177 8
            $attributeValue = $activity->$attribute;
178
179 8
            if (is_null($attributeValue)) {
180 4
                return $match;
181
            }
182
183 4
            $attributeValue = $attributeValue->toArray();
184
185 4
            return Arr::get($attributeValue, $propertyName, $match);
186 288
        }, $description);
187
    }
188
189 296
    protected function getActivity(): ActivityContract
190
    {
191 296
        if (! $this->activity instanceof ActivityContract) {
192 296
            $this->activity = ActivitylogServiceProvider::getActivityModelInstance();
193
            $this
194 296
                ->useLog($this->defaultLogName)
195 296
                ->withProperties([])
196 296
                ->causedBy($this->auth->guard($this->authDriver)->user());
197
        }
198
199 296
        return $this->activity;
200
    }
201
}
202