Completed
Pull Request — master (#229)
by
unknown
05:02
created

Ldapdump::configureAttrs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 3
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace phpbu\App\Cli\Executable;
4
5
use phpbu\App\Backup\Target\Compression;
6
use phpbu\App\Cli\Executable;
7
use phpbu\App\Exception;
8
use phpbu\App\Util\Cli;
9
use SebastianFeldmann\Cli\CommandLine;
10
use SebastianFeldmann\Cli\Command\Executable as Cmd;
11
12
/**
13
 * Ldapdump executable class.
14
 *
15
 * @package    phpbu
16
 * @subpackage Backup
17
 * @author     Sebastian Feldmann <[email protected]>
18
 * @author     Julian Marié <[email protected]>
19
 * @copyright  Sebastian Feldmann <[email protected]>
20
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
21
 * @link       http://phpbu.de/
22
 * @since      Class available since Release 2.1.12
23
 */
24
class Ldapdump extends Abstraction implements Executable
25
{
26
    use OptionMasker;
27
28
    /**
29
     * Host to connect to
30
     * -h <hostname>
31
     *
32
     * @var string
33
     */
34
    private $host;
35
36
    /**
37
     * Port to connect to
38
     * -p <number>
39
     *
40
     * @var int
41
     */
42
    private $port;
43
44
    /**
45
     * Basename
46
     * -b <basename>
47
     *
48
     * @var string
49
     */
50
    private $searchBase;
51
52
    /**
53
     * BindDn to connect with
54
     * -D <DN>
55
     *
56
     * @var string
57
     */
58
    private $bindDn;
59
60
    /**
61
     * Password to authenticate with
62
     * -w <password>
63
     *
64
     * @var string
65
     */
66
    private $password;
67
68
    /**
69
     * Filter 
70
     * <filter>
71
     *
72
     * @var string
73
     */
74
    private $filter;
75
76
    /**
77
     * Attributes
78
     * <attrs>
79
     *
80
     * @var string
81
     */
82
    private $attrs;
83
84
    /**
85
     * Path to dump file
86
     *
87
     * @var string
88
     */
89
    private $dumpPathname;
90
91
    /**
92
     * Constructor.
93
     *
94
     * @param string $path
95
     */
96
    public function __construct(string $path = '')
97
    {
98
        $this->setup('ldapsearch', $path);
99
    }
100
101
    /**
102
     * Set the ldap credentials
103
     *
104
     * @param  string $bindDn
105
     * @param  string $password
106
     * @return \phpbu\App\Cli\Executable\Ldapdump
107
     */
108
    public function credentials(string $bindDn = '', string $password = '') : Ldapdump
109
    {
110
        $this->bindDn   = $bindDn;
111
        $this->password = $password;
112
        return $this;
113
    }
114
115
    /**
116
     * Set the ldapdb hostname.
117
     *
118
     * @param  string $host
119
     * @return \phpbu\App\Cli\Executable\Ldapdump
120
     */
121
    public function useHost(string $host) : Ldapdump
122
    {
123
        $this->host = $host;
124
        return $this;
125
    }
126
127
    /**
128
     * Set the ldap port
129
     *
130
     * @param  int $port
131
     * @return \phpbu\App\Cli\Executable\Ldapdump
132
     */
133
    public function usePort(int $port) : Ldapdump
134
    {
135
        $this->port = $port;
136
        return $this;
137
    }
138
139
    /**
140
     * Set the ldap searchBase
141
     *
142
     * @param  string $searchBase
143
     * @return \phpbu\App\Cli\Executable\Ldapdump
144
     */
145
    public function useSearchBase(string $searchBase) : Ldapdump
146
    {
147
        $this->searchBase = $searchBase;
148
        return $this;
149
    }
150
151
    /**
152
     * Set the ldap filter
153
     *
154
     * @param  string $filter
155
     * @return \phpbu\App\Cli\Executable\Ldapdump
156
     */
157
    public function useFilter(string $filter) : Ldapdump
158
    {
159
        $this->filter = $filter;
160
        return $this;
161
    }
162
163
    /**
164
     * Set the ldap attrs
165
     *
166
     * @param  array $attrs
167
     * @return \phpbu\App\Cli\Executable\Ldapdump
168
     */
169
    public function useAttributes(array $attrs) : Ldapdump
170
    {
171
        $this->attrs = $attrs;
0 ignored issues
show
Documentation Bug introduced by
It seems like $attrs of type array is incompatible with the declared type string of property $attrs.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
172
        return $this;
173
    }
174
175
    /**
176
     * Pipe compressor
177
     *
178
     * @param  \phpbu\App\Backup\Target\Compression $compression
179
     * @return \phpbu\App\Cli\Executable\Ldapdump
180
     */
181
    public function compressOutput(Compression $compression) : Ldapdump
182
    {
183
        $this->compression = $compression;
0 ignored issues
show
Bug Best Practice introduced by
The property compression does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
184
        return $this;
185
    }
186
187
    /**
188
     * Set the dump target path
189
     *
190
     * @param  string $path
191
     * @return \phpbu\App\Cli\Executable\Ldapdump
192
     */
193
    public function dumpTo(string $path) : Ldapdump
194
    {
195
        $this->dumpPathname = $path;
196
        return $this;
197
    }
198
199
    /**
200
     * Ldapd CommandLine generator.
201
     *
202
     * @return \SebastianFeldmann\Cli\CommandLine
203
     * @throws \phpbu\App\Exception
204
     */
205
    protected function createCommandLine() : CommandLine
206
    {
207
        $process = new CommandLine();
208
        $cmd     = new Cmd($this->binary);
209
        $process->addCommand($cmd);
210
211
        $cmd->addOptionIfNotEmpty('-b', $this->searchBase, true, ' ');
212
        $cmd->addOption('-x');
213
        $cmd->addOptionIfNotEmpty('-h', $this->host, true, ' ');
214
        $cmd->addOptionIfNotEmpty('-p', $this->port, true, ' ');
215
        $cmd->addOptionIfNotEmpty('-D', $this->bindDn, true, ' ');
216
        $cmd->addOptionIfNotEmpty('-w', $this->password, true, ' ');
217
218
        $this->configureFilter($cmd);
219
        $this->configureAttrs($cmd);
220
        $this->configureCompression($process);
221
        $this->configureOutput($process);
222
223
        return $process;
224
    }
225
226
    /**
227
     * Configure Filter
228
     *
229
     * param \SebastianFeldmann\Cli\Command\Executable $cmd
230
     */
231
    private function configureFilter(Cmd $cmd)
232
    {
233
        $cmd->addOption("'{$this->filter}'");
234
    }
235
236
    /**
237
     * Configure Attributes
238
     *
239
     * param \SebastianFeldmann\Cli\Command\Executable $cmd
240
     */
241
    private function configureAttrs(Cmd $cmd)
242
    {
243
        if ($this->attrs) {
244
            foreach ($this->attrs as $attr) {
0 ignored issues
show
Bug introduced by
The expression $this->attrs of type string is not traversable.
Loading history...
245
                $cmd->addOption("'$attr'");
246
            }
247
        }
248
    }
249
250
    /**
251
     * Add compressor pipe if set
252
     *
253
     * @param \SebastianFeldmann\Cli\CommandLine $process
254
     */
255
    private function configureCompression(CommandLine $process)
256
    {
257
        // if file per table isn't active and a compressor is set
258
        if (!empty($this->compression)) {
259
            $binary = Cli::detectCmdLocation($this->compression->getCommand(), $this->compression->getPath());
260
            $cmd    = new Cmd($binary);
261
            $process->pipeOutputTo($cmd);
262
        }
263
    }
264
265
    /**
266
     * Configure output redirect
267
     *
268
     * @param \SebastianFeldmann\Cli\CommandLine $process
269
     */
270
    private function configureOutput(CommandLine $process)
271
    {
272
        $process->redirectOutputTo(
273
            $this->dumpPathname . (!empty($this->compression) ? '.' . $this->compression->getSuffix() : '')
274
        );
275
    }
276
}
277