BanObserver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A creating() 0 7 4
A created() 0 6 1
A deleted() 0 7 2
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)) {
0 ignored issues
show
Bug introduced by
Accessing created_by_id on the interface Cog\Contracts\Ban\Ban suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing created_by_type on the interface Cog\Contracts\Ban\Ban suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
32
            $ban->fill([
0 ignored issues
show
Bug introduced by
The method fill() does not exist on Cog\Contracts\Ban\Ban. Since it exists in all sub-types, consider adding an abstract or default implementation to Cog\Contracts\Ban\Ban. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
            $ban->/** @scrutinizer ignore-call */ 
33
                  fill([
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method bannable() does not exist on Cog\Contracts\Ban\Ban. Since it exists in all sub-types, consider adding an abstract or default implementation to Cog\Contracts\Ban\Ban. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        $bannable = $ban->/** @scrutinizer ignore-call */ bannable()->withBanned()->first();
Loading history...
48
        $bannable->setBannedFlag($ban->created_at)->save();
0 ignored issues
show
Bug introduced by
Accessing created_at on the interface Cog\Contracts\Ban\Ban suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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