ChatsAwareTrait::getChats()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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\Chat;
9
use Zored\Telegram\Util\Collection\FuzzyMatcher;
10
use Zored\Telegram\Util\Collection\StringMatcherInterface;
11
12
trait ChatsAwareTrait
13
{
14
    /**
15
     * @Serializer\Type("array<Zored\Telegram\Entity\Chat>")
16
     *
17
     * @var Chat[]
18
     */
19
    private $chats = [];
20
21
    /**
22
     * @return Chat[]
23
     */
24
    public function getChats(): array
25
    {
26
        return $this->chats;
27
    }
28
29
    public function findChatByTitle(string $title): ?Chat
30
    {
31
        /** @var Chat|null $chat */
32
        $chat = $this->getChatMatcher()->matchFirst($title, function (Chat $chat) {
33
            return $chat->getTitle();
34
        });
35
36
        return $chat;
37
    }
38
39
    public function setChats(array $chats): self
40
    {
41
        $this->chats = $chats;
42
43
        return $this;
44
    }
45
46
    private function getChatMatcher(): StringMatcherInterface
47
    {
48
        return new FuzzyMatcher($this->getChats());
49
    }
50
}
51