Issues (214)

src/Service/Planning/SessionTrait.php (6 issues)

1
<?php
2
3
namespace Siak\Tontine\Service\Planning;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Collection;
7
use Siak\Tontine\Model\Guild;
8
use Siak\Tontine\Model\Round;
9
use Siak\Tontine\Model\Session;
10
11
trait SessionTrait
12
{
13
    /**
14
     * @var bool
15
     */
16
    private bool $filterActive = false;
17
18
    /**
19
     * @param bool $filter
20
     *
21
     * @return self
22
     */
23
    public function active(bool $filter = true): self
24
    {
25
        $this->filterActive = $filter;
26
        return $this;
27
    }
28
29
    /**
30
     * @param Guild $guild
31
     *
32
     * @return int
33
     */
34
    public function getGuildSessionCount(Guild $guild): int
35
    {
36
        return $guild->sessions()
37
            ->when($this->filterActive, fn(Builder $query) => $query->active())
38
            ->count();
39
    }
40
41
    /**
42
     * @param Guild $guild
43
     * @param int $page
44
     * @param bool $orderAsc
45
     *
46
     * @return Collection
47
     */
48
    public function getGuildSessions(Guild $guild, int $page = 0, bool $orderAsc = true): Collection
49
    {
50
        return $guild->sessions()
51
            ->when($this->filterActive, fn(Builder $query) => $query->active())
52
            ->orderBy('day_date', $orderAsc ? 'asc' : 'desc')
53
            ->page($page, $this->tenantService->getLimit())
54
            ->get();
55
    }
56
57
    /**
58
     * @param Guild $guild
59
     * @param int $sessionId    The session id
60
     *
61
     * @return Session|null
62
     */
63
    public function getGuildSession(Guild $guild, int $sessionId): ?Session
64
    {
65
        return $guild->sessions()
66
            ->when($this->filterActive, fn(Builder $query) => $query->active())
67
            ->find($sessionId);
68
    }
69
70
    /**
71
     * @param Guild $guild
72
     * @param Session $currSession
73
     * @param bool $getAfter Get the sessions after or before the provided one
74
     * @param bool $withCurr Keep the provided session in the list
75
     *
76
     * @return int
77
     */
78
    public function getSessionCount(Guild $guild, Session $currSession,
79
        bool $getAfter = false, bool $withCurr = true): int
80
    {
81
        $operator = $getAfter ? ($withCurr ? '>=' : '>') : ($withCurr ? '<=' : '<');
82
        return $guild->sessions()
83
            ->where('day_date', $operator, $currSession->day_date)->count();
84
    }
85
86
    /**
87
     * Find the prev session.
88
     *
89
     * @param Round $round
90
     * @param Session $session
91
     *
92
     * @return Session|null
93
     */
94
    private function getPrevSession(Round $round, Session $session): ?Session
95
    {
96
        return $round->sessions()
97
            ->where('day_date', '<', $session->day_date)
98
            ->orderBy('day_date', 'desc')
99
            ->first();
100
    }
101
102
    /**
103
     * Find the next session.
104
     *
105
     * @param Round $round
106
     * @param Session $session
107
     *
108
     * @return Session|null
109
     */
110
    private function getNextSession(Round $round, Session $session): ?Session
111
    {
112
        return $round->sessions()
113
            ->where('day_date', '>', $session->day_date)
114
            ->orderBy('day_date', 'asc')
115
            ->first();
116
    }
117
118
    /**
119
     * Find the first session for a new pool or fund.
120
     *
121
     * @param Round $round
122
     * @param Builder $itemQuery
123
     *
124
     * @return Session
125
     */
126
    private function getStartSession(Round $round, Builder $itemQuery): Session
127
    {
128
        $prevEndSession = Session::select(['id', 'day_date'])
129
            ->whereIn('id', $itemQuery->select(['end_sid']))
130
            ->where('day_date', '<', $round->end->day_date)
0 ignored issues
show
The property end does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
131
            ->orderBy('day_date', 'desc')
0 ignored issues
show
'day_date' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderBy(). ( Ignorable by Annotation )

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

131
            ->orderBy(/** @scrutinizer ignore-type */ 'day_date', 'desc')
Loading history...
132
            ->first();
133
        if(!$prevEndSession)
134
        {
135
            return $round->start;
0 ignored issues
show
The property start does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
136
        }
137
138
        $firstSession = $round->sessions()
139
            ->where('day_date', '>', $prevEndSession->day_date)
140
            ->orderBy('day_date', 'asc')
141
            ->first();
142
        return $firstSession !== null &&
143
            $firstSession->day_date > $round->start->day_date ?
144
            $firstSession : $round->start;
145
    }
146
147
    /**
148
     * Find the last session for a new pool or fund.
149
     *
150
     * @param Round $round
151
     * @param Builder $itemQuery
152
     *
153
     * @return Session
154
     */
155
    private function getEndSession(Round $round, Builder $itemQuery): Session
156
    {
157
        $nextStartSession = Session::select(['id', 'day_date'])
158
            ->whereIn('id', $itemQuery->select(['start_sid']))
159
            ->where('day_date', '>', $round->start->day_date)
0 ignored issues
show
The property start does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
160
            ->orderBy('day_date', 'asc')
0 ignored issues
show
'day_date' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderBy(). ( Ignorable by Annotation )

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

160
            ->orderBy(/** @scrutinizer ignore-type */ 'day_date', 'asc')
Loading history...
161
            ->first();
162
        if(!$nextStartSession)
163
        {
164
            return $round->end;
0 ignored issues
show
The property end does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
165
        }
166
167
        $lastSession = $round->sessions()
168
            ->where('day_date', '<', $nextStartSession->day_date)
169
            ->orderBy('day_date', 'desc')
170
            ->first();
171
        return $lastSession !== null &&
172
            $lastSession->day_date < $round->end->day_date ?
173
            $lastSession : $round->end;
174
    }
175
}
176