Completed
Push — master ( ab5ce3...f55930 )
by Helmut
02:51
created

Password::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 0
crap 1
1
<?php 
2
3
namespace Helmut\Forms\Fields\Password;
4
5
use Helmut\Forms\Field;
6
use Helmut\Forms\Utility\Validate;
7
8
class Password extends Field {
9
10
    public $value = '';
11 3
12
    public function hash($value) 
13 3
    {
14 1
        if (strlen($value) === 0) {
15
            return '';
16
        }
17 3
18
        return password_hash($value, PASSWORD_BCRYPT, ['cost' => 10]);
19
    }
20
21
    public function needsRehash($hash = null)
22
    {
23
        if (is_null($hash)) $hash = $this->value;
24
25
        return password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 10]);
26
    }   
27 1
28
    public function matches($hash)
29 1
    {
30 1
        if (strlen($hash) === 0) {
31
            return false;
32
        }
33 1
34
        $check = $this->form->getRequest()->get($this->name);
35 1
36
        return password_verify($check, $hash);
37
    }
38 5
39
    public function getValue()
40 5
    {
41
        return $this->value;
42
    }
43 3
44
    public function getButtonName()
45 3
    {
46
        //
47
    }
48 10
49
    public function renderWith()
50 10
    {
51
        //
52
    }
53 10
54
    public function setValueFromDefault()
55
    {
56 10
        //
57
    }
58 1
59
    public function setValueFromModel($model)
60 1
    {
61 1
        if (property_exists($model, $this->name)) $this->value = $model->{$this->name};
62
    }
63 3
64
    public function setValueFromRequest($request)
65 3
    {
66 3
        $this->value = $this->hash($request->get($this->name));
67
    }
68 4
69
    public function fillModelWithValue($model)
70 4
    {
71 4
        if (property_exists($model, $this->name)) $model->{$this->name} = $this->value;
72
    }   
73 1
74
    public function validateRequired()
75 1
    {
76
        return Validate::required($this->value);
77
    }
78
79
}