|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Laravel Love. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Anton Komarev <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Cog\Laravel\Love\Reacterable\Models\Traits; |
|
15
|
|
|
|
|
16
|
|
|
use Cog\Contracts\Love\Reacter\Facades\Reacter as ReacterFacadeInterface; |
|
17
|
|
|
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterInterface; |
|
18
|
|
|
use Cog\Contracts\Love\Reacterable\Exceptions\AlreadyRegisteredAsLoveReacter; |
|
19
|
|
|
use Cog\Laravel\Love\Reacter\Facades\Reacter as ReacterFacade; |
|
20
|
|
|
use Cog\Laravel\Love\Reacter\Models\NullReacter; |
|
21
|
|
|
use Cog\Laravel\Love\Reacter\Models\Reacter; |
|
22
|
|
|
use Cog\Laravel\Love\Reacterable\Observers\ReacterableObserver; |
|
23
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @mixin \Cog\Contracts\Love\Reacterable\Models\Reacterable |
|
27
|
|
|
*/ |
|
28
|
|
|
trait Reacterable |
|
29
|
|
|
{ |
|
30
|
|
|
protected static function bootReacterable(): void |
|
31
|
|
|
{ |
|
32
|
|
|
static::observe(ReacterableObserver::class); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function loveReacter(): BelongsTo |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->belongsTo(Reacter::class, 'love_reacter_id'); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getLoveReacter(): ReacterInterface |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->getAttribute('loveReacter') ?? new NullReacter($this); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function viaLoveReacter(): ReacterFacadeInterface |
|
46
|
|
|
{ |
|
47
|
|
|
return new ReacterFacade($this->getLoveReacter()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function isRegisteredAsLoveReacter(): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return !$this->isNotRegisteredAsLoveReacter(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function isNotRegisteredAsLoveReacter(): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->getAttributeValue('love_reacter_id') === null; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function registerAsLoveReacter(): void |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->isRegisteredAsLoveReacter()) { |
|
63
|
|
|
throw new AlreadyRegisteredAsLoveReacter(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @var \Cog\Contracts\Love\Reacter\Models\Reacter $reacter */ |
|
67
|
|
|
$reacter = $this->loveReacter()->create([ |
|
68
|
|
|
'type' => $this->getMorphClass(), |
|
|
|
|
|
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
$this->setAttribute('love_reacter_id', $reacter->getId()); |
|
|
|
|
|
|
72
|
|
|
$this->save(); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|