Completed
Push — feature/custom-import-headers ( 1958d7 )
by
unknown
04:10
created

ImportCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 39
cts 39
cp 1
rs 9.125
c 0
b 0
f 0
cc 1
eloc 39
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * import json data into graviton
4
 *
5
 * Supports importing json data from either a single file or a complete folder of files.
6
 *
7
 * The data needs to contain frontmatter to hint where the bits and pieces should go.
8
 */
9
10
namespace Graviton\ImportExport\Command;
11
12
use Graviton\ImportExport\Exception\MissingTargetException;
13
use Graviton\ImportExport\Exception\JsonParseException;
14
use Graviton\ImportExport\Exception\UnknownFileTypeException;
15
use Graviton\ImportExport\Service\HttpClient;
16
use GuzzleHttp\Exception\ClientException;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Finder\Finder;
22
use Symfony\Component\Yaml\Parser;
23
use Symfony\Component\VarDumper\Cloner\VarCloner;
24
use Symfony\Component\VarDumper\Dumper\CliDumper as Dumper;
25
use GuzzleHttp\Promise;
26
use GuzzleHttp\Exception\RequestException;
27
use Symfony\Component\Finder\SplFileInfo;
28
use Webuni\FrontMatter\FrontMatter;
29
use Webuni\FrontMatter\Document;
30
use Psr\Http\Message\ResponseInterface;
31
32
/**
33
 * @author   List of contributors <https://github.com/libgraviton/import-export/graphs/contributors>
34
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
35
 * @link     http://swisscom.ch
36
 */
37
class ImportCommand extends ImportCommandAbstract
38
{
39
    /**
40
     * @var HttpClient
41
     */
42
    private $client;
43
44
    /**
45
     * @var FrontMatter
46
     */
47
    private $frontMatter;
48
49
    /**
50
     * @var Parser
51
     */
52
    private $parser;
53
54
    /**
55
     * @var VarCloner
56
     */
57
    private $cloner;
58
59
    /**
60
     * @var Dumper
61
     */
62
    private $dumper;
63
64
    /**
65
     * Count of errors
66
     * @var array
67
     */
68
    private $errors = [];
69
70
    /**
71
     * Header basic auth
72
     * @var string
73
     */
74
    private $headerBasicAuth;
75
76
    /**
77
     * Header for custom variables
78
     * @var string
79
     */
80
    private $customHeaders;
81
82
    /**
83
     * @param HttpClient  $client      Grv HttpClient guzzle http client
84
     * @param Finder      $finder      symfony/finder instance
85
     * @param FrontMatter $frontMatter frontmatter parser
86
     * @param Parser      $parser      yaml/json parser
87
     * @param VarCloner   $cloner      var cloner for dumping reponses
88
     * @param Dumper      $dumper      dumper for outputing responses
89
     */
90 5
    public function __construct(
91
        HttpClient $client,
92
        Finder $finder,
93
        FrontMatter $frontMatter,
94
        Parser $parser,
95
        VarCloner $cloner,
96
        Dumper $dumper
97
    ) {
98 5
        parent::__construct(
99 5
            $finder
100
        );
101 5
        $this->client = $client;
102 5
        $this->frontMatter = $frontMatter;
103 5
        $this->parser = $parser;
104 5
        $this->cloner = $cloner;
105 5
        $this->dumper = $dumper;
106 5
    }
107
108
    /**
109
     * Configures the current command.
110
     *
111
     * @return void
112
     */
113 5
    protected function configure()
114
    {
115
        $this
116 5
            ->setName('graviton:import')
117 5
            ->setDescription('Import files from a folder or file.')
118 5
            ->addOption(
119 5
                'rewrite-host',
120 5
                'r',
121 5
                InputOption::VALUE_OPTIONAL,
122 5
                'Replace the value of this option with the <host> value before importing.',
123 5
                'http://localhost'
124
            )
125 5
            ->addOption(
126 5
                'rewrite-to',
127 5
                't',
128 5
                InputOption::VALUE_OPTIONAL,
129 5
                'String to use as the replacement value for the [REWRITE-HOST] string.',
130 5
                '<host>'
131
            )
132 5
            ->addOption(
133 5
                'sync-requests',
134 5
                's',
135 5
                InputOption::VALUE_NONE,
136 5
                'Send requests synchronously'
137
            )
138 5
            ->addOption(
139 5
                'headers-basic-auth',
140 5
                'a',
141 5
                InputOption::VALUE_OPTIONAL,
142 5
                'Header user:password for Basic auth'
143
            )
144 5
            ->addOption(
145 5
                'custom-headers',
146 5
                'c',
147 5
                InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
148 5
                'Custom Header variable(s), -c{key:value} and multiple is optional.'
149
            )
150 5
            ->addArgument(
151 5
                'host',
152 5
                InputArgument::REQUIRED,
153 5
                'Protocol and host to load data into (ie. https://graviton.nova.scapp.io)'
154
            )
155 5
            ->addArgument(
156 5
                'file',
157 5
                InputArgument::REQUIRED + InputArgument::IS_ARRAY,
158 5
                'Directories or files to load'
159
            );
160 5
    }
161
162
    /**
163
     * Executes the current command.
164
     *
165
     * @param Finder          $finder Finder
166
     * @param InputInterface  $input  User input on console
167
     * @param OutputInterface $output Output of the command
168
     *
169
     * @return void
170
     */
171 5
    protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output)
