BanService::unban()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Services;
15
16
use Cog\Contracts\Ban\Ban as BanContract;
17
use Cog\Contracts\Ban\Bannable as BannableContract;
18
use Cog\Contracts\Ban\BanService as BanServiceContract;
19
use Cog\Laravel\Ban\Models\Ban;
20
use Illuminate\Support\Carbon;
21
22
class BanService implements BanServiceContract
23
{
24
    /**
25
     * Ban entity.
26
     *
27
     * @param \Cog\Contracts\Ban\Bannable $bannable
28
     * @param array $attributes
29
     * @return \Cog\Contracts\Ban\Ban
30
     */
31
    public function ban(BannableContract $bannable, array $attributes = []): BanContract
32
    {
33
        return $bannable->bans()->create($attributes);
0 ignored issues
show
Bug introduced by
The method bans() does not exist on Cog\Contracts\Ban\Bannable. Did you maybe mean ban()? ( Ignorable by Annotation )

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

33
        return $bannable->/** @scrutinizer ignore-call */ bans()->create($attributes);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
    }
35
36
    /**
37
     * Unban entity.
38
     *
39
     * @param \Cog\Contracts\Ban\Bannable $bannable
40
     * @return void
41
     */
42
    public function unban(BannableContract $bannable): void
43
    {
44
        $bannable->bans->each(function ($ban) {
0 ignored issues
show
Bug introduced by
Accessing bans on the interface Cog\Contracts\Ban\Bannable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
45
            $ban->delete();
46
        });
47
    }
48
49
    /**
50
     * Delete all expired Ban models.
51
     *
52
     * @return void
53
     */
54
    public function deleteExpiredBans(): void
55
    {
56
        $bans = Ban::query()
57
            ->where('expired_at', '<=', Carbon::now()->format('Y-m-d H:i:s'))
58
            ->get();
59
60
        $bans->each(function ($ban) {
61
            $ban->delete();
62
        });
63
    }
64
}
65