Completed
Push — develop ( 045e3f...43816c )
by Narcotic
15s
created

ImportCommand::parseContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 7
cts 14
cp 0.5
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 2
crap 6
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 array
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
            $finder
100 5
        );
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 5
        $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
                'http://localhost'
124 5
            )
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
                '<host>'
131 5
            )
132 5
            ->addOption(
133 5
                'sync-requests',
134 5
                's',
135 5
                InputOption::VALUE_NONE,
136
                'Send requests synchronously'
137 5
            )
138 5
            ->addOption(
139 5
                'headers-basic-auth',
140 5
                'a',
141 5
                InputOption::VALUE_OPTIONAL,
142
                'Header user:password for Basic auth'
143 5
            )
144 5
            ->addOption(
145 5
                'custom-headers',
146 5
                'c',
147 5
                InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
148
                'Custom Header variable(s), -c{key:value} and multiple is optional.'
149 5
            )
150 5
            ->addOption(
151 5
                'input-file',
152 5
                'i',
153 5
                InputOption::VALUE_REQUIRED,
154
                'If provided, the list of files to load will be loaded from this file, one file per line.'
155 5
            )
156 5
            ->addArgument(
157 5
                'host',
158 5
                InputArgument::REQUIRED,
159
                'Protocol and host to load data into (ie. https://graviton.nova.scapp.io)'
160 5
            )
161 5
            ->addArgument(
162 5
                'file',
163 5
                InputArgument::IS_ARRAY,
164
                'Directories or files to load'
165 5
            );
166 5
    }
167
168
    /**
169
     * Executes the current command.
170
     *
171
     * @param Finder          $finder Finder
172
     * @param InputInterface  $input  User input on console
173
     * @param OutputInterface $output Output of the command
174
     *
175
     * @return integer
176
     */
177 5
    protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output)
178
    {
179 5
        $exitCode = 0;
180 5
        $host = $input->getArgument('host');
181 5
        $rewriteHost = $input->getOption('rewrite-host');
182 5
        $rewriteTo = $input->getOption('rewrite-to');
183 5
        $this->headerBasicAuth = $input->getOption('headers-basic-auth');
184 5
        if ($rewriteTo === $this->getDefinition()->getOption('rewrite-to')->getDefault()) {
185 5
            $rewriteTo = $host;
186 5
        }
187 5
        $sync = $input->getOption('sync-requests');
188
189 5
        $this->importPaths($finder, $output, $host, $rewriteHost, $rewriteTo, $sync);
190
191
        // Error exit
192 4
        if (empty($this->errors)) {
193
            // No errors
194 3
            $output->writeln("\n".'<info>No errors</info>');
195 3
        } else {
196
            // Yes, there was errors
197 1
            $output->writeln("\n".'<info>There was errors: '.count($this->errors).'</info>');
198 1
            foreach ($this->errors as $file => $error) {
199 1
                $output->writeln("<error>{$file}: {$error}</error>");
200 1
            }
201 1
            $exitCode = 1;
202
        }
203 4
        return $exitCode;
204
    }
205
206
    /**
207
     * @param Finder          $finder      finder primmed with files to import
208
     * @param OutputInterface $output      output interfac
209
     * @param string          $host        host to import into
210
     * @param string          $rewriteHost string to replace with value from $rewriteTo during loading
211
     * @param string          $rewriteTo   string to replace value from $rewriteHost with during loading
212
     * @param boolean         $sync        send requests syncronously
213
     *
214
     * @return void
215
     *
216
     * @throws MissingTargetException
217
     */
218 5
    protected function importPaths(
219
        Finder $finder,
220
        OutputInterface $output,
221
        $host,
222
        $rewriteHost,
223
        $rewriteTo,
224
        $sync = false
225
    ) {
226 5
        $promises = [];
227
        /** @var SplFileInfo $file */
228 5
        foreach ($finder as $file) {
229 5
            $doc = $this->frontMatter->parse($file->getContents());
230
231 5
            $output->writeln("<info>Loading data from ${file}</info>");
232
233 5
            if (!array_key_exists('target', $doc->getData())) {
234 1
                throw new MissingTargetException('Missing target in \'' . $file . '\'');
235
            }
236
237 4
            $targetUrl = sprintf('%s%s', $host, $doc->getData()['target']);
238
239 4
            $promises[] = $this->importResource(
240 4
                $targetUrl,
241 4
                (string) $file,
242 4
                $output,
243 4
                $doc,
244 4
                $rewriteHost,
245 4
                $rewriteTo,
246
                $sync
247 4
            );
248 4
        }
249
250
        try {
251
            Promise\unwrap($promises);
252
        } catch (ClientException $e) {
253
            // silently ignored since we already output an error when the promise fails
254
        }
255
    }
256
257
    /**
258
     * @param string          $targetUrl   target url to import resource into
259
     * @param string          $file        path to file being loaded
260
     * @param OutputInterface $output      output of the command
261
     * @param Document        $doc         document to load
262
     * @param string          $rewriteHost string to replace with value from $host during loading
263
     * @param string          $rewriteTo   string to replace value from $rewriteHost with during loading
264
     * @param boolean         $sync        send requests syncronously
265
     *
266
     * @return Promise\PromiseInterface|null
267
     */
