Completed
Push — feature/EVO-6215-file-error-ha... ( e4d646 )
by
unknown
03:16
created

ImportCommand::doImport()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 28
rs 8.5806
cc 4
eloc 18
nc 6
nop 3
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
     * @param HttpClient  $client      Grv HttpClient guzzle http client
72
     * @param Finder      $finder      symfony/finder instance
73
     * @param FrontMatter $frontMatter frontmatter parser
74
     * @param Parser      $parser      yaml/json parser
75
     * @param VarCloner   $cloner      var cloner for dumping reponses
76
     * @param Dumper      $dumper      dumper for outputing responses
77
     */
78
    public function __construct(
79
        HttpClient $client,
80
        Finder $finder,
81
        FrontMatter $frontMatter,
82
        Parser $parser,
83
        VarCloner $cloner,
84
        Dumper $dumper
85
    ) {
86
        parent::__construct(
87
            $finder
88
        );
89
        $this->client = $client;
90
        $this->frontMatter = $frontMatter;
91
        $this->parser = $parser;
92
        $this->cloner = $cloner;
93
        $this->dumper = $dumper;
94
    }
95
96
    /**
97
     * Configures the current command.
98
     *
99
     * @return void
100
     */
101
    protected function configure()
102
    {
103
        $this
104
            ->setName('graviton:import')
105
            ->setDescription('Import files from a folder or file.')
106
            ->addOption(
107
                'rewrite-host',
108
                'r',
109
                InputOption::VALUE_OPTIONAL,
110
                'Replace the value of this option with the <host> value before importing.',
111
                'http://localhost'
112
            )
113
            ->addOption(
114
                'rewrite-to',
115
                't',
116
                InputOption::VALUE_OPTIONAL,
117
                'String to use as the replacement value for the [REWRITE-HOST] string.',
118
                '<host>'
119
            )
120
            ->addOption(
121
                'sync-requests',
122
                's',
123
                InputOption::VALUE_NONE,
124
                'Send requests synchronously'
125
            )
126
            ->addArgument(
127
                'host',
128
                InputArgument::REQUIRED,
129
                'Protocol and host to load data into (ie. https://graviton.nova.scapp.io)'
130
            )
131
            ->addArgument(
132
                'file',
133
                InputArgument::REQUIRED + InputArgument::IS_ARRAY,
134
                'Directories or files to load'
135
            );
136
    }
137
138
    /**
139
     * Executes the current command.
140
     *
141
     * @param Finder          $finder Finder
142
     * @param InputInterface  $input  User input on console
143
     * @param OutputInterface $output Output of the command
144
     *
145
     * @return void
146
     */
147
    protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output)
148
    {
149
        $host = $input->getArgument('host');
150
        $rewriteHost = $input->getOption('rewrite-host');
151
        $rewriteTo = $input->getOption('rewrite-to');
152
        if ($rewriteTo === $this->getDefinition()->getOption('rewrite-to')->getDefault()) {
153
            $rewriteTo = $host;
154
        }
155
        $sync = $input->getOption('sync-requests');
156
157
        $this->importPaths($finder, $output, $host, $rewriteHost, $rewriteTo, $sync);
158
159
        // Error exit
160
        if (empty($this->errors)) {
161
            // No errors
162
            $output->writeln("\n".'<info>No errors</info>');
163
            $output->writeln('0');
164
            exit(0);
165
        } else {
166
            // Yes, there was errors
167
            $output->writeln("\n".'<info>There was errors: '.count($this->errors).'</info>');
168
            foreach ($this->errors as $file => $error) {
169
                $output->writeln("<error>{$file}: {$error}</error>");
170
            }
171
            $output->writeln('1');
172
            exit(1);
173
        }
174
    }
175
176
    /**
177
     * @param Finder          $finder      finder primmed with files to import
178
     * @param OutputInterface $output      output interfac
179
     * @param string          $host        host to import into
180
     * @param string          $rewriteHost string to replace with value from $rewriteTo during loading
181
     * @param string          $rewriteTo   string to replace value from $rewriteHost with during loading
182
     * @param boolean         $sync        send requests syncronously
183
     *
184
     * @return void
185
     *
186
     * @throws MissingTargetException
187
     */
188
    protected function importPaths(
189
        Finder $finder,
190
        OutputInterface $output,
191
        $host,
192
        $rewriteHost,
193
        $rewriteTo,
194
        $sync = false
195
    ) {
196
        $promises = [];
197
        /** @var SplFileInfo $file */
198
        foreach ($finder as $file) {
199
            $doc = $this->frontMatter->parse($file->getContents());
200
201
            $output->writeln("<info>Loading data from ${file}</info>");
202
203
            if (!array_key_exists('target', $doc->getData())) {
204
                throw new MissingTargetException('Missing target in \'' . $file . '\'');
205
            }
206
207
            $targetUrl = sprintf('%s%s', $host, $doc->getData()['target']);
208
209
            $promises[] = $this->importResource(
210
                $targetUrl,
211
                (string) $file,
212
                $output,
213
                $doc,
214
                $rewriteHost,
215
                $rewriteTo,
216
                $sync
217
            );
218
        }
219
220
        try {
221
            Promise\unwrap($promises);
222
        } catch (ClientException $e) {
223
            // silently ignored since we already output an error when the promise fails
224
        }
225
    }
