Passed
Pull Request — master (#249)
by Anton
03:08
created

ReacterableObserver::created()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of Laravel Love.
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\Love\Reacterable\Observers;
15
16
use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableInterface;
17
18
final class ReacterableObserver
19
{
20
    public function creating(
21
        ReacterableInterface $reacterable,
22
    ): void {
23
        if (
24
            $this->shouldRegisterAsReacterOnCreate($reacterable)
25
            && $reacterable->isNotRegisteredAsLoveReacter()
26
        ) {
27
            $reacterable->registerAsLoveReacter();
28
        }
29
    }
30
31
    private function shouldRegisterAsReacterOnCreate(
32
        ReacterableInterface $reacterable,
33
    ): bool {
34
        return !method_exists($reacterable, 'shouldRegisterAsLoveReacterOnCreate')
35
            || $reacterable->shouldRegisterAsLoveReacterOnCreate();
36
    }
37
}
38