268
    protected function importResource(
269
        $targetUrl,
270
        $file,
271
        OutputInterface $output,
272
        Document $doc,
273
        $rewriteHost,
274
        $rewriteTo,
275
        $sync = false
276
    ) {
277 4
        $content = str_replace($rewriteHost, $rewriteTo, $doc->getContent());
278 4
        $uploadFile = $this->validateUploadFile($doc, $file);
279
280
        $successFunc = function (ResponseInterface $response) use ($output) {
281 3
            $output->writeln(
282 3
                '<comment>Wrote ' . $response->getHeader('Link')[0] . '</comment>'
283 3
            );
284 4
        };
285
286
        $errFunc = function (RequestException $e) use ($output, $file) {
287 1
            $this->errors[$file] = $e->getMessage();
288 1
            $output->writeln(
289 1
                '<error>' . str_pad(
290 1
                    sprintf(
291 1
                        'Failed to write <%s> from \'%s\' with message \'%s\'',
292 1
                        $e->getRequest()->getUri(),
293 1
                        $file,
294 1
                        $e->getMessage()
295 1
                    ),
296 1
                    140,
297
                    ' '
298 1
                ) . '</error>'
299 1
            );
300 1
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
301 1
                $this->dumper->dump(
302 1
                    $this->cloner->cloneVar(
303 1
                        $this->parser->parse($e->getResponse()->getBody(), false, false, true)
304 1
                    ),
305
                    function ($line, $depth) use ($output) {
306 1
                        if ($depth > 0) {
307 1
                            $output->writeln(
308 1
                                '<error>' . str_pad(str_repeat('  ', $depth) . $line, 140, ' ') . '</error>'
309 1
                            );
310 1
                        }
311 1
                    }
312 1
                );
313 1
            }
314 4
        };
315
316
        $data = [
317 4
            'json'   => $this->parseContent($content, $file),
318 4
            'upload' => $uploadFile,
319 4
            'headers'=> []
320 4
        ];
321
322
        // Authentication or custom headers.
323 4
        if ($this->headerBasicAuth) {
324
            $data['headers']['Authorization'] = 'Basic '. base64_encode($this->headerBasicAuth);
325
        }
326 4
        if ($this->customHeaders) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->customHeaders 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...
327
            foreach ($this->customHeaders as $headers) {
328
                list($key, $value) = explode(':', $headers);
329
                $data['headers'][$key] = $value;
330
            }
331
        }
332 4
        if (empty($data['headers'])) {
333 4
            unset($data['headers']);
334 4
        }
335
336 4
        $promise = $this->client->requestAsync(
337 4
            'PUT',
338 4
            $targetUrl,
339
            $data
340 4
        );
341
342
        // If there is a file to be uploaded, and it exists in remote, we delete it first.
343
        // TODO This part, $uploadFile, promise should be removed once Graviton/File service is resolved in new Story.
344 4
        $fileRepeatFunc = false;
345 4
        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...
346
            $fileRepeatFunc = function () use ($targetUrl, $successFunc, $errFunc, $output, $file, $data) {
347
                unset($this->errors[$file]);
348
                $output->writeln('<info>File deleting: '.$targetUrl.'</info>');
349
                $deleteRequest = $this->client->requestAsync('DELETE', $targetUrl);
350
                $insert = function () use ($targetUrl, $successFunc, $errFunc, $output, $data) {
351
                    $output->writeln('<info>File inserting: '.$targetUrl.'</info>');
352
                    $promiseInsert = $this->client->requestAsync('PUT', $targetUrl, $data);
353
                    $promiseInsert->then($successFunc, $errFunc);
354
                };
355
                $deleteRequest
356
                    ->then($insert, $errFunc)->wait();
357 1
            };
358 1
        }
359
360 4
        $promiseError = $fileRepeatFunc ? $fileRepeatFunc : $errFunc;
361 4
        if ($sync) {
362
            $promise->then($successFunc, $promiseError)->wait();
363
        } else {
364 4
            $promise->then($successFunc, $promiseError);
365
        }
366
367
368
369 4
        return $promise;
370
    }
371
372
    /**
373
     * parse contents of a file depending on type
374
     *
375
     * @param string $content contents part of file
376
     * @param string $file    full path to file
377
     *
378
     * @return mixed
379
     * @throws UnknownFileTypeException
380
     * @throws JsonParseException
381
     */
382
    protected function parseContent($content, $file)
383
    {
384 4
        if (substr($file, -5) == '.json') {
385 3
            $data = json_decode($content);
386 3
            if (json_last_error() !== JSON_ERROR_NONE) {
387
                throw new JsonParseException(
388
                    sprintf(
389
                        '%s in %s',
390
                        json_last_error_msg(),
391
                        $file
392
                    )
393
                );
394
            }
395 4
        } elseif (substr($file, -4) == '.yml') {
396 1
            $data = $this->parser->parse($content);
397 1
        } else {
398
            throw new UnknownFileTypeException($file);
399
        }
400
401 4
        return $data;
402
    }
403
404
    /**
405
     * Checks if file exists and return qualified fileName location
406
     *
407
     * @param Document $doc        Data source for import data
408
     * @param string   $originFile Original full filename used toimport
409
     * @return bool|mixed
410
     */
411
    private function validateUploadFile(Document $doc, $originFile)
412
    {
413 4
        $documentData = $doc->getData();
414
415 4
        if (!array_key_exists('file', $documentData)) {
416 3
            return false;
417
        }
418
419
        // Find file
420 1
        $fileName = dirname($originFile) . DIRECTORY_SEPARATOR . $documentData['file'];
421 1
        $fileName = str_replace('//', '/', $fileName);
422 1
        if (!file_exists($fileName)) {
423
            return false;
424
        }
425
426 1
        return $fileName;
427
    }
428
}
429