CheckForBlocklistJob   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 11
c 2
b 0
f 0
dl 0
loc 29
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 10 5
A __construct() 0 4 2
1
<?php
2
3
namespace LaraBlockList\Jobs;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Contracts\Queue\ShouldQueue;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Foundation\Bus\Dispatchable;
9
use Illuminate\Queue\InteractsWithQueue;
10
use Illuminate\Queue\SerializesModels;
11
use LaraBlockList\Contracts\CanBeInBlocklist;
12
13
class CheckForBlocklistJob implements ShouldQueue
14
{
15
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by LaraBlockList\Jobs\CheckForBlocklistJob: $collectionClass, $id, $relations, $keyBy
Loading history...
16
17
    protected array $entities = [];
18
19
    protected string $class;
20
21
    /**
22
     * Create a new job instance.
23
     *
24
     * @return void
25
     */
26 4
    public function __construct(string $class, array|int|string $entities = [])
27
    {
28 4
        $this->class    = $class;
29 4
        $this->entities = is_array($entities) ? $entities : [ $entities ];
0 ignored issues
show
introduced by
The condition is_array($entities) is always true.
Loading history...
30
    }
31
32 3
    public function handle()
33
    {
34
        /** @var CanBeInBlocklist|Model $entity */
35 3
        foreach ($this->entities as $entity) {
36 3
            if (!($entity instanceof $this->class)) {
37 3
                $entity = $this->class::find($entity);
38
            }
39
40 3
            if (!$entity->isAllowlisted() && !$entity->getBlocklistProcessor()->passed($entity)) {
41 3
                $entity->toBlocklist()->save();
42
            }
43
        }
44
    }
45
}
46