Completed
Push — master ( b12f66...4ba672 )
by Rémi
03:04
created

UserEventHandler::handle()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 32
ccs 0
cts 21
cp 0
rs 6.7272
cc 7
eloc 17
nc 5
nop 2
crap 56
1
<?php
2
3
namespace MessageApp\Listener;
4
5
use League\Event\EventInterface;
6
use MessageApp\Event\UserEvent;
7
use MessageApp\Finder\MessageFinder;
8
use MessageApp\Message\DefaultMessage;
9
use MessageApp\Message\MessageFactory;
10
use MessageApp\Message\Sender\MessageSender;
11
use MessageApp\User\Finder\AppUserFinder;
12
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LoggerAwareTrait;
14
use Psr\Log\NullLogger;
15
use RemiSan\Context\Context;
16
17
class UserEventHandler implements MessageEventHandler, LoggerAwareInterface
18
{
19
    use LoggerAwareTrait;
20
21
    /**
22
     * @var AppUserFinder
23
     */
24
    private $userFinder;
25
26
    /**
27
     * @var MessageFinder
28
     */
29
    private $messageFinder;
30
31
    /**
32
     * @var MessageFactory
33
     */
34
    private $messageFactory;
35
36
    /**
37
     * @var MessageSender
38
     */
39
    private $messageSender;
40
41
    /**
42
     * Constructor
43
     *
44
     * @param AppUserFinder $userFinder
45
     * @param MessageFinder $messageFinder
46
     * @param MessageFactory $messageFactory
47
     * @param MessageSender $messageSender
48
     */
49
    public function __construct(
50
        AppUserFinder $userFinder,
51
        MessageFinder $messageFinder,
52
        MessageFactory $messageFactory,
53
        MessageSender $messageSender
54
    ) {
55
        $this->userFinder = $userFinder;
56
        $this->messageFinder = $messageFinder;
57
        $this->messageFactory = $messageFactory;
58
        $this->messageSender = $messageSender;
59
        $this->logger = new NullLogger();
60
    }
61
62
    /**
63
     * Handle an event.
64
     *
65
     * @param EventInterface $event
66
     * @param Context        $context
67
     *
68
     * @return void
69
     */
70
    public function handle(EventInterface $event, Context $context = null)
71
    {
72
        if (! ($event instanceof UserEvent && $event->getUserId() && $event->getAsMessage())) {
73
            return;
74
        }
75
76
        // Build message
77
        $user = $this->userFinder->find($event->getUserId());
78
79
        $this->logger->info(
80
            'Send message',
81
            [
82
                'user' => $user->getName(),
83
                'message' => $event->getAsMessage(),
84
                'type' => $event->getName()
85
            ]
86
        );
87
88
        $messageContext = null;
89
        if ($context) {
90
            $messageContext = $this->messageFinder->findByReference($context->getValue());
91
        }
92
93
        $message = $this->messageFactory->buildMessage([$user], $event);
94
95
        if (!$message) {
96
            $this->logger->warning('Message could not be generated');
97
            return;
98
        }
99
100
        $this->messageSender->send($message, ($messageContext) ? $messageContext->getSource() : null);
101
    }
102
}
103