Passed
Pull Request — master (#5329)
by Angel Fernando Quiroz
07:23
created

MessageByGroupStateProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A provide() 0 9 2
A supports() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\State;
6
7
use ApiPlatform\Metadata\Operation;
8
use ApiPlatform\State\ProviderInterface;
9
use Chamilo\CoreBundle\Entity\Message;
10
use Chamilo\CoreBundle\Repository\MessageRepository;
11
12
/**
13
 * @template-implements ProviderInterface<array<int, Message>>
14
 */
15
final class MessageByGroupStateProvider implements ProviderInterface
16
{
17
    private MessageRepository $messageRepository;
18
19
    public function __construct(MessageRepository $messageRepository)
20
    {
21
        $this->messageRepository = $messageRepository;
22
    }
23
24
    public function supports(Operation $operation, array $uriVariables = [], array $context = []): bool
25
    {
26
        return Message::class === $operation->getClass() && 'get_messages_by_group' === $operation->getName();
27
    }
28
29
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
30
    {
31
        $groupId = $context['filters']['groupId'] ?? null;
32
33
        if (null === $groupId) {
34
            return [];
35
        }
36
37
        return $this->messageRepository->getMessagesByGroup((int) $groupId, true);
38
    }
39
}
40