Completed
Push — master ( dde37b...9454b2 )
by Saurabh
01:54
created

RenewCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 9
cts 12
cp 0.75
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B handle() 0 32 6
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 3
     * @var ApiKey
31
     */
32 3
    protected $apiKey;
33 3
34
    /**
35
     * Create a new command instance.
36 3
     *
37
     * @param ApiKey $apiKey
38 3
     */
39 1
    public function __construct(ApiKey $apiKey)
40
    {
41 2
        parent::__construct();
42
43
        $this->apiKey = $apiKey;
44 3
    }
45
46
    /**
47
     * Execute the console command.
48
     *
49
     * @return void
50 3
     */
51
    public function handle()
52
    {
53
        $all = $this->option('all');
54
55
        if ($all) {
56
            $this->info("Trying to renew both your keys...");
57
        } else {
58
            $key = $this->option('key') ?: 'primary';
59
            $this->info("Trying to renew your {$key} key...");
60
        }
61
62
        try {
63
            if ($all) {
64
                $this->apiKey->renewPrimary(config('signere.primary_key'));
65
                $this->apiKey->renewSecondary(config('signere.secondary_key'));
66
67
                $this->info("Both your keys were renewed!");
68
            } else {
69
                if ($this->option('key') === 'secondary') {
70
                    $this->apiKey->renewSecondary(config('signere.secondary_key'));
71
                } else {
72
                    $this->apiKey->renewPrimary(config('signere.primary_key'));
73
                }
74
                
75
                $this->info("Your {$key} key was renewed!");
0 ignored issues
show
Bug introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
76
            }
77
        } catch (Exception $e) {
78
            $this->error("Renewing failed because: {$e->getMessage()}.");
79
80
            return -1;
81
        }
82
    }
83
}
84