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

ReactionObserver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 3
b 0
f 0
dl 0
loc 31
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A deleted() 0 5 1
A creating() 0 5 2
A created() 0 5 1
A __construct() 0 4 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