Completed
Pull Request — master (#78)
by Aydin
02:26
created

Password   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 1
dl 0
loc 93
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setPromptText() 0 6 1
A getPromptText() 0 4 1
A setValidationFailedText() 0 6 1
A getValidationFailedText() 0 4 1
A setPlaceholderText() 0 6 1
A getPlaceholderText() 0 4 1
A setValidator() 0 4 1
A ask() 0 4 1
A validate() 0 9 2
A format() 0 4 1
1
<?php
2
3
namespace PhpSchool\CliMenu\Input;
4
5
/**
6
 * @author Aydin Hassan <[email protected]>
7
 */
8
class Password implements Input
9
{
10
    /**
11
     * @var InputIO
12
     */
13
    private $inputIO;
14
15
    /**
16
     * @var string
17
     */
18
    private $promptText = 'Enter password:';
19
20
    /**
21
     * @var string
22
     */
23
    private $validationFailedText = 'Invalid password, try again';
24
25
    /**
26
     * @var string
27
     */
28
    private $placeholderText = '';
29
30
    /**
31
     * @var null|callable
32
     */
33
    private $validator;
34
35
    public function __construct(InputIO $inputIO)
36
    {
37
        $this->inputIO = $inputIO;
38
    }
39
40
    public function setPromptText(string $promptText) : Input
41
    {
42
        $this->promptText = $promptText;
43
44
        return $this;
45
    }
46
47
    public function getPromptText() : string
48
    {
49
        return $this->promptText;
50
    }
51
52
    public function setValidationFailedText(string $validationFailedText) : Input
53
    {
54
        $this->validationFailedText = $validationFailedText;
55
56
        return $this;
57
    }
58
59
    public function getValidationFailedText() : string
60
    {
61
        return $this->validationFailedText;
62
    }
63
64
    public function setPlaceholderText(string $placeholderText) : Input
65
    {
66
        $this->placeholderText = $placeholderText;
67
68
        return $this;
69
    }
70
71
    public function getPlaceholderText() : string
72
    {
73
        return $this->placeholderText;
74
    }
75
76
    public function setValidator(callable $validator)
77
    {
78
        $this->validator = $validator;
79
    }
80
81
    public function ask() : InputResult
82
    {
83
        return $this->inputIO->collect($this);
84
    }
85
86
    public function validate(string $input) : bool
87
    {
88
        if ($this->validator) {
89
            $validator = $this->validator;
90
            return $validator($input);
91
        }
92
93
        return mb_strlen($input) > 16;
94
    }
95
96
    public function format(string $value) : string
97
    {
98
        return str_repeat('*', mb_strlen($value));
99
    }
100
}
101