Completed
Push — master ( 0d6573...bd21d3 )
by Fumio
07:02
created

HashCheckCommand::handle()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 3
nop 0
dl 0
loc 23
ccs 15
cts 15
cp 1
crap 4
rs 8.7972
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelPlus\Extension\Commands;
4
5
use Illuminate\Console\Command;
6
7
/**
8
 * @author Fumio Furukawa <[email protected]>
9
 */
10
class HashCheckCommand extends Command
11
{
12
    /**
13
     * The console command signature.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'hash:check
18
        {string1 : Plain or Hashed string one.}
19
        {string2 : Plain or Hashed string two.}
20
        {--cost=10 : Cost of generate.}
21
    ';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = '[+] Check hashed value';
29
30
    /**
31
     * Execute the console command.
32
     *
33
     * @return mixed
34
     */
35 4
    public function handle()
36
    {
37 4
        $cost = $this->option('cost');
38
39 4
        $string1 = $this->argument('string1');
40 4
        $string2 = $this->argument('string2');
41
42 4
        if ($this->isHashed($string1)) {
43 2
            $rawString = $string2;
44 2
            $hashedString = $string1;
45 2
        } elseif ($this->isHashed($string2)) {
46 1
            $rawString = $string1;
47 1
            $hashedString = $string2;
48
        } else {
49 1
            $this->error(sprintf('Error: both "%s" and "%s" are not hashed string.', $string1, $string2));
50
51 1
            return;
52
        }
53
54 3
        $result = app('hash')->check($string1, $string2);
55
56 3
        $this->info($result ? 'Check OK!' : 'Check NG.');
57 3
    }
58
59 4
    protected function isHashed($string)
60
    {
61 4
        return strlen($string) == 60 && starts_with($string, '$2y$');
62
    }
63
}
64