ForgotPassword   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 61.29%

Importance

Changes 0
Metric Value
wmc 8
eloc 33
c 0
b 0
f 0
dl 0
loc 114
ccs 19
cts 31
cp 0.6129
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setEventManager() 0 4 1
A setSuffix() 0 4 1
A __construct() 0 10 1
A proceed() 0 31 4
A getEventManager() 0 3 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license       MIT
8
 */
9
10
namespace Auth\Service;
11
12
use Auth\Repository;
13
use Auth\Service\Exception\UserDoesNotHaveAnEmailException;
14
use Auth\Service\Exception\UserNotFoundException;
15
use Auth\Options\ModuleOptions;
16
use Core\Controller\Plugin;
17
use Laminas\InputFilter\InputFilterInterface;
18
use Laminas\Mvc\Controller\Plugin\Url;
19
use Laminas\EventManager\EventManagerAwareInterface;
20
use Laminas\EventManager\EventManagerInterface;
21
use Auth\Filter\LoginFilter;
22
use Auth\Listener\Events\AuthEvent;
23
24
/**
25
 * Class ForgotPassword
26
 * @package Auth\Service
27
 */
28
class ForgotPassword
29
{
30
    /**
31
     * @var Repository\User
32
     */
33
    private $userRepository;
34
35
    /**
36
     * @var UserUniqueTokenGenerator
37
     */
38
    private $tokenGenerator;
39
40
    /**
41
     * @var LoginFilter
42
     */
43
    private $loginFilter;
44
45
    /**
46
     * @var EventManagerInterface
47
     */
48
    protected $eventManager;
49
50
    /**
51
     * @var string
52
     */
53
    protected $suffix;
54
55
    /**
56
     * @var \Auth\Options\ModuleOptions
57
     */
58
    protected $options;
59
60
    /**
61
     * @param Repository\User $userRepository
62
     * @param UserUniqueTokenGenerator $tokenGenerator
63
     * @param LoginFilter $loginFilter
64
     */
65 4
    public function __construct(
66
        Repository\User $userRepository,
67
        UserUniqueTokenGenerator $tokenGenerator,
68
        LoginFilter $loginFilter,
69
        ModuleOptions $options
70
    ) {
71 4
        $this->userRepository = $userRepository;
72 4
        $this->tokenGenerator = $tokenGenerator;
73 4
        $this->loginFilter = $loginFilter;
74 4
        $this->options = $options;
75
    }
76
77
    public function setSuffix($suffix)
78
    {
79
        $this->suffix = $suffix;
80
        return $this;
81
    }
82
83
    /**
84
     * @param EventManagerInterface $eventManager
85
     * @return $this|void
86
     */
87 1
    public function setEventManager(EventManagerInterface $eventManager)
88
    {
89 1
        $this->eventManager = $eventManager;
90 1
        return $this;
91
    }
92
93
    /**
94
     * @return EventManagerInterface
95
     */
96 1
    public function getEventManager()
97
    {
98 1
        return $this->eventManager;
99
    }
100
101
    /**
102
     * @todo remove unused $mailer parameter an fix tests
103
     *
104
     * @param InputFilterInterface $filter
105
     * @param Plugin\Mailer $mailer
106
     * @param Url $url
107
     * @throws \LogicException
108
     * @throws UserDoesNotHaveAnEmailException
109
     * @throws UserNotFoundException
110
     */
111 3
    public function proceed(InputFilterInterface $filter, Plugin\Mailer $mailer, Url $url)
0 ignored issues
show
Unused Code introduced by
The parameter $mailer is not used and could be removed. ( Ignorable by Annotation )

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

111
    public function proceed(InputFilterInterface $filter, /** @scrutinizer ignore-unused */ Plugin\Mailer $mailer, Url $url)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113 3
        if (!$filter->isValid()) {
114 1
            throw new \LogicException('Form is not valid');
115
        }
116
117 2
        $identity = $filter->getValue('identity');
118
119 2
        $suffix = $this->loginFilter->filter();
120
121 2
        if (!($user = $this->userRepository->findByLoginOrEmail($identity, $suffix))) {
122 1
            throw new UserNotFoundException('User is not found');
123
        }
124
125 1
        if (!($email = $user->getInfo()->getEmail())) {
0 ignored issues
show
Unused Code introduced by
The assignment to $email is dead and can be removed.
Loading history...
126 1
            throw new UserDoesNotHaveAnEmailException('User does not have an email');
127
        }
128
129
        $tokenHash = $this->tokenGenerator->generate($user);
130
131
        $resetLink = $url->fromRoute(
132
            'lang/goto-reset-password',
133
            array('token' => $tokenHash, 'userId' => $user->getId()),
134
            array('force_canonical' => true)
135
        );
136
137
        $e = new AuthEvent();
138
        $e->setResetLink($resetLink);
139
        $e->setUser($user);
140
141
        $this->eventManager->trigger(AuthEvent::EVENT_AUTH_NEWPASSWORD, $e);
142
    }
143
}
144