Passed
Push — master ( 26669d...eddd64 )
by
unknown
04:41
created

EnsureConsistencyPublisher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\TableSync\Integration\Laravel\Publishers;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\App;
9
use Umbrellio\TableSync\Messages\PublishMessage;
10
use Umbrellio\TableSync\Publisher;
11
12
final class EnsureConsistencyPublisher implements Publisher
13
{
14
    private $publisher;
15
16 2
    public function __construct(Publisher $publisher)
17
    {
18 2
        $this->publisher = $publisher;
19
    }
20
21 2
    public function publish(PublishMessage $message): void
22
    {
23 2
        if (!$this->isRecordExists($message) && !$message->isDestroyed()) {
24 1
            return;
25
        }
26
27 1
        $this->publisher->publish($message);
28
    }
29
30 2
    private function isRecordExists(PublishMessage $message): bool
31
    {
32
        /** @var Model $model */
33 2
        $model = App::make($message->className());
34
35 2
        if (!isset($message->attributes()[$model->getKeyName()])) {
36
            return false;
37
        }
38
39
        return $model
0 ignored issues
show
Bug Best Practice introduced by
The expression return $model->newQuery(...etKeyName()])->exists() could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
40 2
            ->newQuery()
41 2
            ->where($model->getKeyName(), $message->attributes()[$model->getKeyName()])
42 2
            ->exists();
43
    }
44
}
45