Last active
August 22, 2019 23:09
-
-
Save osbre/f20686b9fe313db8d69b6d4d53a73af1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App; | |
| use Modules\Forum\Entities\ReplyWithThread; | |
| use Spatie\Activitylog\Models\Activity as BaseActivity; | |
| class Activity extends BaseActivity | |
| { | |
| private $morphRelationModels = [ | |
| 'replies' => ReplyWithThread::class | |
| ]; | |
| public function getSubjectTypeAttribute($value) | |
| { | |
| if (is_null($value)) return ($value); | |
| if (array_key_exists($value, $this->morphRelationModels)){ | |
| return $this->morphRelationModels[$value]; | |
| } | |
| return $value; | |
| } | |
| public function getSubjectDescriptionAttribute() | |
| { | |
| switch ($this->getSubjectType()){ | |
| case 'replies': | |
| return __('Replied to the thread').$this->generateLink( | |
| route('forum.threads.show', ['slug' => $this->subject->thread->slug]), | |
| $this->subject->thread->title | |
| ); | |
| break; | |
| case 'threads': | |
| return __('Created the thread').$this->generateLink( | |
| route('forum.threads.show', ['slug' => $this->subject->slug]), | |
| $this->subject->slug | |
| ); | |
| break; | |
| default: | |
| return ''; | |
| } | |
| } | |
| private function getSubjectType(): string | |
| { | |
| $subjectType = $this->subject_type; | |
| $relationModels = array_flip($this->morphRelationModels); | |
| if (array_key_exists($subjectType, $relationModels)){ | |
| $subjectType = $relationModels[$subjectType]; | |
| } | |
| return $subjectType; | |
| } | |
| private function generateLink(string $url, string $title): string | |
| { | |
| return sprintf( | |
| ' <a href="%s">%s</a>', | |
| $url, | |
| $title | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment