Passed
Pull Request — master (#6127)
by Angel Fernando Quiroz
07:52
created

getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
use Chamilo\CoreBundle\Event\UserCreatedEvent;
8
use Chamilo\CoreBundle\Event\Events;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11
class CreateDrupalUserEventSubscriber implements EventSubscriberInterface
12
{
13
    private CreateDrupalUser $plugin;
14
15
    public function __construct()
16
    {
17
        $this->plugin = CreateDrupalUser::create();
18
    }
19
20
    /**
21
     * @inheritDoc
22
     */
23
    public static function getSubscribedEvents(): array
24
    {
25
        return [
26
            Events::USER_CREATED => 'onCreateUser',
27
        ];
28
    }
29
30
    public function onCreateUser(UserCreatedEvent $event): void
31
    {
32
        if (!$this->plugin->isEnabled(true)) {
33
            return;
34
        }
35
36
        $drupalDomain = $this->plugin->get('drupal_domain');
37
        $drupalDomain = rtrim($drupalDomain, '/').'/';
38
39
        if (HOOK_EVENT_TYPE_POST === $event->getType()) {
40
            $return = $event->getUser();
41
            $originalPassword = $event->getOriginalPassword();
42
43
            $userInfo = api_get_user_info($return->getId());
44
            $fields = [
45
                'name' => $userInfo['username'],
46
                'pass' => $originalPassword,
47
                'mail' => $userInfo['email'],
48
                'status' => 1,
49
                'init' => $userInfo['email'],
50
            ];
51
52
            $extraFields = [
53
                'first_name' => $userInfo['firstname'],
54
                'last_name' => $userInfo['lastname'],
55
            ];
56
57
            $options = [
58
                'location' => $drupalDomain.'sites/all/modules/chamilo/soap.php?wsdl',
59
                'uri' => $drupalDomain,
60
            ];
61
62
            $client = new SoapClient(null, $options);
63
            $drupalUserId = false;
64
65
            if (isset($_SESSION['ws_drupal_user_id'])) {
66
                //$drupalUserId = $_SESSION['ws_drupal_user_id'];
67
68
                return;
69
            }
70
71
            if (false === $drupalUserId) {
72
                $drupalUserId = $client->addUser($fields, $extraFields);
73
            }
74
75
            if (false !== $drupalUserId) {
76
                UserManager::update_extra_field_value($return->getId(), 'drupal_user_id', $drupalUserId);
77
            }
78
        }
79
    }
80
}
81