Completed
Push — master ( 063bdc...995895 )
by Mahmoud
04:00
created

UpdateVisitorUserAction::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 10
nc 3
nop 4
1
<?php
2
3
namespace App\Containers\User\Actions;
4
5
use App\Containers\ApiAuthentication\Exceptions\MissingVisitorIdException;
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
12
/**
13
 * Class UpdateVisitorUserAction.
14
 *
15
 * @author Mahmoud Zalt <[email protected]>
16
 */
17
class UpdateVisitorUserAction extends Action
18
{
19
20
    /**
21
     * @var  \App\Containers\User\Services\UpdateUserService
22
     */
23
    private $updateUserService;
24
25
    /**
26
     * @var  \App\Containers\User\Services\FindUserService
27
     */
28
    private $findUserService;
29
30
    /**
31
     * @var  \App\Containers\User\Services\CreateUserService
32
     */
33
    private $createUserService;
34
35
    /**
36
     * @var  \App\Containers\ApiAuthentication\Services\ApiAuthenticationService
37
     */
38
    private $apiAuthenticationService;
39
40
    /**
41
     * UpdateUserAction constructor.
42
     *
43
     * @param \App\Containers\User\Services\UpdateUserService                     $updateUserService
44
     * @param \App\Containers\User\Services\FindUserService                       $findUserService
45
     * @param \App\Containers\User\Services\CreateUserService                     $createUserService
46
     * @param \App\Containers\ApiAuthentication\Services\ApiAuthenticationService $apiAuthenticationService
47
     */
48
    public function __construct(
49
        UpdateUserService $updateUserService,
50
        FindUserService $findUserService,
51
        CreateUserService $createUserService,
52
        ApiAuthenticationService $apiAuthenticationService
53
    ) {
54
        $this->updateUserService = $updateUserService;
55
        $this->findUserService = $findUserService;
56
        $this->createUserService = $createUserService;
57
        $this->apiAuthenticationService = $apiAuthenticationService;
58
    }
59
60
    /**
61
     * This will register an existing User Visitor. After being created by the middleware.
62
     * Only case the "Registration by Device ID" feature is enabled, via its middleware.
63
     *
64
     * @param      $visitorId
65
     * @param null $email
66
     * @param null $password
67
     * @param null $name
68
     *
69
     * @return  mixed
70
     */
71
    public function run($visitorId, $email = null, $password = null, $name = null)
72
    {
73
        if (!$visitorId) {
74
            throw (new MissingVisitorIdException())->debug('from (UpdateVisitorUserAction)');
75
        }
76
77
        $user = $this->findUserService->byVisitorId($visitorId);
78
79
        if ($user) {
80
            // update the existing user by adding his credentials
81
            $user = $this->updateUserService->run($user->id, $password, $name, $email);
82
            // Login the User from his object
83
            $user = $this->apiAuthenticationService->loginFromObject($user);
84
        } else {
85
            // create the user now, in case that user have registered from the first screen
86
            $user = $this->createUserService->byCredentials($email, $password, $name, true);
87
        }
88
89
        return $user;
90
    }
91
}
92