172
    {
173 5
        $exitCode = 0;
174 5
        $host = $input->getArgument('host');
175 5
        $rewriteHost = $input->getOption('rewrite-host');
176 5
        $rewriteTo = $input->getOption('rewrite-to');
177 5
        $this->headerBasicAuth = $input->getOption('headers-basic-auth');
178 5
        $this->customHeaders = $input->getOption('custom-headers');
179 5
        if ($rewriteTo === $this->getDefinition()->getOption('rewrite-to')->getDefault()) {
180 5
            $rewriteTo = $host;
181
        }
182 5
        $sync = $input->getOption('sync-requests');
183
184 5
        $this->importPaths($finder, $output, $host, $rewriteHost, $rewriteTo, $sync);
185
186
        // Error exit
187 3
        if (empty($this->errors)) {
188
            // No errors
189 2
            $output->writeln("\n".'<info>No errors</info>');
190
        } else {
191
            // Yes, there was errors
192 1
            $output->writeln("\n".'<info>There was errors: '.count($this->errors).'</info>');
193 1
            foreach ($this->errors as $file => $error) {
194 1
                $output->writeln("<error>{$file}: {$error}</error>");
195
            }
196 1
            $exitCode = 1;
197
        }
198 3
        return $exitCode;
199
    }
200
201
    /**
202
     * @param Finder          $finder      finder primmed with files to import
203
     * @param OutputInterface $output      output interfac
204
     * @param string          $host        host to import into
205
     * @param string          $rewriteHost string to replace with value from $rewriteTo during loading
206
     * @param string          $rewriteTo   string to replace value from $rewriteHost with during loading
207
     * @param boolean         $sync        send requests syncronously
208
     *
209
     * @return void
210
     *
211
     * @throws MissingTargetException
212
     */
213 5
    protected function importPaths(
214
        Finder $finder,
215
        OutputInterface $output,
216
        $host,
217
        $rewriteHost,
218
        $rewriteTo,
219
        $sync = false
220
    ) {
221 5
        $promises = [];
222
        /** @var SplFileInfo $file */
223 5
        foreach ($finder as $file) {
224 5
            $doc = $this->frontMatter->parse($file->getContents());
225
226 5
            $output->writeln("<info>Loading data from ${file}</info>");
227
228 5
            if (!array_key_exists('target', $doc->getData())) {
229 1
                throw new MissingTargetException('Missing target in \'' . $file . '\'');
230
            }
231
232 4
            $targetUrl = sprintf('%s%s', $host, $doc->getData()['target']);
233
234 4
            $promises[] = $this->importResource(
235 4
                $targetUrl,
236 4
                (string) $file,
237 4
                $output,
238 4
                $doc,
239 4
                $rewriteHost,
240 4
                $rewriteTo,
241 4
                $sync
242
            );
243
        }
244
245
        try {
246
            Promise\unwrap($promises);
247
        } catch (ClientException $e) {
248
            // silently ignored since we already output an error when the promise fails
249
        }
250
    }
251
252
    /**
253
     * @param string          $targetUrl   target url to import resource into
254
     * @param string          $file        path to file being loaded
255
     * @param OutputInterface $output      output of the command
256
     * @param Document        $doc         document to load
257
     * @param string          $rewriteHost string to replace with value from $host during loading
258
     * @param string          $rewriteTo   string to replace value from $rewriteHost with during loading
259
     * @param boolean         $sync        send requests syncronously
260
     *
261
     * @return Promise\Promise|null
262
     */
