Completed
Push — master ( 97d2bc...038e4e )
by Tomáš
12:56
created

UserControllerSubscriber   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 6 1
B postWrite() 0 31 6
1
<?php
2
3
namespace Webcook\Cms\SecurityBundle\EventSubscriber;
4
5
use ApiPlatform\Core\EventListener\EventPriorities;
6
use Webcook\Cms\SecurityBundle\Entity\User;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
final class UserControllerSubscriber implements EventSubscriberInterface
13
{
14
    private $em;
15
16
    private $encoderFactory;
17
18
    public function __construct($em, $encoderFactory)
19
    {
20
        $this->em             = $em;
21
        $this->encoderFactory = $encoderFactory;
22
    }
23
24
    public static function getSubscribedEvents()
25
    {
26
        return [
27
            KernelEvents::VIEW => [['postWrite', EventPriorities::POST_WRITE]],
28
        ];
29
    }
30
31
    public function postWrite(GetResponseForControllerResultEvent $event)
32
    {
33
        $user   = $event->getControllerResult();
34
        $method = $event->getRequest()->getMethod();
35
36
        if (!$user instanceof User) {
37
            return;
38
        }
39
40
        $data = json_decode($event->getRequest()->getContent());
41
42
        if (isset($data->password) && !empty($data->password)) {
43
            if ($method === 'PUT') {
44
                $oldPassword = $user->getPassword();
45
            }
46
47
            $plainPassword = $user->getPassword();
48
49
            if (!empty($plainPassword)) {
50
                $factory  = $this->encoderFactory;
51
                $encoder  = $factory->getEncoder($user);
52
                $password = $encoder->encodePassword($plainPassword, $user->getSalt());
53
                $user->setPassword($password);
54
            } else {
55
                $user->setPassword($oldPassword);
0 ignored issues
show
Bug introduced by
The variable $oldPassword does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
56
            }
57
58
            $this->em->persist($user);
59
            $this->em->flush();
60
        }
61
    }
62
}
63