Passed
Push — main ( 5b7914...28aa50 )
by Thierry
05:31
created

TenantService::checkHostAccess()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 3
dl 0
loc 17
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Siak\Tontine\Service;
4
5
use Siak\Tontine\Exception\MessageException;
6
use Siak\Tontine\Model\Round;
7
use Siak\Tontine\Model\Tontine;
8
use Siak\Tontine\Model\User;
9
10
class TenantService
11
{
12
    /**
13
     * @var User|null
14
     */
15
    protected ?User $user = null;
16
17
    /**
18
     * @var Tontine|null
19
     */
20
    protected ?Tontine $tontine = null;
21
22
    /**
23
     * @var Round|null
24
     */
25
    protected ?Round $round = null;
26
27
    /**
28
     * @var int
29
     */
30
    protected int $limit = 10;
31
32
    /**
33
     * @param LocaleService $localeService
34
     */
35
    public function __construct(protected LocaleService $localeService)
36
    {}
37
38
    /**
39
     * @param User $user
40
     *
41
     * @return void
42
     */
43
    public function setUser(User $user): void
44
    {
45
        $this->user = $user;
46
    }
47
48
    /**
49
     * @param Tontine $tontine
50
     *
51
     * @return void
52
     */
53
    public function setTontine(Tontine $tontine): void
54
    {
55
        $this->tontine = $tontine;
56
        // Set the currency for locales.
57
        $this->localeService->setCurrency($tontine->currency_code);
58
        // Save as latest tontine id if it has changed.
59
        $tontineId = $this->user?->properties['latest']['tontine'] ?? 0;
0 ignored issues
show
Bug introduced by
The property properties does not seem to exist on Siak\Tontine\Model\User. 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...
60
        if(!$this->user || $tontineId === $tontine->id)
61
        {
62
            return;
63
        }
64
        $properties = $this->user->properties;
65
        $properties['latest']['tontine'] = $tontine->id;
66
        $this->user->saveProperties($properties);
67
    }
68
69
    /**
70
     * @param Round $round
71
     *
72
     * @return void
73
     */
74
    public function setRound(Round $round): void
75
    {
76
        $this->round = $round;
77
        // Save as latest round id if it has changed.
78
        $roundId = $this->user->properties['latest']['round'] ?? 0;
0 ignored issues
show
Bug introduced by
The property properties does not seem to exist on Siak\Tontine\Model\User. 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...
79
        if($roundId === $round->id)
80
        {
81
            return;
82
        }
83
        $properties = $this->user->properties;
84
        $properties['latest']['round'] = $round->id;
85
        $this->user->saveProperties($properties);
0 ignored issues
show
Bug introduced by
The method saveProperties() does not exist on null. ( Ignorable by Annotation )

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

85
        $this->user->/** @scrutinizer ignore-call */ 
86
                     saveProperties($properties);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
    }
87
88
    /**
89
     * @return void
90
     */
91
    public function resetRound(): void
92
    {
93
        $this->round = null;
94
    }
95
96
    /**
97
     * @return User|null
98
     */
99
    public function user(): ?User
100
    {
101
        return $this->user;
102
    }
103
104
    /**
105
     * @return Tontine|null
106
     */
107
    public function tontine(): ?Tontine
108
    {
109
        return $this->tontine;
110
    }
111
112
    /**
113
     * @return Round|null
114
     */
115
    public function round(): ?Round
116
    {
117
        return $this->round;
118
    }
119
120
    /**
121
     * @param int $roundId    The round id
122
     *
123
     * @return Round|null
124
     */
125
    public function getRound(int $roundId): ?Round
126
    {
127
        return $this->tontine->rounds()->find($roundId);
0 ignored issues
show
Bug introduced by
The method rounds() does not exist on null. ( Ignorable by Annotation )

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

127
        return $this->tontine->/** @scrutinizer ignore-call */ rounds()->find($roundId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
    }
129
130
    /**
131
     * @return Round|null
132
     */
133
    public function getFirstRound(): ?Round
134
    {
135
        return $this->tontine->rounds()->first();
136
    }
137
138
    /**
139
     * @return int
140
     */
141
    public function getLimit(): int
142
    {
143
        return $this->limit;
144
    }
145
146
    /**
147
     * @return bool
148
     */
149
    private function userIsGuest(): bool
150
    {
151
        return $this->tontine !== null && $this->tontine->isGuest;
152
    }
153
154
    /**
155
     * @return array
156
     */
157
    private function getHostAccess(): array
158
    {
159
        if(!$this->tontine || !$this->user)
160
        {
161
            return [];
162
        }
163
        $userInvite = $this->tontine->invites()
164
            ->where('guest_id', $this->user->id)
165
            ->first();
166
        if(!$userInvite)
167
        {
168
            return [];
169
        }
170
171
        return $userInvite->permission->access;
172
    }
173
174
    /**
175
     * Check guest user access to a menu entry in a section
176
     *
177
     * @param string $section
178
     * @param string $entry
179
     * @param bool $return
180
     *
181
     * @return bool
182
     * @throws MessageException
183
     */
184
    public function checkHostAccess(string $section, string $entry, bool $return = false): bool
185
    {
186
        if(!$this->userIsGuest())
187
        {
188
            return true;
189
        }
190
191
        $guestAccess = $this->getHostAccess();
192
        if(!($guestAccess[$section][$entry] ?? false))
193
        {
194
            if($return)
195
            {
196
                return false;
197
            }
198
            throw new MessageException(trans('tontine.invite.errors.access_denied'));
199
        }
200
        return true;
201
    }
202
}
203