Issues (83)

src/Cli/Executable/Ldapdump.php (1 issue)

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\Util\Cli;
8
use SebastianFeldmann\Cli\CommandLine;
9
use SebastianFeldmann\Cli\Command\Executable as Cmd;
10
11
/**
12
 * Ldapdump executable class.
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @author     Julian Marié <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       http://phpbu.de/
21
 * @since      Class available since Release 2.1.12
22
 */
23
class Ldapdump extends Abstraction implements Executable
24
{
25
    use OptionMasker;
26
27
    /**
28
     * Host to connect to
29
     * -h <hostname>
30
     *
31
     * @var string
32
     */
33
    private $host;
34
35
    /**
36
     * Port to connect to
37
     * -p <number>
38
     *
39
     * @var int
40
     */
41
    private $port;
42
43
    /**
44
     * Basename
45
     * -b <basename>
46
     *
47
     * @var string
48
     */
49
    private $searchBase;
50
51
    /**
52
     * BindDn to connect with
53
     * -D <DN>
54
     *
55
     * @var string
56
     */
57
    private $bindDn;
58
59
    /**
60
     * Password to authenticate with
61
     * -w <password>
62
     *
63
     * @var string
64
     */
65
    private $password;
66
67
    /**
68
     * Filter
69
     * <filter>
70
     *
71
     * @var string
72
     */
73
    private $filter;
74
75
    /**
76
     * Attributes
77
     * <attrs>
78
     *
79
     * @var array
80
     */
81
    private $attrs;
82
83
    /**
84
     * Path to dump file
85
     *
86
     * @var string
87
     */
88
    private $dumpPathname;
89
90
    /**
91
     * Compression command to pipe output to
92
     *
93
     * @var \phpbu\App\Backup\Target\Compression
94
     */
95
    private $compression;
96
97
    /**
98
     * Constructor.
99
     *
100
     * @param string $path
101
     */
102
    public function __construct(string $path = '')
103
    {
104
        $this->setup('ldapsearch', $path);
105
    }
106
107
    /**
108
     * Set the ldap credentials
109
     *
110
     * @param  string $bindDn
111
     * @param  string $password
112
     * @return \phpbu\App\Cli\Executable\Ldapdump
113
     */
114
    public function credentials(string $bindDn = '', string $password = '') : Ldapdump
115
    {
116
        $this->bindDn   = $bindDn;
117
        $this->password = $password;
118
        return $this;
119
    }
120
121
    /**
122
     * Set the ldapdb hostname.
123
     *
124
     * @param  string $host
125
     * @return \phpbu\App\Cli\Executable\Ldapdump
126
     */
127
    public function useHost(string $host) : Ldapdump
128
    {
129
        $this->host = $host;
130
        return $this;
131
    }
132
133
    /**
134
     * Set the ldap port
135
     *
136
     * @param  int $port
137
     * @return \phpbu\App\Cli\Executable\Ldapdump
138
     */
139
    public function usePort(int $port) : Ldapdump
140
    {
141
        $this->port = $port;
142
        return $this;
143
    }
144
145
    /**
146
     * Set the ldap searchBase
147
     *
148
     * @param  string $searchBase
149
     * @return \phpbu\App\Cli\Executable\Ldapdump
150
     */
151
    public function useSearchBase(string $searchBase) : Ldapdump
152
    {
153
        $this->searchBase = $searchBase;
154
        return $this;
155
    }
156
157
    /**
158
     * Set the ldap filter
159
     *
160
     * @param  string $filter
161
     * @return \phpbu\App\Cli\Executable\Ldapdump
162
     */
163
    public function useFilter(string $filter) : Ldapdump
164
    {
165
        $this->filter = $filter;
166
        return $this;
167
    }
168
169
    /**
170
     * Set the ldap attrs
171
     *
172
     * @param  array $attrs
173
     * @return \phpbu\App\Cli\Executable\Ldapdump
174
     */
175
    public function useAttributes(array $attrs) : Ldapdump
176
    {
177
        $this->attrs = $attrs;
178
        return $this;
179
    }
180
181
    /**
182
     * Pipe compressor
183
     *
184
     * @param  \phpbu\App\Backup\Target\Compression $compression
185
     * @return \phpbu\App\Cli\Executable\Ldapdump
186
     */
187
    public function compressOutput(Compression $compression) : Ldapdump
188
    {
189
        $this->compression = $compression;
190
        return $this;
191
    }
192
193
    /**
194
     * Set the dump target path
195
     *
196
     * @param  string $path
197
     * @return \phpbu\App\Cli\Executable\Ldapdump
198
     */
199
    public function dumpTo(string $path) : Ldapdump
200
    {
201
        $this->dumpPathname = $path;
202
        return $this;
203
    }
204
205
    /**
206
     * Ldapd CommandLine generator.
207
     *
208
     * @return \SebastianFeldmann\Cli\CommandLine
209
     * @throws \phpbu\App\Exception
210
     */
211
    protected function createCommandLine() : CommandLine
212
    {
213
        $process = new CommandLine();
214
        $cmd     = new Cmd($this->binary);
215
        $process->addCommand($cmd);
216
217
        $cmd->addOptionIfNotEmpty('-b', $this->searchBase, true, ' ');
218
        $cmd->addOption('-x');
219
        $cmd->addOptionIfNotEmpty('-h', $this->host, true, ' ');
220
        $cmd->addOptionIfNotEmpty('-p', $this->port, true, ' ');
221
        $cmd->addOptionIfNotEmpty('-D', $this->bindDn, true, ' ');
222
        $cmd->addOptionIfNotEmpty('-w', $this->password, true, ' ');
223
224
        $this->configureFilter($cmd);
225
        $this->configureAttrs($cmd);
226
        $this->configureCompression($process);
227
        $this->configureOutput($process);
228
229
        return $process;
230
    }
231
232
    /**
233
     * Configure Filter
234
     *
235
     * param \SebastianFeldmann\Cli\Command\Executable $cmd
236
     */
237
    private function configureFilter(Cmd $cmd)
238
    {
239
        $cmd->addOption("'{$this->filter}'");
240
    }
241
242
    /**
243
     * Configure Attributes
244
     *
245
     * param \SebastianFeldmann\Cli\Command\Executable $cmd
246
     */
247
    private function configureAttrs(Cmd $cmd)
248
    {
249
        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...
250
            foreach ($this->attrs as $attr) {
251
                $cmd->addOption("'$attr'");
252
            }
253
        }
254
    }
255
256
    /**
257
     * Add compressor pipe if set
258
     *
259
     * @param \SebastianFeldmann\Cli\CommandLine $process
260
     */
261
    private function configureCompression(CommandLine $process)
262
    {
263
        // if file per table isn't active and a compressor is set
264
        if (!empty($this->compression)) {
265
            $binary = Cli::detectCmdLocation($this->compression->getCommand(), $this->compression->getPath());
266
            $cmd    = new Cmd($binary);
267
            $process->pipeOutputTo($cmd);
268
        }
269
    }
270
271
    /**
272
     * Configure output redirect
273
     *
274
     * @param \SebastianFeldmann\Cli\CommandLine $process
275
     */
276
    private function configureOutput(CommandLine $process)
277
    {
278
        $process->redirectOutputTo(
279
            $this->dumpPathname . (!empty($this->compression) ? '.' . $this->compression->getSuffix() : '')
280
        );
281
    }
282
}
283