Completed
Push — master ( d15e93...7d628f )
by Anton
03:26
created

ReactionObserver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
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\Reaction\Observers;
15
16
use Cog\Laravel\Love\Reaction\Events\ReactionHasBeenAdded;
17
use Cog\Laravel\Love\Reaction\Events\ReactionHasBeenRemoved;
18
use Cog\Laravel\Love\Reaction\Models\Reaction;
19
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
20
21
final class ReactionObserver
22
{
23
    private $eventDispatcher;
24
25
    public function __construct(
26
        DispatcherContract $eventDispatcher
27
    ) {
28
        $this->eventDispatcher = $eventDispatcher;
29
    }
30
31
    public function creating(
32
        Reaction $reaction
33
    ): void {
34
        if ($reaction->getAttributeValue('rate') === null) {
35
            $reaction->setAttribute('rate', Reaction::RATE_DEFAULT);
36
        }
37
    }
38
39
    public function created(
40
        Reaction $reaction
41
    ): void {
42
        $this->eventDispatcher->dispatch(
43
            new ReactionHasBeenAdded($reaction)
44
        );
45
    }
46
47
    public function deleted(
48
        Reaction $reaction
49
    ): void {
50
        $this->eventDispatcher->dispatch(
51
            new ReactionHasBeenRemoved($reaction)
52
        );
53
    }
54
}
55