<?php declare(strict_types=1); /* * This file is part of vonRotenberg Shopware API Bundle. * * (c) vonRotenberg * * @license proprietary */ namespace vonRotenberg\ShopwareApiBundle\Command; use Contao\CoreBundle\Framework\ContaoFramework; use Contao\System; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\VarDumper\Cloner\VarCloner; use Symfony\Component\VarDumper\Dumper\CliDumper; use vonRotenberg\ShopwareApiBundle\API\Shopware; class ShopwareApiTester extends Command { protected static $defaultName = 'shopware:api-test'; protected $framework; public function __construct(ContaoFramework $framework) { $this->framework = $framework; parent::__construct(); } protected function configure(): void { $this ->setName(self::$defaultName) ->setDescription('Query the Shopware API') ->addOption('authenticate','a',InputOption::VALUE_NONE, 'Use Token authentication with request') ; } protected function execute(InputInterface $input, OutputInterface $output): int { $this->framework->initialize(); $cloner = new VarCloner(); $dumper = new CliDumper(); $io = new SymfonyStyle($input, $output); $io->title('Shopware API Tester'); $strUrlFragment = $io->ask('Enter URL fragment'); $strMethod = $io->choice('Request type',['GET','POST','PUT','DELETE','PATCH'], 'GET'); $strBody = $io->ask('Enter request body (JSON)', null, function ($strBody) { if ($strBody !== null && !json_validate($strBody)) { throw new \RuntimeException('Invalid JSON body'); } return $strBody; }); /** @var Shopware $Shopware */ $Shopware = System::getContainer()->get(Shopware::class); $Result = $Shopware->queryAPI($strUrlFragment,$strBody, $strMethod, (bool)$input->getOption('authenticate')); if ($Result->getStatusCode() === 200) { $dumper->dump(($cloner->cloneVar(json_validate($Result->getContent()) ? json_decode($Result->getContent(), true) : $Result))); $io->success('Successful response'); return self::SUCCESS; } $io->error(sprintf('Error when querying the Shopware API. HTTP Status returned: %s',$Result->getStatusCode())); return self::FAILURE; } }