UsersAwareTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 40
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUsers() 0 3 1
A setUsers() 0 5 1
A findUserByFullName() 0 8 1
A getUserMatcher() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zored\Telegram\Entity\General;
6
7
use JMS\Serializer\Annotation as Serializer;
8
use Zored\Telegram\Entity\User;
9
use Zored\Telegram\Util\Collection\FuzzyMatcher;
10
use Zored\Telegram\Util\Collection\StringMatcherInterface;
11
12
trait UsersAwareTrait
13
{
14
    /**
15
     * @Serializer\Type("array<Zored\Telegram\Entity\User>")
16
     *
17
     * @var User[]
18
     */
19
    private $users = [];
20
21
    /**
22
     * @return User[]
23
     */
24
    public function getUsers(): array
25
    {
26
        return $this->users;
27
    }
28
29
    public function setUsers(array $users): self
30
    {
31
        $this->users = $users;
32
33
        return $this;
34
    }
35
36
    /**
37
     * @throws \LogicException
38
     */
39
    public function findUserByFullName(string $name): ?User
40
    {
41
        /** @var User|null $user */
42
        $user = $this->getUserMatcher()->matchFirst($name, function (User $user) {
43
            return $user->getFullName();
44
        });
45
46
        return $user;
47
    }
48
49
    private function getUserMatcher(): StringMatcherInterface
50
    {
51
        return new FuzzyMatcher($this->getUsers());
52
    }
53
}
54