Completed
Push — master ( efa9b4...b05f68 )
by wen
05:55
created

Password::notRequiredWithUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
class Password extends Text
6
{
7
    protected $type = 'password';
8
9
    protected $notRequired = false;
10
11
    public function save()
12
    {
13
        $value = $this->getValueFromRequest();
14
15
        // empty value don't save
16
        if (empty($value)) {
17
            return;
18
        }
19
20
        $this->setModelAttribute($value);
21
    }
22
23
    public function getValidationRules()
24
    {
25
        $rules = parent::getValidationRules();
26
27
        if ($this->isNotRequiredWithUpdate() && $this->getModel()->exists) {
28
            if (($key = array_search('required', $rules[$this->getName()])) !== false) {
29
                unset($rules[$this->getName()][$key]);
30
            }
31
        }
32
33
        return $rules;
34
    }
35
36
    public function getValue()
37
    {
38
        return '';
39
    }
40
41
    protected function hashAsBcrypt()
42
    {
43
        $this->setMutator(function ($value) {
44
            return bcrypt($value);
45
        });
46
47
        return $this;
48
    }
49
50
    protected function hashAsMD5()
51
    {
52
        $this->setMutator(function ($value) {
53
            return md5($value);
54
        });
55
56
        return $this;
57
    }
58
59
    protected function isNotRequiredWithUpdate()
60
    {
61
        return $this->notRequired;
62
    }
63
64
    public function notRequiredWithUpdate()
65
    {
66
        $this->notRequired = true;
67
68
        return $this;
69
    }
70
}
71