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

MessageDataPersister::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
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