Passed
Push — develop ( d32912...6dcea6 )
by Septianata
05:48
created

HandleRecipient::findRecipientFromUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace App\Support\Events;
4
5
use App\Models\Contracts\HasTelegramChatId;
6
use App\Models\User;
7
use InvalidArgumentException;
8
9
trait HandleRecipient
10
{
11
    /**
12
     * Find the recipient from user model if the given parameter is null.
13
     *
14
     * @param  \App\Models\Contracts\HasTelegramChatId|null  $recipient
15
     * @return \App\Models\Contracts\HasTelegramChatId
16
     *
17
     * @throws \InvalidArgumentException
18
     */
19
    protected function findRecipientFromUser(?HasTelegramChatId $recipient): HasTelegramChatId
20
    {
21
        if ($recipient instanceof HasTelegramChatId) {
22
            return $recipient;
23
        }
24
25
        if (!$recipient = User::findAdmin()) {
26
            throw new InvalidArgumentException(
27
                sprintf('Recipient for event %s is unavailable', static::class)
28
            );
29
30
        }
31
32
        return $recipient;
33
    }
34
}
35