EarlyAccess::activate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Neo\EarlyAccess\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\InteractsWithTime;
7
use Neo\EarlyAccess\Traits\InteractsWithEarlyAccess;
8
use Illuminate\Contracts\Filesystem\Filesystem as Storage;
9
10
class EarlyAccess extends Command
11
{
12
    use InteractsWithTime, InteractsWithEarlyAccess;
13
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'early-access
20
                            {--a|activate : Activate early access}
21
                            {--d|deactivate : Deactivate early access}
22
                            {--allow=* : IP or networks allowed to access the application while in early access mode}
23
                            {status? : Get the status}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Activate, deactivate, or check if early access mode is enabled.';
31
32
    /**
33
     * @var \Illuminate\Contracts\Filesystem\Filesystem
34
     */
35
    private $storage;
36
37
    /**
38
     * Create a new command instance.
39
     *
40
     * @param \Illuminate\Contracts\Filesystem\Filesystem $storage
41
     */
42
    public function __construct(Storage $storage)
43
    {
44
        parent::__construct();
45
46
        $this->storage = $storage;
47
    }
48
49
    /**
50
     * Execute the console command.
51
     *
52
     * @return mixed
53
     */
54
    public function handle()
55
    {
56
        if ($this->argument('status')) {
57
            return $this->getStatus();
58
        }
59
60
        $activate = $this->option('activate');
61
62
        $deactivate = $this->option('deactivate');
63
64
        if (! $activate && ! $deactivate) {
65
            $intent = $this->option('allow')
66
                ? 'activate'
67
                : $this->choice('What do you want to do', ['1' => 'activate', '2' => 'deactivate']);
68
69
            ${$intent} = true;
70
        }
71
72
        return $activate ? $this->activate() : $this->deactivate();
73
    }
74
75
    /**
76
     * Deactivate early access.
77
     */
78
    protected function activate()
79
    {
80
        $this->saveBeacon($this->option('allow') ?? [])
81
            ? $this->comment('Early access activated.')
82
            : $this->error('Unable to activate early access.');
83
    }
84
85
    /**
86
     * Activate early access.
87
     */
88
    protected function deactivate()
89
    {
90
        $this->deleteBeacon()
91
            ? $this->comment('Early access deactivated.')
92
            : $this->comment('Could not deactivate. Possibly not active.');
93
94
        if (config('early-access.enabled')) {
95
            $this->comment('Set EARLY_ACCESS_ENABLED to false in your .env to fully deactivate it.');
96
        }
97
    }
98
99
    /**
100
     * Gets the status of the early access mode.
101
     */
102
    protected function getStatus()
103
    {
104
        $allowedNetworks = with(($data = $this->getBeaconDetails()), function ($details) {
105
            return (isset($details['allowed']) && count($details['allowed']))
106
                ? implode(', ', $details['allowed'])
107
                : 'none';
108
        });
109
110
        $this->info($data ? "Active. Allowed networks: {$allowedNetworks}" : 'Not active');
111
    }
112
}
113