Passed
Push — master ( f10e67...52beba )
by Julito
07:56
created

MessageDataPersister   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
A remove() 0 4 1
A persist() 0 29 4
A __construct() 0 5 1
A resumable() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\DataPersister;
8
9
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
10
use ApiPlatform\Core\DataPersister\ResumableDataPersisterInterface;
11
use Chamilo\CoreBundle\Entity\Message;
12
use Doctrine\ORM\EntityManager;
13
use Symfony\Component\Messenger\MessageBusInterface;
14
15
class MessageDataPersister implements ContextAwareDataPersisterInterface, ResumableDataPersisterInterface
16
{
17
    private EntityManager $entityManager;
18
    private ContextAwareDataPersisterInterface $decorated;
19
    private MessageBusInterface $bus;
20
21
    public function __construct(ContextAwareDataPersisterInterface $decorated, EntityManager $entityManager, MessageBusInterface $bus)
22
    {
23
        $this->decorated = $decorated;
24
        $this->entityManager = $entityManager;
25
        $this->bus = $bus;
26
    }
27
28
    public function supports($data, array $context = []): bool
29
    {
30
        return $this->decorated->supports($data, $context);
31
    }
32
33
    public function persist($data, array $context = [])
34
    {
35
        $result = $this->decorated->persist($data, $context);
36
37
        if ($data instanceof Message && (
38
            ($context['collection_operation_name'] ?? null) === 'post' ||
39
                ($context['graphql_operation_name'] ?? null) === 'create'
40
                //($context['item_operation_name'] ?? null) === 'put' // on update
41
        )
42
        ) {
43
            /*if (Message::MESSAGE_TYPE_INBOX === $result->getMsgType()) {
44
                $messageSent = clone $result;
45
                $messageSent
46
                    ->setMsgType(Message::MESSAGE_TYPE_OUTBOX)
47
                    //->setRead(true)
48
                ;
49
                $this->entityManager->persist($messageSent);
50
                $this->entityManager->flush();
51
                echo 'send11';
52
53
                // Send message.
54
                $this->bus->dispatch($data);
55
            }*/
56
        }
57
58
        /*$this->entityManager->persist($data);
59
        $this->entityManager->flush();*/
60
61
        return $result;
62
    }
63
64
    public function remove($data, array $context = []): void
65
    {
66
        $this->entityManager->remove($data);
67
        $this->entityManager->flush();
68
    }
69
70
    public function resumable(array $context = []): bool
71
    {
72
        return true;
73
    }
74
}
75