SyncImport   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 31
c 3
b 0
f 0
dl 0
loc 99
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 35 5
A flushListeners() 0 12 4
A measure() 0 9 1
1
<?php
2
3
namespace Slides\Connector\Auth\Commands;
4
5
use Slides\Connector\Auth\Sync\Syncer;
6
use Slides\Connector\Auth\Concerns\PassesModes;
7
8
/**
9
 * Class SyncImport
10
 *
11
 * @package Slides\Connector\Auth\Commands
12
 */
13
class SyncImport extends \Illuminate\Console\Command
14
{
15
    use PassesModes;
16
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'connector:sync-import {filename}
23
                            { --k|key=    : Encryption key }
24
                            { --passwords : Allow syncing passwords (can rewrite remotely and locally) }
25
                            { --users=    : Sync the specific users }
26
                            { --no-modes  : Omit all modes }';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Synchronize remote difference to apply latest changes locally';
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return void
39
     */
40
    public function handle()
41
    {
42
        if(!$key = $this->option('key')) {
43
            throw new \InvalidArgumentException('Encryption key must be passed.');
44
        }
45
46
        $noModes = $this->option('no-modes');
47
48
        $this->flushListeners();
49
50
        $syncer = new Syncer(null, $noModes ? [] : $this->modes());
51
        $syncer->setOutputCallback(function(string $message) {
52
            $this->info('[Syncer] ' . $message);
53
        });
54
55
56
        $this->info('Importing the dump...');
57
58
        $syncer->import($this->argument('filename'), $key, $noModes ? false : !$this->hasModes());
0 ignored issues
show
Bug introduced by
It seems like $this->argument('filename') can also be of type null and string[]; however, parameter $path of Slides\Connector\Auth\Sync\Syncer::import() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

58
        $syncer->import(/** @scrutinizer ignore-type */ $this->argument('filename'), $key, $noModes ? false : !$this->hasModes());
Loading history...
59
60
        $this->displayModes($syncer->getModes());
61
62
        $changes = $syncer->getForeignersCount();
63
64
        if(!$this->confirm('Apply ' . $changes . ' changes?', true)) {
65
            return;
66
        }
67
68
        $duration = $this->measure(function() use ($syncer, $changes) {
69
            $this->info("Applying {$changes} changes...");
70
71
            $syncer->apply();
72
        });
73
74
        $this->info("Finished in {$duration}s.");
75
    }
76
77
    /**
78
     * Measure an execution time of the callback.
79
     *
80
     * @param \Closure $callback
81
     *
82
     * @return float
83
     */
84
    public function measure(\Closure $callback)
85
    {
86
        $start = microtime(true);
87
88
        $callback();
89
90
        $end = microtime(true);
91
92
        return round($end - $start, 2);
93
    }
94
95
    /**
96
     * Flush models event listeners to speed-up the process.
97
     *
98
     * @return void
99
     */
100
    protected function flushListeners()
101
    {
102
        if(class_exists('\App\Http\Models\User')) {
103
            \App\Http\Models\User::flushEventListeners();
0 ignored issues
show
Bug introduced by
The type App\Http\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
104
        }
105
106
        if(class_exists('App\Http\Models\CustomerProfile')) {
107
            \App\Http\Models\CustomerProfile::flushEventListeners();
0 ignored issues
show
Bug introduced by
The type App\Http\Models\CustomerProfile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
108
        }
109
110
        if(class_exists('App\Modules\Billing\Models\Account::class')) {
111
            \App\Modules\Billing\Models\Account::flushEventListeners();
0 ignored issues
show
Bug introduced by
The type App\Modules\Billing\Models\Account was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
112
        }
113
    }
114
}