|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Laravel Ban. |
|
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\Ban\Observers; |
|
15
|
|
|
|
|
16
|
|
|
use Cog\Contracts\Ban\Ban as BanContract; |
|
17
|
|
|
use Cog\Laravel\Ban\Events\ModelWasBanned; |
|
18
|
|
|
use Cog\Laravel\Ban\Events\ModelWasUnbanned; |
|
19
|
|
|
|
|
20
|
|
|
class BanObserver |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Handle the creating event for the Ban model. |
|
24
|
|
|
* |
|
25
|
|
|
* @param \Cog\Contracts\Ban\Ban $ban |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function creating(BanContract $ban): void |
|
29
|
|
|
{ |
|
30
|
|
|
$bannedBy = auth()->user(); |
|
31
|
|
|
if ($bannedBy && is_null($ban->created_by_type) && is_null($ban->created_by_id)) { |
|
|
|
|
|
|
32
|
|
|
$ban->fill([ |
|
|
|
|
|
|
33
|
|
|
'created_by_type' => $bannedBy->getMorphClass(), |
|
34
|
|
|
'created_by_id' => $bannedBy->getKey(), |
|
35
|
|
|
]); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Handle the created event for the Ban model. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Cog\Contracts\Ban\Ban $ban |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function created(BanContract $ban): void |
|
46
|
|
|
{ |
|
47
|
|
|
$bannable = $ban->bannable()->withBanned()->first(); |
|
|
|
|
|
|
48
|
|
|
$bannable->setBannedFlag($ban->created_at)->save(); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
event(new ModelWasBanned($bannable, $ban)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Handle the deleted event for the Ban model. |
|
55
|
|
|
* |
|
56
|
|
|
* @param \Cog\Contracts\Ban\Ban $ban |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public function deleted(BanContract $ban): void |
|
60
|
|
|
{ |
|
61
|
|
|
$bannable = $ban->bannable()->withBanned()->first(); |
|
62
|
|
|
if ($bannable->bans->count() === 0) { |
|
63
|
|
|
$bannable->unsetBannedFlag()->save(); |
|
64
|
|
|
|
|
65
|
|
|
event(new ModelWasUnbanned($bannable)); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|