1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of CarcelUserBundle. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Damien Carcel <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Carcel\Bundle\UserBundle\Handler; |
13
|
|
|
|
14
|
|
|
use Carcel\Bundle\UserBundle\Event\UserEvents; |
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
16
|
|
|
use FOS\UserBundle\Model\UserInterface; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Damien Carcel <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class UserStatusHandler implements UserStatusHandlerInterface |
24
|
|
|
{ |
25
|
|
|
/** @var EventDispatcherInterface */ |
26
|
|
|
protected $eventDispatcher; |
27
|
|
|
|
28
|
|
|
/** @var EntityManagerInterface */ |
29
|
|
|
protected $entityManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
33
|
|
|
* @param EntityManagerInterface $entityManager |
34
|
|
|
*/ |
35
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, EntityManagerInterface $entityManager) |
36
|
|
|
{ |
37
|
|
|
$this->eventDispatcher = $eventDispatcher; |
38
|
|
|
$this->entityManager = $entityManager; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function enable(UserInterface $user) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$this->eventDispatcher->dispatch(UserEvents::PRE_ACTIVATE, new GenericEvent($user)); |
47
|
|
|
|
48
|
|
|
$user->setEnabled(true); |
49
|
|
|
|
50
|
|
|
$this->entityManager->flush(); |
51
|
|
|
|
52
|
|
|
$this->eventDispatcher->dispatch(UserEvents::POST_ACTIVATE, new GenericEvent($user)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function disable(UserInterface $user) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->eventDispatcher->dispatch(UserEvents::PRE_DEACTIVATE, new GenericEvent($user)); |
61
|
|
|
|
62
|
|
|
$user->setEnabled(false); |
63
|
|
|
|
64
|
|
|
$this->entityManager->flush(); |
65
|
|
|
|
66
|
|
|
$this->eventDispatcher->dispatch(UserEvents::POST_DEACTIVATE, new GenericEvent($user)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
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.