PrepareUserListener::getSubscribedEvents()   A
last analyzed

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
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminSecurityBundle\EventListener;
13
14
use FSi\Bundle\AdminBundle\Event\FormEvent;
15
use FSi\Bundle\AdminBundle\Event\FormEvents;
16
use FSi\Bundle\AdminSecurityBundle\Event\UserEvent;
17
use FSi\Bundle\AdminSecurityBundle\Security\User\UserInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
21
22
class PrepareUserListener implements EventSubscriberInterface
23
{
24
    /**
25
     * @var EventDispatcherInterface
26
     */
27
    private $eventDispatcher;
28
29
    public function __construct(EventDispatcherInterface $eventDispatcher)
30
    {
31
        $this->eventDispatcher = $eventDispatcher;
32
    }
33
34
    public static function getSubscribedEvents(): array
35
    {
36
        return [
37
            FormEvents::FORM_DATA_PRE_SAVE => [
38
                'prepareAndDispatchUserCreated',
39
            ]
40
        ];
41
    }
42
43
    public function prepareAndDispatchUserCreated(FormEvent $event): void
44
    {
45
        $entity = $event->getForm()->getData();
46
47
        if (!$entity instanceof UserInterface) {
48
            return;
49
        }
50
51
        // do not dispatch event and do not set random password for existing users
52
        if ($entity->getPassword()) {
53
            return;
54
        }
55
56
        $entity->setEnabled(false);
57
        $entity->enforcePasswordChange(true);
58
        $entity->setPlainPassword(bin2hex(random_bytes(32)));
59
60
        $this->eventDispatcher->dispatch(
61
            AdminSecurityEvents::USER_CREATED,
0 ignored issues
show
Bug introduced by
FSi\Bundle\AdminSecurity...ityEvents::USER_CREATED of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            /** @scrutinizer ignore-type */ AdminSecurityEvents::USER_CREATED,
Loading history...
62
            new UserEvent($entity)
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with new FSi\Bundle\AdminSecu...vent\UserEvent($entity). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
63
                                dispatch(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
63
        );
64
    }
65
}
66