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

ReactableObserver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 7
c 1
b 0
f 0
dl 0
loc 18
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldRegisterAsReactantOnCreate() 0 5 2
A creating() 0 8 3
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\Reactable\Observers;
15
16
use Cog\Contracts\Love\Reactable\Models\Reactable as ReactableInterface;
17
18
final class ReactableObserver
19
{
20
    public function creating(
21
        ReactableInterface $reactable,
22
    ): void {
23
        if (
24
            $this->shouldRegisterAsReactantOnCreate($reactable)
25
            && $reactable->isNotRegisteredAsLoveReactant()
26
        ) {
27
            $reactable->registerAsLoveReactant();
28
        }
29
    }
30
31
    private function shouldRegisterAsReactantOnCreate(
32
        ReactableInterface $reactable,
33
    ): bool {
34
        return !method_exists($reactable, 'shouldRegisterAsLoveReactantOnCreate')
35
            || $reactable->shouldRegisterAsLoveReactantOnCreate();
36
    }
37
}
38