Completed
Push — master ( a2561f...b35847 )
by Elf
01:21
created

OptimusGenerateCommand::getTimes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid\Console;
4
5
use Illuminate\Console\Command;
6
use Jenssegers\Optimus\Energon;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class OptimusGenerateCommand extends Command
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'hashid:optimus';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Generate Optimus numbers';
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @return mixed
29
     */
30 1
    public function handle()
31
    {
32 1
        $this->table(
33 1
            ['prime', 'inverse', 'random'],
34 1
            $this->generateOptimusNumbers(
35 1
                $this->getTimes(),
36 1
                $this->option('prime')
37 1
            )
38 1
        );
39 1
    }
40
41
    /**
42
     * Get "times" option value.
43
     *
44
     * @return int
45
     */
46 1
    protected function getTimes()
47
    {
48 1
        $times = (int) $this->option('times');
49
50 1
        return max(1, min($times, 100));
51
    }
52
53
    /**
54
     * Generate Optimus numbers.
55
     *
56
     * @param  int  $times
57
     * @param  mixed  $prime
58
     * @return array
59
     */
60 1
    protected function generateOptimusNumbers($times = 1, $prime = null)
61
    {
62 1
        if ($prime = (int) $prime) {
63
            $times = 1;
64
        } else {
65 1
            $prime = null;
66
        }
67
68 1
        $result = [];
69
70 1
        for ($i = 0; $i < $times; $i++) {
71 1
            $result[] = Energon::generate($prime);
72 1
        }
73
74 1
        return $result;
75
    }
76
77
    /**
78
     * Get the console command options.
79
     *
80
     * @return array
81
     */
82 2
    protected function getOptions()
83
    {
84
        return [
85 2
            ['prime', 'p', InputOption::VALUE_OPTIONAL, 'Generate with the given prime'],
86 2
            ['times', 't', InputOption::VALUE_OPTIONAL, 'Times to generate', 1],
87 2
        ];
88
    }
89
}
90