Completed
Push — master ( 19e3bd...22f213 )
by Guilherme
17:20
created

NfgSubscriber::onConnectCallbackResponse()   C

Complexity

Conditions 8
Paths 19

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 19
nop 1
dl 0
loc 32
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PROCERGS\LoginCidadao\NfgBundle\EventListener;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use PROCERGS\LoginCidadao\NfgBundle\Event\GetConnectCallbackResponseEvent;
15
use PROCERGS\LoginCidadao\NfgBundle\NfgEvents;
16
use Psr\Log\LoggerAwareInterface;
17
use Psr\Log\LoggerAwareTrait;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
20
class NfgSubscriber implements EventSubscriberInterface, LoggerAwareInterface
21
{
22
    use LoggerAwareTrait;
23
    /** @var EntityManagerInterface */
24
    private $em;
25
26
    /**
27
     * NfgSubscriber constructor.
28
     * @param EntityManagerInterface $em
29
     */
30
    public function __construct(EntityManagerInterface $em)
31
    {
32
        $this->em = $em;
33
    }
34
35
    /**
36
     * Returns an array of event names this subscriber wants to listen to.
37
     *
38
     * The array keys are event names and the value can be:
39
     *
40
     *  * The method name to call (priority defaults to 0)
41
     *  * An array composed of the method name to call and the priority
42
     *  * An array of arrays composed of the method names to call and respective
43
     *    priorities, or 0 if unset
44
     *
45
     * For instance:
46
     *
47
     *  * array('eventName' => 'methodName')
48
     *  * array('eventName' => array('methodName', $priority))
49
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
50
     *
51
     * @return array The event names to listen to
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<*,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
52
     */
53
    public static function getSubscribedEvents()
54
    {
55
        return [
56
            NfgEvents::CONNECT_CALLBACK_RESPONSE => 'onConnectCallbackResponse',
57
        ];
58
    }
59
60
    /**
61
     * Complete missing information with data from NFG.
62
     *
63
     * @param GetConnectCallbackResponseEvent $event
64
     */
65
    public function onConnectCallbackResponse(GetConnectCallbackResponseEvent $event)
66
    {
67
        $nfgProfile = $event->getPersonMeuRS()->getNfgProfile();
68
        $person = $event->getPersonMeuRS()->getPerson();
69
70
        if (!$person || !$nfgProfile) {
71
            return;
72
        }
73
74
        $updated = false;
75
        if (!$person->getMobile()) {
76
            if ($this->logger) {
77
                $this->logger->notice('Updating user\'s phone number with data from NFG',
78
                    ['person_id' => $person->getId()]);
79
            }
80
            $person->setMobile($nfgProfile->getMobile());
81
            $updated = true;
82
        }
83
        if (!$person->getBirthdate()) {
84
            if ($this->logger) {
85
                $this->logger->notice('Updating user\'s birthday with data from NFG',
86
                    ['person_id' => $person->getId()]);
87
            }
88
            $person->setBirthdate($nfgProfile->getBirthdate());
89
            $updated = true;
90
        }
91
92
        if ($updated) {
93
            $this->em->persist($person);
94
            $this->em->flush();
95
        }
96
    }
97
}
98