Completed
Push — master ( d7e260...fc7aea )
by Artem
10:19
created

HandlesActions   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 114
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A actionUpdate() 0 24 4
A localNewerThanRemote() 0 15 4
A actionDelete() 0 9 2
A handleAction() 0 9 2
A actionCreate() 0 10 2
1
<?php
2
3
namespace Slides\Connector\Auth\Sync;
4
5
use Slides\Connector\Auth\Sync\Syncable as LocalUser;
6
use Slides\Connector\Auth\Sync\User as RemoteUser;
7
use Illuminate\Support\Facades\Auth;
8
use Slides\Connector\Auth\AuthService;
9
10
/**
11
 * Trait HandlesActions
12
 *
13
 * @property AuthService $authService
14
 *
15
 * @package Slides\Connector\Auth\Sync
16
 */
17
trait HandlesActions
18
{
19
    /**
20
     * Handle a user action.
21
     *
22
     * @param RemoteUser $remote
23
     * @param string $action
24
     *
25
     * @return void
26
     */
27
    protected function handleAction(RemoteUser $remote, string $action)
28
    {
29
        $handler = 'action' . studly_case($action);
30
31
        if(!method_exists($this, $handler)) {
32
            throw new \Slides\Connector\Auth\Exceptions\SyncException("User action handler {$handler} cannot be found.");
33
        }
34
35
        $this->{$handler}($remote);
36
    }
37
38
    /**
39
     * Handle the "create" action.
40
     *
41
     * @param RemoteUser $remote
42
     *
43
     * @return void
44
     */
45
    protected function actionCreate(RemoteUser $remote)
46
    {
47
        // If a user with the same email was found, we need to skip the process
48
        if(Auth::getProvider()->retrieveByCredentials(['email' => strtolower($remote->getEmail())])) {
49
            return;
50
        }
51
52
        $this->authService->handle(AuthService::HANDLER_USER_SYNC_CREATE, ['remote' => $remote]);
53
54
        $this->incrementStats('created');
0 ignored issues
show
Bug introduced by
It seems like incrementStats() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

54
        $this->/** @scrutinizer ignore-call */ 
55
               incrementStats('created');
Loading history...
55
    }
56
57
    /**
58
     * Handle the "update" action.
59
     *
60
     * @param RemoteUser $remote
61
     *
62
     * @return void
63
     */
64
    protected function actionUpdate(RemoteUser $remote)
65
    {
66
        // If a user with the same email cannot be found, we should skip the process
67
        if(!$local = Auth::getProvider()->retrieveByCredentials(['email' => $remote->getEmail()])) {
68
            return;
69
        }
70
71
        // If a local user was updated later than remote one, we should skip the process
72
        // Since we have a latest one
73
        if($this->localNewerThanRemote($local, $remote)) {
74
            return;
75
        }
76
77
        // If "password" mode is not enabled, we cannot update local passwords, so we simply resetting it
78
        if(!$this->hasMode(static::MODE_PASSWORDS)) {
0 ignored issues
show
Bug introduced by
It seems like hasMode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

78
        if(!$this->/** @scrutinizer ignore-call */ hasMode(static::MODE_PASSWORDS)) {
Loading history...
Bug introduced by
The constant Slides\Connector\Auth\Sy...Actions::MODE_PASSWORDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
79
            $remote->resetPassword();
80
        }
81
82
        $this->authService->handle(AuthService::HANDLER_USER_SYNC_UPDATE, [
83
            'remote' => $remote,
84
            'local' => $local
85
        ]);
86
87
        $this->incrementStats('updated');
88
    }
89
90
    /**
91
     * Handle the "delete" action.
92
     *
93
     * @param RemoteUser $remote
94
     *
95
     * @return void
96
     */
97
    protected function actionDelete(RemoteUser $remote)
98
    {
99
        if(!$local = Auth::getProvider()->retrieveByCredentials(['email' => $remote->getEmail()])) {
100
            return;
101
        }
102
103
        $this->authService->handle(AuthService::HANDLER_USER_SYNC_DELETE, ['remote' => $remote, 'local' => $local]);
104
105
        $this->incrementStats('deleted');
106
    }
107
108
    /**
109
     * Check whether remote user has updated earlier than local one.
110
     *
111
     * @param Syncable|\Illuminate\Contracts\Auth\Authenticatable $local
112
     * @param User $remote
113
     *
114
     * @return bool
115
     */
116
    private function localNewerThanRemote(LocalUser $local, RemoteUser $remote)
117
    {
118
        if(!$local->retrieveRemoteId()) {
119
            return false;
120
        }
121
122
        if(!$remoteUpdated = $remote->getUpdated()) {
123
            return false;
124
        }
125
126
        if(!$localUpdate = $local->retrieveUpdatedAt()) {
127
            return false;
128
        }
129
130
        return $remoteUpdated->lessThan($localUpdate);
131
    }
132
}