226
227
    /**
228
     * @param string          $targetUrl   target url to import resource into
229
     * @param string          $file        path to file being loaded
230
     * @param OutputInterface $output      output of the command
231
     * @param Document        $doc         document to load
232
     * @param string          $rewriteHost string to replace with value from $host during loading
233
     * @param string          $rewriteTo   string to replace value from $rewriteHost with during loading
234
     * @param boolean         $sync        send requests syncronously
235
     *
236
     * @return Promise\Promise|null
237
     */
238
    protected function importResource(
239
        $targetUrl,
240
        $file,
241
        OutputInterface $output,
242
        Document $doc,
243
        $rewriteHost,
244
        $rewriteTo,
245
        $sync = false
246
    ) {
247
        $content = str_replace($rewriteHost, $rewriteTo, $doc->getContent());
248
        $uploadFile = $this->validateUploadFile($doc, $file);
249
250
        $successFunc = function (ResponseInterface $response) use ($output) {
251
            $output->writeln(
252
                '<comment>Wrote ' . $response->getHeader('Link')[0] . '</comment>'
253
            );
254
        };
255
256
        $errFunc = function (RequestException $e) use ($output, $file) {
257
            $this->errors[$file] = $e->getMessage();
258
            $output->writeln(
259
                '<error>' . str_pad(
260
                    sprintf(
261
                        'Failed to write <%s> from \'%s\' with message \'%s\'',
262
                        $e->getRequest()->getUri(),
263
                        $file,
264
                        $e->getMessage()
265
                    ),
266
                    140,
267
                    ' '
268
                ) . '</error>'
269
            );
270
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
271
                $this->dumper->dump(
272
                    $this->cloner->cloneVar(
273
                        $this->parser->parse($e->getResponse()->getBody(), false, false, true)
274
                    ),
275
                    function ($line, $depth) use ($output) {
276
                        if ($depth > 0) {
277
                            $output->writeln(
278
                                '<error>' . str_pad(str_repeat('  ', $depth) . $line, 140, ' ') . '</error>'
279
                            );
280
                        }
281
                    }
282
                );
283
            }
284
        };
285
286
        $data = [
287
            'json'   => $this->parseContent($content, $file),
288
            'upload' => $uploadFile
289
        ];
290
        $promise = $this->client->requestAsync(
291
            'PUT',
292
            $targetUrl,
293
            $data
294
        );
295
296
        // If there is a file to be uploaded, and it exists in remote, we delete it first.
297
        $fileRepeatFunc = false;
298
        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...
299
            $fileRepeatFunc = function () use ($targetUrl, $successFunc, $errFunc, $output, $file, $data) {
300
                unset($this->errors[$file]);
301
                $output->writeln('<info>File deleting: '.$targetUrl.'</info>');
302
                $deleteRequest = $this->client->requestAsync('DELETE', $targetUrl);
303
                $insert = function () use ($targetUrl, $successFunc, $errFunc, $output, $data) {
304
                    $output->writeln('<info>File inserting: '.$targetUrl.'</info>');
305
                    $promiseInsert = $this->client->requestAsync('PUT', $targetUrl, $data);
306
                    $promiseInsert->then($successFunc, $errFunc);
307
                };
308
                $deleteRequest
309
                    ->then($insert, $errFunc)->wait();
310
            };
311
        }
312
313
        $promiseError = $fileRepeatFunc ? $fileRepeatFunc : $errFunc;
314
        if ($sync) {
315
            $promise->then($successFunc, $promiseError)->wait();
316
        } else {
317
            $promise->then($successFunc, $promiseError);
318
        }
319
320
321
322
        return $promise;
323
    }
324
325
    /**
326
     * parse contents of a file depending on type
327
     *
328
     * @param string $content contents part of file
329
     * @param string $file    full path to file
330
     *
331
     * @return mixed
332
     * @throws UnknownFileTypeException
333
     * @throws JsonParseException
334
     */
335
    protected function parseContent($content, $file)
336
    {
337
        if (substr($file, -5) == '.json') {
338
            $data = json_decode($content);
339
            if (json_last_error() !== JSON_ERROR_NONE) {
340
                throw new JsonParseException(
341
                    sprintf(
342
                        '%s in %s',
343
                        json_last_error_msg(),
344
                        $file
345
                    )
346
                );
347
            }
348
        } elseif (substr($file, -4) == '.yml') {
349
            $data = $this->parser->parse($content);
350
        } else {
351
            throw new UnknownFileTypeException($file);
352
        }
353
354
        return $data;
355
    }
356
357
    /**
358
     * Checks if file exists and return qualified fileName location
359
     *
360
     * @param Document $doc        Data source for import data
361
     * @param string   $originFile Original full filename used toimport
362
     * @return bool|mixed
363
     */
364
    private function validateUploadFile(Document $doc, $originFile)
365
    {
366
        $documentData = $doc->getData();
367
368
        if (!array_key_exists('file', $documentData)) {
369
            return false;
370
        }
371
372
        // Find file
373
        $fileName = dirname($originFile) . DIRECTORY_SEPARATOR . $documentData['file'];
374
        $fileName = str_replace('//', '/', $fileName);
375
        if (!file_exists($fileName)) {
376
            return false;
377
        }
378
379
        return $fileName;
380
    }
381
}
382