Passed
Pull Request — master (#240)
by Anton
03:47 queued 52s
created

Reactant::getReactions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
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 2
rs 10
cc 1
nc 1
nop 0
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\Reactant\Facades;
15
16
use Cog\Contracts\Love\Reactant\Facades\Reactant as ReacterFacadeInterface;
17
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantInterface;
18
use Cog\Contracts\Love\Reactant\ReactionCounter\Models\ReactionCounter as ReactionCounterInterface;
19
use Cog\Contracts\Love\Reactant\ReactionTotal\Models\ReactionTotal as ReactionTotalInterface;
20
use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableInterface;
21
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
22
23
final class Reactant implements
24
    ReacterFacadeInterface
25
{
26
    private ReactantInterface $reactant;
27
28
    public function __construct(
29
        ReactantInterface $reactant,
30
    ) {
31
        $this->reactant = $reactant;
32
    }
33
34
    /**
35
     * @return iterable|\Cog\Contracts\Love\Reaction\Models\Reaction[]
36
     */
37
    public function getReactions(): iterable {
38
        return $this->reactant->getReactions();
39
    }
40
41
    /**
42
     * @return iterable|\Cog\Contracts\Love\Reaction\Models\Reaction[]
43
     */
44
    public function getReactionsBy(
45
        ReacterableInterface $reacterable,
46
    ): iterable {
47
        return $this->reactant->getReactionsBy(
48
            $reacterable->getLoveReacter(),
49
        );
50
    }
51
52
    /**
53
     * @return iterable|\Cog\Contracts\Love\Reactant\ReactionCounter\Models\ReactionCounter[]
54
     */
55
    public function getReactionCounters(): iterable
56
    {
57
        return $this->reactant->getReactionCounters();
58
    }
59
60
    public function getReactionCounterOfType(
61
        string $reactionTypeName,
62
    ): ReactionCounterInterface {
63
        return $this->reactant->getReactionCounterOfType(
64
            ReactionType::fromName($reactionTypeName),
65
        );
66
    }
67
68
    public function getReactionTotal(): ReactionTotalInterface
69
    {
70
        return $this->reactant->getReactionTotal();
71
    }
72
73
    public function isReactedBy(
74
        ReacterableInterface | null $reacterable = null,
75
        string | null $reactionTypeName = null,
76
        float | null $rate = null,
77
    ): bool {
78
        if ($reacterable === null) {
79
            return false;
80
        }
81
82
        $reactionType = $reactionTypeName === null ? null : ReactionType::fromName($reactionTypeName);
83
84
        return $this->reactant->isReactedBy(
85
            $reacterable->getLoveReacter(),
86
            $reactionType,
87
            $rate,
88
        );
89
    }
90
91
    public function isNotReactedBy(
92
        ReacterableInterface | null $reacterable = null,
93
        string | null $reactionTypeName = null,
94
        float | null $rate = null,
95
    ): bool {
96
        if ($reacterable === null) {
97
            return true;
98
        }
99
100
        $reactionType = $reactionTypeName === null ? null : ReactionType::fromName($reactionTypeName);
101
102
        return $this->reactant->isNotReactedBy(
103
            $reacterable->getLoveReacter(),
104
            $reactionType,
105
            $rate,
106
        );
107
    }
108
}
109