Completed
Push — master ( b462b5...dd132d )
by Sebastian
02:54
created

OptionMasker::getPropertiesToMask()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
namespace phpbu\App\Cli\Executable;
3
4
use phpbu\App\Cli\Process;
5
use phpbu\App\Util\Cli;
6
7
/**
8
 * Binary using credentials not safe for printing.
9
 *
10
 * @package    phpbu
11
 * @subpackage Backup
12
 * @author     Sebastian Feldmann <[email protected]>
13
 * @copyright  Sebastian Feldmann <[email protected]>
14
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
15
 * @link       http://phpbu.de/
16
 * @since      Class available since Release 3.0.5
17
 */
18
trait OptionMasker
19
{
20
    /**
21
     * List of properties to mask for print safe output
22
     *
23
     * @var array
24
     */
25
    protected $maskCandidates = [];
26
27
    /**
28
     * Set potentially insecure properties.
29
     *
30
     * @param array $candidates
31
     */
32
    protected function setMaskCandidates(array $candidates)
33
    {
34
        $this->maskCandidates = $candidates;
35
    }
36
37
    /**
38
     * Return the command with masked passwords or keys.
39
     *
40
     * @return string
41
     */
42
    public function getCommandLinePrintable()
43
    {
44
        $propertiesToMask = $this->getPropertiesToMask();
45
        // no candidates need masking
46
        if (0 === count($propertiesToMask)) {
47
            return $this->getCommandLine();
0 ignored issues
show
Bug introduced by
The method getCommandLine() does not exist on phpbu\App\Cli\Executable\OptionMasker. Did you maybe mean getCommandLinePrintable()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
48
        }
49
50
        $masked = $this->mask($propertiesToMask);
51
        $cmd    = $this->getCommandLine();
0 ignored issues
show
Bug introduced by
The method getCommandLine() does not exist on phpbu\App\Cli\Executable\OptionMasker. Did you maybe mean getCommandLinePrintable()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
52
        $this->restore($masked);
53
54
        return $cmd;
55
    }
56
57
    /**
58
     * Mask given properties and return map with original values.
59
     *
60
     * @param  array $properties
61
     * @return array
62
     */
63
    private function mask(array $properties)
64
    {
65
        $masked = [];
66
        foreach ($properties as $p) {
67
            $masked[$p] = $this->$p;
68
            $this->$p   = '******';
69
        }
70
        return $masked;
71
    }
72
73
    /**
74
     * Restore masked properties.
75
     *
76
     * @param array $masked
77
     */
78
    private function restore(array $masked)
79
    {
80
        foreach ($masked as $p => $value) {
81
            $this->$p = $value;
82
        }
83
    }
84
85
    /**
86
     * Return list of properties that actually needs masking.
87
     *
88
     * @return array
89
     */
90
    private function getPropertiesToMask()
91
    {
92
        $properties = [];
93
        foreach ($this->maskCandidates as $p) {
94
            if ($this->$p !== null) {
95
                $properties[] = $p;
96
            }
97
        }
98
        return $properties;
99
    }
100
}
101