SyncExport   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 38
c 3
b 2
f 0
dl 0
loc 115
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A measure() 0 9 1
A filePath() 0 9 2
A handle() 0 38 3
A syncingUsers() 0 10 2
1
<?php
2
3
namespace Slides\Connector\Auth\Commands;
4
5
use Illuminate\Support\Str;
6
use Slides\Connector\Auth\Sync\Syncer;
7
use Slides\Connector\Auth\Helpers\ConsoleHelper;
8
use Slides\Connector\Auth\Concerns\PassesModes;
9
10
/**
11
 * Class SyncExport
12
 *
13
 * @package Slides\Connector\Auth\Commands
14
 */
15
class SyncExport extends \Illuminate\Console\Command
16
{
17
    use PassesModes;
18
19
    /**
20
     * The name and signature of the console command.
21
     *
22
     * @var string
23
     */
24
    protected $signature = 'connector:sync-export
25
                            {--path=  : Allow syncing passwords (can rewrite remotely and locally) }
26
                            {--users= : Export the specific users }
27
                            {--passwords : Export the specific users }';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Export local users to sync users remotely (can be imported only remotely)';
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return void
40
     */
41
    public function handle()
42
    {
43
        $this->displayModes();
44
45
        $syncer = new Syncer($locals = $this->syncingUsers(), $this->modes());
46
47
        if($locals->isEmpty()) {
48
            $this->info('No local users found.');
49
        }
50
51
        if(!$this->confirm('There are ' . $locals->count() . ' local user(s) to export. Continue?', $this->option('no-interaction'))) {
52
            return;
53
        }
54
55
        $syncer->setOutputCallback(function(string $message) {
56
            $this->info('[Syncer] ' . $message);
57
        });
58
59
        $filePath = $this->filePath();
60
61
        $duration = $this->measure(function() use ($syncer, $filePath) {
62
            $syncer->export($filePath);
63
        });
64
65
        $this->info('Dump has been saved to ' . $filePath);
66
        $this->info('Encryption key: ' . $syncer->getEncryptionKey());
67
68
        $filename = basename($filePath);
69
70
        $this->output->note(
71
            'This encryption key is unique for each dump and supposed to be used safely.'
72
            . PHP_EOL . 'It\'s bound to this service and cannot be used on other tenants.'
73
        );
74
75
        $this->output->block('To sync the dump, run the following command on Authentication Service:');
76
        $this->output->block("php artisan sync:import-dump {$filename} --key \"{$syncer->getEncryptionKey()}\"", null, 'fg=cyan;bg=default');
77
78
        $this->info("Finished in {$duration}s.");
79
    }
80
81
    /**
82
     * Measure an execution time of the callback.
83
     *
84
     * @param \Closure $callback
85
     *
86
     * @return float
87
     */
88
    public function measure(\Closure $callback)
89
    {
90
        $start = microtime(true);
91
92
        $callback();
93
94
        $end = microtime(true);
95
96
        return round($end - $start, 2);
97
    }
98
99
    /**
100
     * Retrieve users to sync.
101
     *
102
     * @return \Illuminate\Support\Collection
103
     */
104
    private function syncingUsers()
105
    {
106
        if(!count($ids = ConsoleHelper::stringToArray($this->option('users')))) {
107
            return Syncer::retrieveLocals();
108
        }
109
110
        return \Illuminate\Support\Facades\Auth::getProvider()->createModel()
111
            ->newQuery()
112
            ->whereIn('id', $ids)
113
            ->get();
114
    }
115
116
    /**
117
     * Retrieve a file path.
118
     *
119
     * @return string
120
     */
121
    private function filePath()
122
    {
123
        if(!$path = $this->option('path')) {
124
            $path = storage_path('app');
125
        }
126
127
        $datetime = (new \Carbon\Carbon())->format('Y_m_d_His');
128
129
        return Str::finish($path, '/') . 'sync_export_' . $datetime . '.gz';
130
    }
131
}