263
    protected function importResource(
264
        $targetUrl,
265
        $file,
266
        OutputInterface $output,
267
        Document $doc,
268
        $rewriteHost,
269
        $rewriteTo,
270
        $sync = false
271
    ) {
272 4
        $content = str_replace($rewriteHost, $rewriteTo, $doc->getContent());
273 4
        $uploadFile = $this->validateUploadFile($doc, $file);
274
275
        $successFunc = function (ResponseInterface $response) use ($output) {
276 2
            $output->writeln(
277 2
                '<comment>Wrote ' . $response->getHeader('Link')[0] . '</comment>'
278
            );
279 4
        };
280
281
        $errFunc = function (RequestException $e) use ($output, $file) {
282 1
            $this->errors[$file] = $e->getMessage();
283 1
            $output->writeln(
284 1
                '<error>' . str_pad(
285 1
                    sprintf(
286 1
                        'Failed to write <%s> from \'%s\' with message \'%s\'',
287 1
                        $e->getRequest()->getUri(),
288 1
                        $file,
289 1
                        $e->getMessage()
290
                    ),
291 1
                    140,
292 1
                    ' '
293 1
                ) . '</error>'
294
            );
295 1
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
296 1
                $this->dumper->dump(
297 1
                    $this->cloner->cloneVar(
298 1
                        $this->parser->parse($e->getResponse()->getBody(), false, false, true)
299
                    ),
300
                    function ($line, $depth) use ($output) {
301 1
                        if ($depth > 0) {
302 1
                            $output->writeln(
303 1
                                '<error>' . str_pad(str_repeat('  ', $depth) . $line, 140, ' ') . '</error>'
304
                            );
305
                        }
306 1
                    }
307
                );
308
            }
309 4
        };
310
311
        $data = [
312 4
            'json'   => $this->parseContent($content, $file),
313 4
            'upload' => $uploadFile,
314
            'headers'=> []
315
        ];
316 4
        if ($this->headerBasicAuth) {
317
            $data['headers']['Authorization'] = 'Basic '. base64_encode($this->headerBasicAuth);
318
        }
319 4
        if ($this->customHeaders) {
320
            foreach ($this->customHeaders as $headers) {
0 ignored issues
show
Bug introduced by
The expression $this->customHeaders of type string is not traversable.
Loading history...
321
                list($key, $value) = explode(':', $headers);
322
                $data['headers'][$key] = $value;
323
            }
324
        }
325
326 4
        $promise = $this->client->requestAsync(
327 4
            'PUT',
328 4
            $targetUrl,
329 4
            $data
330
        );
331
332
        // If there is a file to be uploaded, and it exists in remote, we delete it first.
333
        // TODO This part, $uploadFile, promise should be removed once Graviton/File service is resolved in new Story.
334 3
        $fileRepeatFunc = false;
335 3
        if ($uploadFile) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uploadFile of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
336
            $fileRepeatFunc = function () use ($targetUrl, $successFunc, $errFunc, $output, $file, $data) {
337
                unset($this->errors[$file]);
338
                $output->writeln('<info>File deleting: '.$targetUrl.'</info>');
339
                $deleteRequest = $this->client->requestAsync('DELETE', $targetUrl);
340
                $insert = function () use ($targetUrl, $successFunc, $errFunc, $output, $data) {
341
                    $output->writeln('<info>File inserting: '.$targetUrl.'</info>');
342
                    $promiseInsert = $this->client->requestAsync('PUT', $targetUrl, $data);
343
                    $promiseInsert->then($successFunc, $errFunc);
344
                };
345
                $deleteRequest
346
                    ->then($insert, $errFunc)->wait();
347 1
            };
348
        }
349
350 3
        $promiseError = $fileRepeatFunc ? $fileRepeatFunc : $errFunc;
351 3
        if ($sync) {
352
            $promise->then($successFunc, $promiseError)->wait();
353
        } else {
354 3
            $promise->then($successFunc, $promiseError);
355
        }
356
357
358
359 3
        return $promise;
360
    }
361
362
    /**
363
     * parse contents of a file depending on type
364
     *
365
     * @param string $content contents part of file
366
     * @param string $file    full path to file
367
     *
368
     * @return mixed
369
     * @throws UnknownFileTypeException
370
     * @throws JsonParseException
371
     */
372
    protected function parseContent($content, $file)
373
    {
374 4
        if (substr($file, -5) == '.json') {
375 3
            $data = json_decode($content);
376 3
            if (json_last_error() !== JSON_ERROR_NONE) {
377
                throw new JsonParseException(
378
                    sprintf(
379
                        '%s in %s',
380
                        json_last_error_msg(),
381 3
                        $file
382
                    )
383
                );
384
            }
385 1
        } elseif (substr($file, -4) == '.yml') {
386 1
            $data = $this->parser->parse($content);
387
        } else {
388
            throw new UnknownFileTypeException($file);
389
        }
390
391 4
        return $data;
392
    }
393
394
    /**
395
     * Checks if file exists and return qualified fileName location
396
     *
397
     * @param Document $doc        Data source for import data
398
     * @param string   $originFile Original full filename used toimport
399
     * @return bool|mixed
400
     */
401
    private function validateUploadFile(Document $doc, $originFile)
402
    {
403 4
        $documentData = $doc->getData();
404
405 4
        if (!array_key_exists('file', $documentData)) {
406 3
            return false;
407
        }
408
409
        // Find file
410 1
        $fileName = dirname($originFile) . DIRECTORY_SEPARATOR . $documentData['file'];
411 1
        $fileName = str_replace('//', '/', $fileName);
412 1
        if (!file_exists($fileName)) {
413
            return false;
414
        }
415
416 1
        return $fileName;
417
    }
418
}
419