Completed
Pull Request — master (#2737)
by Jeroen
17:30 queued 02:36
created

PasswordResetService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 16.95 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 10
loc 59
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A processResetRequest() 0 12 2
A resetPassword() 0 9 1
A dispatch() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Service;
4
5
use Kunstmaan\AdminBundle\Entity\UserInterface;
6
use Kunstmaan\AdminBundle\Event\ChangePasswordSuccessEvent;
7
use Kunstmaan\AdminBundle\Event\Events;
8
use Kunstmaan\AdminBundle\Service\AuthenticationMailer\AuthenticationMailerInterface;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14
15
class PasswordResetService
16
{
17
    /** @var UserManager */
18
    private $userManager;
19
    /** @var UrlGeneratorInterface */
20
    private $urlGenerator;
21
    /** @var EventDispatcherInterface */
22
    private $eventDispatcher;
23
    /** @var AuthenticationMailerInterface */
24
    private $authenticationMailer;
25
26
    public function __construct(UserManager $userManager, UrlGeneratorInterface $urlGenerator, EventDispatcherInterface $eventDispatcher, AuthenticationMailerInterface $authenticationMailer)
27
    {
28
        $this->userManager = $userManager;
29
        $this->urlGenerator = $urlGenerator;
30
        $this->eventDispatcher = $eventDispatcher;
31
        $this->authenticationMailer = $authenticationMailer;
32
    }
33
34
    public function processResetRequest(string $email, string $locale): void
35
    {
36
        $user = $this->userManager->findUserByUsernameOrEmail($email);
37
38
        if (null === $user) {
39
            //TODO: throw user not found exception
40
            throw new \Exception('User not found');
41
        }
42
43
        $this->userManager->setResetToken($user);
44
        $this->authenticationMailer->sendPasswordResetEmail($user, $locale);
45
    }
46
47
    public function resetPassword(UserInterface $user, string $newPassword): Response
48
    {
49
        $this->userManager->changePassword($user, $newPassword);
50
51
        $response = new RedirectResponse($this->urlGenerator->generate('KunstmaanAdminBundle_homepage'));
52
        $this->dispatch(new ChangePasswordSuccessEvent($user, $response), Events::CHANGE_PASSWORD_COMPLETED);
53
54
        return $response;
55
    }
56
57
    /**
58
     * @param object $event
59
     * @param string $eventName
60
     *
61
     * @return object
62
     */
63 View Code Duplication
    private function dispatch($event, string $eventName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        if (class_exists(LegacyEventDispatcherProxy::class)) {
66
            $eventDispatcher = LegacyEventDispatcherProxy::decorate($this->eventDispatcher);
67
68
            return $eventDispatcher->dispatch($event, $eventName);
69
        }
70
71
        return $this->eventDispatcher->dispatch($eventName, $event);
72
    }
73
}
74