SyncUsers::handle()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 26
rs 9.8333
1
<?php
2
3
namespace Slides\Connector\Auth\Commands;
4
5
use Slides\Connector\Auth\AuthService;
6
use Slides\Connector\Auth\Client as AuthClient;
7
use Slides\Connector\Auth\Sync\Syncer;
8
use Slides\Connector\Auth\Helpers\ConsoleHelper;
9
use Slides\Connector\Auth\Concerns\PassesModes;
10
11
/**
12
 * Class SyncUsers
13
 *
14
 * @package Slides\Connector\Auth\Commands
15
 */
16
class SyncUsers extends \Illuminate\Console\Command
17
{
18
    use PassesModes;
19
20
    /**
21
     * The name and signature of the console command.
22
     *
23
     * @var string
24
     */
25
    protected $signature = 'connector:sync-users
26
                            {--passwords : Allow syncing passwords (can rewrite remotely and locally) }
27
                            {--users=    : Sync the specific users }';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Synchronize users with the remote authentication service';
35
36
    /**
37
     * @var AuthClient
38
     */
39
    protected $authClient;
40
41
    /**
42
     * @var AuthService
43
     */
44
    protected $authService;
45
46
    /**
47
     * SyncUsers constructor.
48
     *
49
     * @param AuthService $authService
50
     */
51
    public function __construct(AuthService $authService)
52
    {
53
        $this->authService = $authService;
54
55
        parent::__construct();
56
    }
57
58
    /**
59
     * Execute the console command.
60
     *
61
     * @return void
62
     */
63
    public function handle()
64
    {
65
        $this->displayModes();
66
67
        $syncer = new Syncer($locals = $this->syncingUsers(), $this->modes());
68
69
        if($locals->isEmpty()) {
70
            $this->info('No local users found.');
71
        }
72
73
        if(!$this->confirm('There are ' . $locals->count() . ' local user(s) to sync. Continue?', $this->option('no-interaction'))) {
74
            return;
75
        }
76
77
        $syncer->setOutputCallback(function(string $message) {
78
            $this->info('[Syncer] ' . $message);
79
        });
80
81
        $duration = $this->measure(function() use ($syncer) {
82
            $syncer->sync();
83
        });
84
85
        $this->writeStats('Remote changes', $syncer->getRemoteStats());
86
        $this->writeStats('Local changes', $syncer->getLocalStats());
87
88
        $this->info("Finished in {$duration}s.");
89
    }
90
91
    /**
92
     * Output the stats.
93
     *
94
     * @param string $title
95
     * @param array $stats
96
     */
97
    private function writeStats(string $title, array $stats)
98
    {
99
        $this->output->title($title);
100
        $this->output->table(array_keys($stats), array(array_values($stats)));
101
    }
102
103
    /**
104
     * Measure an execution time of the callback.
105
     *
106
     * @param \Closure $callback
107
     *
108
     * @return float
109
     */
110
    public function measure(\Closure $callback)
111
    {
112
        $start = microtime(true);
113
114
        $callback();
115
116
        $end = microtime(true);
117
118
        return round($end - $start, 2);
119
    }
120
121
    /**
122
     * Retrieve users to sync.
123
     *
124
     * @return \Illuminate\Support\Collection
125
     */
126
    private function syncingUsers()
127
    {
128
        if (!$this->hasMode(Syncer::MODE_USERS)) {
129
            return Syncer::retrieveLocals();
130
        }
131
132
        if (!count($ids = ConsoleHelper::stringToArray($this->option('users')))) {
133
            throw new \InvalidArgumentException('No users passed');
134
        }
135
136
        return \Illuminate\Support\Facades\Auth::getProvider()->createModel()
137
            ->newQuery()
138
            ->whereIn('id', $ids)
139
            ->get();
140
    }
141
}