Passed
Pull Request — master (#227)
by Anton
02:58
created

joinReactionCounterOfType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 20
rs 9.8666
cc 2
nc 2
nop 2
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\Reactable;
15
16
use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableInterface;
17
use Cog\Laravel\Love\Reactant\ReactionCounter\Models\ReactionCounter;
18
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal;
19
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
20
use Illuminate\Database\Eloquent\Builder;
21
use Illuminate\Database\Query\JoinClause;
22
use Illuminate\Support\Facades\DB;
23
use Illuminate\Support\Str;
24
25
/**
26
 * @mixin Builder
27
 */
28
trait ReactableEloquentBuilderTrait
29
{
30
    public function whereReactedBy(
31
        ReacterableInterface $reacterable,
32
        ?string $reactionTypeName = null,
33
    ): Builder {
34
        return $this->whereHas(
0 ignored issues
show
Bug introduced by
It seems like whereHas() 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

34
        return $this->/** @scrutinizer ignore-call */ whereHas(
Loading history...
35
            'loveReactant.reactions',
36
            function (Builder $reactionsQuery) use ($reacterable, $reactionTypeName): void {
37
                $reactionsQuery->where('reacter_id', $reacterable->getLoveReacter()->getId());
38
                if ($reactionTypeName !== null) {
39
                    $reactionsQuery->where('reaction_type_id', ReactionType::fromName($reactionTypeName)->getId());
40
                }
41
            }
42
        );
43
    }
44
45
    public function whereNotReactedBy(
46
        ReacterableInterface $reacterable,
47
        ?string $reactionTypeName = null,
48
    ): Builder {
49
        return $this->whereDoesntHave(
0 ignored issues
show
Bug introduced by
It seems like whereDoesntHave() 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

49
        return $this->/** @scrutinizer ignore-call */ whereDoesntHave(
Loading history...
50
            'loveReactant.reactions',
51
            function (Builder $reactionsQuery) use ($reacterable, $reactionTypeName): void {
52
                $reactionsQuery->where('reacter_id', $reacterable->getLoveReacter()->getId());
53
                if ($reactionTypeName !== null) {
54
                    $reactionsQuery->where('reaction_type_id', ReactionType::fromName($reactionTypeName)->getId());
55
                }
56
            }
57
        );
58
    }
59
60
    public function joinReactionCounterOfType(
61
        string $reactionTypeName,
62
        ?string $alias = null,
63
    ): Builder {
64
        $reactionType = ReactionType::fromName($reactionTypeName);
65
        $alias = $alias === null ? 'reaction_' . Str::snake($reactionType->getName()) : $alias;
66
67
        $select = $this->getQuery()->columns ?? ["{$this->getModel()->getTable()}.*"];
0 ignored issues
show
Bug introduced by
It seems like getQuery() 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

67
        $select = $this->/** @scrutinizer ignore-call */ getQuery()->columns ?? ["{$this->getModel()->getTable()}.*"];
Loading history...
Bug introduced by
It seems like getModel() 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

67
        $select = $this->getQuery()->columns ?? ["{$this->/** @scrutinizer ignore-call */ getModel()->getTable()}.*"];
Loading history...
68
        $select[] = DB::raw("COALESCE({$alias}.count, 0) as {$alias}_count");
69
        $select[] = DB::raw("COALESCE({$alias}.weight, 0) as {$alias}_weight");
70
71
        return $this
72
            ->leftJoin(
0 ignored issues
show
Bug introduced by
It seems like leftJoin() 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

72
            ->/** @scrutinizer ignore-call */ leftJoin(
Loading history...
73
                (new ReactionCounter())->getTable() . ' as ' . $alias,
74
                function (JoinClause $join) use ($reactionType, $alias): void {
75
                    $join->on("{$alias}.reactant_id", '=', "{$this->getModel()->getTable()}.love_reactant_id");
76
                    $join->where("{$alias}.reaction_type_id", $reactionType->getId());
77
                }
78
            )
79
            ->select($select);
80
    }
81
82
    public function joinReactionTotal(
83
        ?string $alias = null,
84
    ): Builder {
85
        $alias = $alias === null ? 'reaction_total' : $alias;
86
        $select = $this->getQuery()->columns ?? ["{$this->getModel()->getTable()}.*"];
87
        $select[] = DB::raw("COALESCE({$alias}.count, 0) as {$alias}_count");
88
        $select[] = DB::raw("COALESCE({$alias}.weight, 0) as {$alias}_weight");
89
90
        return $this
91
            ->leftJoin(
92
                (new ReactionTotal())->getTable() . ' as ' . $alias,
93
                "{$alias}.reactant_id",
94
                '=',
95
                "{$this->getModel()->getTable()}.love_reactant_id"
96
            )
97
            ->select($select);
98
    }
99
}
100