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

HandleRecipient   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 7
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A findRecipientFromUser() 0 14 3
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