Passed
Pull Request — master (#100)
by Anton
03:19
created

Reaction::isNotByReacter()   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\Models;
15
16
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantContract;
17
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterContract;
18
use Cog\Contracts\Love\Reaction\Exceptions\RateOutOfRange;
19
use Cog\Contracts\Love\Reaction\Models\Reaction as ReactionContract;
20
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract;
21
use Cog\Laravel\Love\Reactant\Models\Reactant;
22
use Cog\Laravel\Love\Reacter\Models\Reacter;
23
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
24
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
25
use Illuminate\Database\Eloquent\Relations\BelongsTo;
26
27
final class Reaction extends Model implements
28
    ReactionContract
29
{
30
    public const RATE_DEFAULT = 1.0;
31
32
    public const RATE_MIN = 0.01;
33
34
    public const RATE_MAX = 99.99;
35
36
    protected $table = 'love_reactions';
37
38
    protected $attributes = [
39
        'rate' => self::RATE_DEFAULT,
40
    ];
41
42
    protected $fillable = [
43
        'reactant_id',
44
        'reaction_type_id',
45
        'rate',
46
    ];
47
48
    protected $casts = [
49
        'id' => 'string',
50
        'rate' => 'float',
51
    ];
52
53
    public function reactant(): BelongsTo
54
    {
55
        return $this->belongsTo(Reactant::class, 'reactant_id');
56
    }
57
58
    public function reacter(): BelongsTo
59
    {
60
        return $this->belongsTo(Reacter::class, 'reacter_id');
61
    }
62
63
    public function type(): BelongsTo
64
    {
65
        return $this->belongsTo(ReactionType::class, 'reaction_type_id');
66
    }
67
68
    public function getId(): string
69
    {
70
        return $this->getAttributeValue('id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAttributeValue('id') could return the type boolean|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
71
    }
72
73
    public function getReactant(): ReactantContract
74
    {
75
        return $this->getAttribute('reactant');
76
    }
77
78
    public function getReacter(): ReacterContract
79
    {
80
        return $this->getAttribute('reacter');
81
    }
82
83
    public function getType(): ReactionTypeContract
84
    {
85
        return $this->getAttribute('type');
86
    }
87
88
    public function getRate(): float
89
    {
90
        return $this->getAttributeValue('rate');
91
    }
92
93
    public function getWeight(): float
94
    {
95
        return $this->getType()->getMass() * $this->getRate();
96
    }
97
98
    public function setRateAttribute(
99
        ?float $rate
100
    ): void {
101
        if (!is_null($rate) && ($rate < self::RATE_MIN || $rate > self::RATE_MAX)) {
102
            throw RateOutOfRange::withValue($rate);
103
        }
104
105
        $this->attributes['rate'] = $rate;
106
    }
107
108
    public function isOfType(
109
        ReactionTypeContract $reactionType
110
    ): bool {
111
        return $this->getType()->isEqualTo($reactionType);
112
    }
113
114
    public function isNotOfType(
115
        ReactionTypeContract $reactionType
116
    ): bool {
117
        return $this->getType()->isNotEqualTo($reactionType);
118
    }
119
120
    public function isToReactant(
121
        ReactantContract $reactant
122
    ): bool {
123
        return $this->getReactant()->isEqualTo($reactant);
124
    }
125
126
    public function isNotToReactant(
127
        ReactantContract $reactant
128
    ): bool {
129
        return !$this->isToReactant($reactant);
130
    }
131
132
    public function isByReacter(
133
        ReacterContract $reacter
134
    ): bool {
135
        return $this->getReacter()->isEqualTo($reacter);
136
    }
137
138
    public function isNotByReacter(
139
        ReacterContract $reacter
140
    ): bool {
141
        return !$this->isByReacter($reacter);
142
    }
143
144
    // TODO: Add tests
145
    public function changeRate(
146
        float $rate
147
    ): void {
148
        if ($this->getRate() === $rate) {
149
            // TODO: Add tests
150
            // TODO: Add static method ::withRate($rate)
151
            throw new RateNotChanged(sprintf(
0 ignored issues
show
Bug introduced by
The type Cog\Laravel\Love\Reaction\Models\RateNotChanged was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
152
                'Can not change Reaction rate to same value: `%s`.', $rate
153
            ));
154
        }
155
156
        $this->update([
157
            'rate' => $rate,
158
        ]);
159
    }
160
}
161