Completed
Push — develop ( 09456b...2094a2 )
by Mathias
14s queued 10s
created

InviteEmployeeController::createErrorViewModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license    MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
10
/** */
11
namespace Organizations\Controller;
12
13
use Organizations\Controller\Plugin\AcceptInvitationHandler;
14
use Zend\Mvc\Controller\AbstractActionController;
15
use Zend\View\Model\JsonModel;
16
use Zend\View\Model\ViewModel;
17
use Organizations\Repository\Organization as OrganizationRepository;
18
19
/**
20
 * This controller is responsible for the user invitation process.
21
 *
22
 * Which consists on these tasks:
23
 * - Create temporary users if necessary or find the existent one for an email address.
24
 * - Send an invitation mail.
25
 * - Handles the acceptance of such an invitation.
26
 *
27
 * @author Mathias Gelhausen <[email protected]>
28
 * @author Anthonius Munthi <[email protected]>
29
 * @since  0.19
30
 */
31
class InviteEmployeeController extends AbstractActionController
32
{
33
    /**
34
     * @var OrganizationRepository
35
     */
36
    private $orgRepo;
37
38
	public function __construct(
39
        OrganizationRepository $orgRepo
40
    )
41
    {
42
        $this->orgRepo = $orgRepo;
43
    }
44
45
    /**
46
     * Invitation first step: Create or find user and send mail.
47
     *
48
     * @return JsonModel
49
     */
50
    public function inviteAction()
51
    {
52
        $email   = $this->params()->fromQuery('email');
53
        $handler = $this->plugin('Organizations/InvitationHandler');
54
        $result  = $handler->process($email);
0 ignored issues
show
Bug introduced by
The method process() does not seem to exist on object<Zend\Stdlib\DispatchableInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
56
        return new JsonModel($result);
57
    }
58
59
    /**
60
     * Invitation second step: Acceptance of the invitation.
61
     *
62
     * @return ViewModel
63
     */
64
    public function acceptAction()
65
    {
66
        /* @var $request \Zend\Http\PhpEnvironment\Request */
67
        $request = $this->getRequest();
68
69
        if ($request->isPost()) {
70
            return $this->createSetPasswordViewModel();
71
        }
72
73
        $token        = $this->params()->fromQuery('token');
74
        $organization = $this->params()->fromQuery('organization');
75
76
        /* @var $handler \Organizations\Controller\Plugin\AcceptInvitationHandler */
77
        $handler = $this->plugin('Organizations/AcceptInvitationHandler');
78
        $result  = $handler->process($token, $organization);
79
80
        switch ($result) {
81
            default:
82
            case AcceptInvitationHandler::OK:
83
                $model = $this->createSuccessViewModel();
84
                break;
85
86
            case AcceptInvitationHandler::OK_SET_PW:
87
                $model = $this->createSetPasswordViewModel();
88
                break;
89
90
            case AcceptInvitationHandler::ERROR_ORGANIZATION_NOT_FOUND:
91
                $model = $this->createErrorViewModel(
92
                    /*@translate*/ 'The organization referenced in your request could not be found.'
93
                );
94
                break;
95
96
            case AcceptInvitationHandler::ERROR_TOKEN_INVALID:
97
                $model = $this->createErrorViewModel(
98
                    /*@translate*/ 'The access token you provided seems to have expired.'
99
                );
100
                break;
101
        }
102
103
        return $model;
104
    }
105
106
    /**
107
     * Creates a view model for success view script with set password form.
108
     *
109
     * @return ViewModel
110
     */
111
    protected function createSetPasswordViewModel()
112
    {
113
        $organization = $this->getOrganizationEntity();
114
        $result       = $this->forward()->dispatch('Auth\Controller\Password', array('action' => 'index'));
115
        $model        = new ViewModel(
116
            array(
117
                'organization' => $organization->getOrganizationName()->getName()
118
            )
119
        );
120
121
        if (!$result->getVariable('valid', false)) {
122
            $model->setVariable('form', $result->getVariable('form'));
123
        }
124
125
        return $model;
126
    }
127
128
    /**
129
     * Gets the referenced organization entity.
130
     *
131
     * Retrieves the identifier from the requests' query params.
132
     *
133
     * @return \Organizations\Entity\OrganizationInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be \Organizations\Entity\OrganizationInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
134
     */
135
    protected function getOrganizationEntity()
136
    {
137
        /* @var $orgRepo \Organizations\Repository\Organization */
138
        $orgRepo = $this->orgRepo;
139
        $organiationId = $this->params()->fromQuery('organization');
140
141
        $organization = $orgRepo->find($organiationId);
142
143
        return $organization;
144
    }
145
146
    /**
147
     * Creates the view model for the success view script.
148
     *
149
     * @return ViewModel
150
     */
151
    protected function createSuccessViewModel()
152
    {
153
        $organization = $this->getOrganizationEntity();
154
155
        return new ViewModel(
156
            array(
157
                'organization' => $organization->getOrganizationName()->getName()
158
            )
159
        );
160
    }
161
162
    /**
163
     * Creates a view model for the error page view script.
164
     *
165
     * Sets the response status code to 500 (indicating an internal error).
166
     *
167
     * @param string $message
168
     *
169
     * @return ViewModel
170
     */
171 View Code Duplication
    protected function createErrorViewModel($message)
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...
172
    {
173
        /* @var $response \Zend\Http\Response */
174
        $response = $this->getResponse();
175
        $response->setStatusCode(500);
176
177
        $model = new ViewModel(array('message' => $message));
178
        $model->setTemplate('organizations/error/invite');
179
180
        return $model;
181
    }
182
}
183