123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- namespace FD3;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
- use GuzzleHttp\Client as GuzzClient;
- use GuzzleHttp\Exception\ServerException as RequestException;
- use GuzzleHttp\Psr7;
- class ImportClient extends Command{
- protected function configure()
- {
- $this
- ->setName('import:client')
- ->setDescription('.')
- ->setHelp('Client.json shuld be a valid json file, probably looking like the following example:
- [
- {"externalId": "abcdefg","name": "Name of client","address": "Address of client"},
- {"externalId": "abcdefg","name": "Name of client","address": "Address of client"},
- {"externalId": "abcdefg","name": "Name of client","address": "Address of client"},
- {"externalId": "abcdefg","name": "Name of client","address": "Address of client"}
- ]
- ')
- ->addArgument('base_url', InputArgument::REQUIRED, 'The base url of the FD3 Client.')
- ->addArgument('data_file.json', InputArgument::OPTIONAL, 'The data source from where to get data.', "Client.json")
- ->addOption('uri', null, InputOption::VALUE_REQUIRED, 'uri after the base_url for the complete URL', '/api/clients.json')
- ->addOption('user', null, InputOption::VALUE_REQUIRED, 'The FD3 Username.', 'admin')
- ->addOption('pass', null, InputOption::VALUE_REQUIRED, 'The FD3 Password.', 'adminpass')
- // ->addOption('source-name', null, InputOption::VALUE_REQUIRED, 'Rename the source to this name.', "upstream")
- ;
- }
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $base_url = $input->getArgument("base_url");
- $uri = $input->getOption("uri");
- $user = $input->getOption('user');
- $pass = $input->getOption('pass');
- $file = $input->getArgument("data_file.json");
- $url = $base_url.$uri;
- $parser = new \JsonCollectionParser\Parser();
- $parser->parse($file, function($data) use ($url, $user, $pass){
- $client = new GuzzClient();
- try{
- $res = $client->request("POST", $url, [ 'auth' => [$user, $pass], 'body' => json_encode($data) ]);
- echo $res->getBody();
- } catch (RequestException $e) {
- echo Psr7\str($e->getRequest());
- if ($e->hasResponse()) {
- echo Psr7\str($e->getResponse());
- }
- }
- }
- );
- }
- }
|