Completed
Push — master ( 09c698...648dbf )
by Sebastian
04:13 queued 11s
created

Ldapdump::useAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
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 array
81
     */
82
    private $attrs;
83
84
    /**
85
     * Path to dump file
86
     *
87
     * @var string
88
     */
89
    private $dumpPathname;
90
91
    /**
92
     * Compression command to pipe output to
93
     *
94
     * @var \phpbu\App\Backup\Target\Compression
95
     */
96
    private $compression;
97
98
    /**
99
     * Constructor.
100
     *
101
     * @param string $path
102
     */
103
    public function __construct(string $path = '')
104
    {
105
        $this->setup('ldapsearch', $path);
106
    }
107
108
    /**
109
     * Set the ldap credentials
110
     *
111
     * @param  string $bindDn
112
     * @param  string $password
113
     * @return \phpbu\App\Cli\Executable\Ldapdump
114
     */
115
    public function credentials(string $bindDn = '', string $password = '') : Ldapdump
116
    {
117
        $this->bindDn   = $bindDn;
118
        $this->password = $password;
119
        return $this;
120
    }
121
122
    /**
123
     * Set the ldapdb hostname.
124
     *
125
     * @param  string $host
126
     * @return \phpbu\App\Cli\Executable\Ldapdump
127
     */
128
    public function useHost(string $host) : Ldapdump
129
    {
130
        $this->host = $host;
131
        return $this;
132
    }
133
134
    /**
135
     * Set the ldap port
136
     *
137
     * @param  int $port
138
     * @return \phpbu\App\Cli\Executable\Ldapdump
139
     */
140
    public function usePort(int $port) : Ldapdump
141
    {
142
        $this->port = $port;
143
        return $this;
144
    }
145
146
    /**
147
     * Set the ldap searchBase
148
     *
149
     * @param  string $searchBase
150
     * @return \phpbu\App\Cli\Executable\Ldapdump
151
     */
152
    public function useSearchBase(string $searchBase) : Ldapdump
153
    {
154
        $this->searchBase = $searchBase;
155
        return $this;
156
    }
157
158
    /**
159
     * Set the ldap filter
160
     *
161
     * @param  string $filter
162
     * @return \phpbu\App\Cli\Executable\Ldapdump
163
     */
164
    public function useFilter(string $filter) : Ldapdump
165
    {
166
        $this->filter = $filter;
167
        return $this;
168
    }
169
170
    /**
171
     * Set the ldap attrs
172
     *
173
     * @param  array $attrs
174
     * @return \phpbu\App\Cli\Executable\Ldapdump
175
     */
176
    public function useAttributes(array $attrs) : Ldapdump
177
    {
178
        $this->attrs = $attrs;
179
        return $this;
180
    }
181
182
    /**
183
     * Pipe compressor
184
     *
185
     * @param  \phpbu\App\Backup\Target\Compression $compression
186
     * @return \phpbu\App\Cli\Executable\Ldapdump
187
     */
188
    public function compressOutput(Compression $compression) : Ldapdump
189
    {
190
        $this->compression = $compression;
191
        return $this;
192
    }
193
194
    /**
195
     * Set the dump target path
196
     *
197
     * @param  string $path
198
     * @return \phpbu\App\Cli\Executable\Ldapdump
199
     */
200
    public function dumpTo(string $path) : Ldapdump
201
    {
202
        $this->dumpPathname = $path;
203
        return $this;
204
    }
205
206
    /**
207
     * Ldapd CommandLine generator.
208
     *
209
     * @return \SebastianFeldmann\Cli\CommandLine
210
     * @throws \phpbu\App\Exception
211
     */
212
    protected function createCommandLine() : CommandLine
213
    {
214
        $process = new CommandLine();
215
        $cmd     = new Cmd($this->binary);
216
        $process->addCommand($cmd);
217
218
        $cmd->addOptionIfNotEmpty('-b', $this->searchBase, true, ' ');
219
        $cmd->addOption('-x');
220
        $cmd->addOptionIfNotEmpty('-h', $this->host, true, ' ');
221
        $cmd->addOptionIfNotEmpty('-p', $this->port, true, ' ');
222
        $cmd->addOptionIfNotEmpty('-D', $this->bindDn, true, ' ');
223
        $cmd->addOptionIfNotEmpty('-w', $this->password, true, ' ');
224
225
        $this->configureFilter($cmd);
226
        $this->configureAttrs($cmd);
227
        $this->configureCompression($process);
228
        $this->configureOutput($process);
229
230
        return $process;
231
    }
232
233
    /**
234
     * Configure Filter
235
     *
236
     * param \SebastianFeldmann\Cli\Command\Executable $cmd
237
     */
238
    private function configureFilter(Cmd $cmd)
239
    {
240
        $cmd->addOption("'{$this->filter}'");
241
    }
242
243
    /**
244
     * Configure Attributes
245
     *
246
     * param \SebastianFeldmann\Cli\Command\Executable $cmd
247
     */
248
    private function configureAttrs(Cmd $cmd)
249
    {
250
        if ($this->attrs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->attrs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
251
            foreach ($this->attrs as $attr) {
252
                $cmd->addOption("'$attr'");
253
            }
254
        }
255
    }
256
257
    /**
258
     * Add compressor pipe if set
259
     *
260
     * @param \SebastianFeldmann\Cli\CommandLine $process
261
     */
262
    private function configureCompression(CommandLine $process)
263
    {
264
        // if file per table isn't active and a compressor is set
265
        if (!empty($this->compression)) {
266
            $binary = Cli::detectCmdLocation($this->compression->getCommand(), $this->compression->getPath());
267
            $cmd    = new Cmd($binary);
268
            $process->pipeOutputTo($cmd);
269
        }
270
    }
271
272
    /**
273
     * Configure output redirect
274
     *
275
     * @param \SebastianFeldmann\Cli\CommandLine $process
276
     */
277
    private function configureOutput(CommandLine $process)
278
    {
279
        $process->redirectOutputTo(
280
            $this->dumpPathname . (!empty($this->compression) ? '.' . $this->compression->getSuffix() : '')
281
        );
282
    }
283
}
284