Completed
Push — master ( 9454b2...915fb7 )
by Saurabh
04:36 queued 02:43
created

RenewCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 22
cts 22
cp 1
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
     * @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 5
        if ($all) {
56 2
            $this->info('Trying to renew both your keys...');
57
        } else {
58 3
            $key = $this->option('key') ?: 'primary';
59 3
            $this->info("Trying to renew your {$key} key...");
60
        }
61
62
        try {
63 5
            if ($all) {
64 2
                $this->apiKey->renewPrimary(config('signere.primary_key'));
65 1
                $this->apiKey->renewSecondary(config('signere.secondary_key'));
66
67 1
                $this->info('Both your keys were renewed!');
68
            } else {
69 3
                if ($this->option('key') === 'secondary') {
70 1
                    $this->apiKey->renewSecondary(config('signere.secondary_key'));
71
                } else {
72 2
                    $this->apiKey->renewPrimary(config('signere.primary_key'));
73
                }
74
75 4
                $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 1
        } catch (Exception $e) {
78 1
            $this->error("Renewing failed because: {$e->getMessage()}.");
79
80 1
            return -1;
81
        }
82 4
    }
83
}
84