Passed
Pull Request — master (#188)
by Anton
03:52
created

Reacterable::scopeWhereReactedTo()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 1
nop 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\Reacterable\Models\Traits;
15
16
use Cog\Contracts\Love\Reactable\Models\Reactable as ReactableInterface;
17
use Cog\Contracts\Love\Reacter\Facades\Reacter as ReacterFacadeInterface;
18
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterInterface;
19
use Cog\Contracts\Love\Reacterable\Exceptions\AlreadyRegisteredAsLoveReacter;
20
use Cog\Laravel\Love\Reacter\Facades\Reacter as ReacterFacade;
21
use Cog\Laravel\Love\Reacter\Models\NullReacter;
22
use Cog\Laravel\Love\Reacter\Models\Reacter;
23
use Cog\Laravel\Love\Reacterable\Observers\ReacterableObserver;
24
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
25
use Illuminate\Database\Eloquent\Builder;
26
use Illuminate\Database\Eloquent\Relations\BelongsTo;
27
28
/**
29
 * @mixin \Cog\Contracts\Love\Reacterable\Models\Reacterable
30
 */
31
trait Reacterable
32
{
33
    protected static function bootReacterable(): void
34
    {
35
        static::observe(ReacterableObserver::class);
36
    }
37
38
    public function loveReacter(): BelongsTo
39
    {
40
        return $this->belongsTo(Reacter::class, 'love_reacter_id');
0 ignored issues
show
Bug introduced by
It seems like belongsTo() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        return $this->/** @scrutinizer ignore-call */ belongsTo(Reacter::class, 'love_reacter_id');
Loading history...
41
    }
42
43
    public function getLoveReacter(): ReacterInterface
44
    {
45
        return $this->getAttribute('loveReacter') ?? new NullReacter($this);
0 ignored issues
show
Bug introduced by
$this of type Cog\Laravel\Love\Reacter...dels\Traits\Reacterable is incompatible with the type Cog\Contracts\Love\Reacterable\Models\Reacterable expected by parameter $reacterable of Cog\Laravel\Love\Reacter...lReacter::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        return $this->getAttribute('loveReacter') ?? new NullReacter(/** @scrutinizer ignore-type */ $this);
Loading history...
Bug introduced by
It seems like getAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        return $this->/** @scrutinizer ignore-call */ getAttribute('loveReacter') ?? new NullReacter($this);
Loading history...
46
    }
47
48
    public function viaLoveReacter(): ReacterFacadeInterface
49
    {
50
        return new ReacterFacade($this->getLoveReacter());
51
    }
52
53
    public function isRegisteredAsLoveReacter(): bool
54
    {
55
        return !$this->isNotRegisteredAsLoveReacter();
56
    }
57
58
    public function isNotRegisteredAsLoveReacter(): bool
59
    {
60
        return $this->getAttributeValue('love_reacter_id') === null;
0 ignored issues
show
Bug introduced by
It seems like getAttributeValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        return $this->/** @scrutinizer ignore-call */ getAttributeValue('love_reacter_id') === null;
Loading history...
61
    }
62
63
    public function registerAsLoveReacter(): void
64
    {
65
        if ($this->isRegisteredAsLoveReacter()) {
66
            throw new AlreadyRegisteredAsLoveReacter();
67
        }
68
69
        /** @var \Cog\Contracts\Love\Reacter\Models\Reacter $reacter */
70
        $reacter = $this->loveReacter()->create([
71
            'type' => $this->getMorphClass(),
0 ignored issues
show
Bug introduced by
It seems like getMorphClass() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            'type' => $this->/** @scrutinizer ignore-call */ getMorphClass(),
Loading history...
72
        ]);
73
74
        $this->setAttribute('love_reacter_id', $reacter->getId());
0 ignored issues
show
Bug introduced by
It seems like setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        $this->/** @scrutinizer ignore-call */ 
75
               setAttribute('love_reacter_id', $reacter->getId());
Loading history...
75
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        $this->/** @scrutinizer ignore-call */ 
76
               save();
Loading history...
76
    }
77
78
    public function scopeWhereReactedTo(
79
        Builder $query,
80
        ReactableInterface $reactable,
81
        ?string $reactionTypeName = null
82
    ): Builder {
83
        return $query->whereHas('loveReacter.reactions', function (Builder $reactionsQuery) use ($reactable, $reactionTypeName) {
84
            $reactionsQuery->where('reactant_id', $reactable->getLoveReactant()->getId());
85
            if ($reactionTypeName !== null) {
86
                $reactionsQuery->where('reaction_type_id', ReactionType::fromName($reactionTypeName)->getId());
87
            }
88
        });
89
    }
90
91
    public function scopeWhereNotReactedTo(
92
        Builder $query,
93
        ReactableInterface $reactable,
94
        ?string $reactionTypeName = null
95
    ): Builder {
96
        return $query->whereDoesntHave('loveReacter.reactions', function (Builder $reactionsQuery) use ($reactable, $reactionTypeName) {
97
            $reactionsQuery->where('reactant_id', $reactable->getLoveReactant()->getId());
98
            if ($reactionTypeName !== null) {
99
                $reactionsQuery->where('reaction_type_id', ReactionType::fromName($reactionTypeName)->getId());
100
            }
101
        });
102
    }
103
}
104