RenewCommand::handle()   B
last analyzed

Complexity

Conditions 5
Paths 21

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 21
nop 0
dl 0
loc 34
ccs 18
cts 18
cp 1
crap 5
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Sausin\Signere\Console;
4
5
use Exception;
6
use Sausin\Signere\ApiKey;
7
use Illuminate\Console\Command;
8
9
class RenewCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'signere:renew 
17
                            {--key= : Specify the key to be renewed}
18
                            {--all}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Renews your primary or secondary signere key';
26
27
    /**
28
     * The ApiKey instance.
29
     *
30
     * @var ApiKey
31
     */
32
    protected $apiKey;
33
34
    /**
35
     * Create a new command instance.
36
     *
37
     * @param ApiKey $apiKey
38
     */
39 5
    public function __construct(ApiKey $apiKey)
40
    {
41 5
        parent::__construct();
42
43 5
        $this->apiKey = $apiKey;
44 5
    }
45
46
    /**
47
     * Execute the console command.
48
     *
49
     * @return void
50
     */
51 5
    public function handle()
52
    {
53 5
        $all = $this->option('all');
54
55
        try {
56 5
            if ($all) {
57 2
                $this->info('Trying to renew both your keys...');
58
59 2
                $this->apiKey->renewPrimary(config('signere.primary_key'));
60 1
                $this->apiKey->renewSecondary(config('signere.secondary_key'));
61
62 1
                $this->info('Both your keys were renewed!');
63
64 1
                return;
65
            }
66
67 3
            $key = $this->option('key') ?: 'primary';
68 3
            $this->info("Trying to renew your {$key} key...");
69
70 3
            if ($key === 'secondary') {
71 1
                $this->apiKey->renewSecondary(config('signere.secondary_key'));
72
            } else {
73 2
                $this->apiKey->renewPrimary(config('signere.primary_key'));
74
            }
75
76 3
            $this->info("Your {$key} key was renewed!");
77
78 3
            return;
79 1
        } catch (Exception $e) {
80 1
            $this->error("Renewing failed because: {$e->getMessage()}.");
81
82 1
            return -1;
83
        }
84
    }
85
}
86