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