Completed
Push — master ( 1625e5...901021 )
by Tom
07:59 queued 03:01
created

ActivityLogger::enableLogging()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog;
4
5
use Illuminate\Support\Carbon;
6
use Illuminate\Auth\AuthManager;
7
use Illuminate\Contracts\Config\Repository;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Traits\Macroable;
11
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
12
use Spatie\Activitylog\Exceptions\CouldNotLogActivity;
13
use Spatie\String\Str;
14
15
class ActivityLogger
16
{
17
    use Macroable;
18
19
    /** @var \Illuminate\Auth\AuthManager */
20
    protected $auth;
21
22
    protected $defaultLogName = '';
23
24
    /** @var string */
25
    protected $authDriver;
26
27
    /** @var \Spatie\Activitylog\ActivityLogStatus */
28
    protected $logStatus;
29
30
    /** @var \Spatie\Activitylog\Contracts\Activity */
31
    protected $activity;
32 340
33
    public function __construct(AuthManager $auth, Repository $config, ActivityLogStatus $logStatus)
34 340
    {
35
        $this->auth = $auth;
36 340
37
        $this->authDriver = $config['activitylog']['default_auth_driver'] ?? $auth->getDefaultDriver();
38 340
39
        $this->defaultLogName = $config['activitylog']['default_log_name'];
40 340
41 340
        $this->logStatus = $logStatus;
42
    }
43 132
44
    public function setLogStatus(ActivityLogStatus $logStatus)
45 132
    {
46
        $this->logStatus = $logStatus;
47 132
48
        return $this;
49
    }
50 232
51
    public function performedOn(Model $model)
52 232
    {
53
        $this->getActivity()->subject()->associate($model);
54 232
55
        return $this;
56
    }
57 16
58
    public function on(Model $model)
59 16
    {
60
        return $this->performedOn($model);
61
    }
62 340
63
    public function causedBy($modelOrId)
64 340
    {
65 332
        if ($modelOrId === null) {
66
            return $this;
67
        }
68 44
69
        $model = $this->normalizeCauser($modelOrId);
70 44
71
        $this->getActivity()->causer()->associate($model);
72 44
73
        return $this;
74
    }
75 20
76
    public function by($modelOrId)
77 20
    {
78
        return $this->causedBy($modelOrId);
79
    }
80 8
81
    public function causedByAnonymous()
82 8
    {
83 8
        $this->activity->causer_id = null;
0 ignored issues
show
Bug introduced by
Accessing causer_id 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...
84
        $this->activity->causer_type = null;
0 ignored issues
show
Bug introduced by
Accessing causer_type 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...
85 8
86
        return $this;
87
    }
88 4
89
    public function byAnonymous()
90 4
    {
91
        return $this->causedByAnonymous();
92
    }
93 340
94
    public function withProperties($properties)
95 340
    {
96
        $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...
97 340
98
        return $this;
99
    }
100 4
101
    public function withProperty(string $key, $value)
102 4
    {
103
        $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...
104 4
105
        return $this;
106
    }
107 340
108
    public function createdAt(Carbon $dateTime)
109 340
    {
110
        $this->getActivity()->created_at = $dateTime;
0 ignored issues
show
Bug introduced by
Accessing created_at 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...
111 340
112
        return $this;
113
    }
114
115
    public function useLog(string $logName)
116
    {
117
        $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...
118
119 12
        return $this;
120
    }
121 12
122
    public function inLog(string $logName)
123 12
    {
124
        return $this->useLog($logName);
125
    }
126 4
127
    public function tap(callable $callback, string $eventName = null)
128 4
    {
129
        call_user_func($callback, $this->getActivity(), $eventName);
130 4
131
        return $this;
132
    }
133 4
134
    public function enableLogging()
135 4
    {
136
        $this->logStatus->enable();
137 4
138
        return $this;
139
    }
140 340
141
    public function disableLogging()
142 340
    {
143 8
        $this->logStatus->disable();
144
145
        return $this;
146 332
    }
147
148 332
    public function log(string $description)
149 332
    {
150 83
        if ($this->logStatus->disabled()) {
151
            return;
152
        }
153 332
154
        $activity = $this->activity;
155 332
156
        $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...
157 332
            $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...
158
            $activity
159
        );
160 44
161
        $activity->save();
162 44
163 40
        $this->activity = null;
164
165
        return $activity;
166 4
    }
167 4
168 4
    protected function normalizeCauser($modelOrId): Model
169
    {
170 4
        if ($modelOrId instanceof Model) {
171 4
            return $modelOrId;
172
        }
173
174
        $guard = $this->auth->guard($this->authDriver);
175
        $provider = method_exists($guard, 'getProvider') ? $guard->getProvider() : null;
176
        $model = method_exists($provider, 'retrieveById') ? $provider->retrieveById($modelOrId) : null;
177 332
178
        if ($model instanceof Model) {
179
            return $model;
180 12
        }
181
182 12
        throw CouldNotLogActivity::couldNotDetermineUser($modelOrId);
183
    }
184 12
185 4
    protected function replacePlaceholders(string $description, ActivityContract $activity): string
186
    {
187
        return preg_replace_callback('/:[a-z0-9._-]+/i', function ($match) use ($activity) {
188 8
            $match = $match[0];
189
190 8
            $attribute = (string) (new Str($match))->between(':', '.');
191
192 8
            if (! in_array($attribute, ['subject', 'causer', 'properties'])) {
193 4
                return $match;
194
            }
195
196 4
            $propertyName = substr($match, strpos($match, '.') + 1);
197
198 4
            $attributeValue = $activity->$attribute;
199 332
200
            if (is_null($attributeValue)) {
201
                return $match;
202 340
            }
203
204 340
            $attributeValue = $attributeValue->toArray();
205 340
206
            return Arr::get($attributeValue, $propertyName, $match);
207 340
        }, $description);
208 340
    }
209 340
210
    protected function getActivity(): ActivityContract
211
    {
212 340
        if (! $this->activity instanceof ActivityContract) {
213
            $this->activity = ActivitylogServiceProvider::getActivityModelInstance();
214
            $this
215
                ->useLog($this->defaultLogName)
216
                ->withProperties([])
217
                ->causedBy($this->auth->guard($this->authDriver)->user());
218
        }
219
220
        return $this->activity;
221
    }
222
}
223