Completed
Push — master ( b1844b...03ac79 )
by Elf
01:34
created

AlphabetGenerateCommand::getTimes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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