Completed
Push — master ( 4d6b90...85818a )
by Mahmoud
03:14
created

UpdateAgentUserAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 6
dl 0
loc 74
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A run() 0 22 3
1
<?php
2
3
namespace App\Containers\User\Actions;
4
5
use App\Containers\ApiAuthentication\Exceptions\MissingAgentIdException;
6
use App\Containers\ApiAuthentication\Services\ApiAuthenticationService;
7
use App\Containers\User\Services\CreateUserService;
8
use App\Containers\User\Services\FindUserService;
9
use App\Containers\User\Services\UpdateUserService;
10
use App\Port\Action\Abstracts\Action;
11
use App\Port\Exception\Exceptions\InternalErrorException;
12
13
/**
14
 * Class UpdateAgentUserAction.
15
 *
16
 * @author Mahmoud Zalt <[email protected]>
17
 */
18
class UpdateAgentUserAction extends Action
19
{
20
21
    /**
22
     * @var  \App\Containers\User\Services\UpdateUserService
23
     */
24
    private $updateUserService;
25
26
    /**
27
     * @var  \App\Containers\User\Services\FindUserService
28
     */
29
    private $findUserService;
30
31
    /**
32
     * @var  \App\Containers\User\Services\CreateUserService
33
     */
34
    private $createUserService;
35
36
    /**
37
     * @var  \App\Containers\ApiAuthentication\Services\ApiAuthenticationService
38
     */
39
    private $apiAuthenticationService;
40
41
    /**
42
     * UpdateUserAction constructor.
43
     *
44
     * @param \App\Containers\User\Services\UpdateUserService                     $updateUserService
45
     * @param \App\Containers\User\Services\FindUserService                       $findUserService
46
     * @param \App\Containers\User\Services\CreateUserService                     $createUserService
47
     * @param \App\Containers\ApiAuthentication\Services\ApiAuthenticationService $apiAuthenticationService
48
     */
49
    public function __construct(
50
        UpdateUserService $updateUserService,
51
        FindUserService $findUserService,
52
        CreateUserService $createUserService,
53
        ApiAuthenticationService $apiAuthenticationService
54
    ) {
55
        $this->updateUserService = $updateUserService;
56
        $this->findUserService = $findUserService;
57
        $this->createUserService = $createUserService;
58
        $this->apiAuthenticationService = $apiAuthenticationService;
59
    }
60
61
    /**
62
     * @param      $agentId
63
     * @param null $email
64
     * @param null $password
65
     * @param null $name
66
     *
67
     * @return  mixed
68
     */
69
    public function run($agentId, $email = null, $password = null, $name = null)
70
    {
71
        if (!$agentId) {
72
            throw (new MissingAgentIdException())->debug('from (UpdateAgentUserAction)');
73
        }
74
75
        $user = $this->findUserService->byAgentId($agentId);
76
77
        // There should be no way a user is not created, since before hitting this action's endpoint
78
        // a middleware already checking if the user exist by his Agent-Id header, and if not found
79
        // he automatically creates it.
80
        if (!$user) {
81
            throw new InternalErrorException('Something went wrong while registering a user!');
82
        }
83
84
        $user = $this->updateUserService->run($user->id, $password, $name, $email);
85
86
        // Login the User from its object
87
        $user = $this->apiAuthenticationService->loginFromObject($user);
88
89
        return $user;
90
    }
91
}
92