add Basic Shopware API
This commit is contained in:
144
src/Command/SwGetOrdersCommand.php
Normal file
144
src/Command/SwGetOrdersCommand.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Controller\ShopwareController;
|
||||
use App\Repository\OrdersRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
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\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
|
||||
#[AsCommand(
|
||||
name: 'sw:get-orders',
|
||||
description: 'Holt alle offenen Bestellungen von SW ab',
|
||||
)]
|
||||
class SwGetOrdersCommand extends Command
|
||||
{
|
||||
private $ordersRepository;
|
||||
private $client;
|
||||
private $logger;
|
||||
|
||||
|
||||
public function __construct(OrdersRepository $ordersRepository, HttpClientInterface $client, LoggerInterface $logger )
|
||||
{
|
||||
$this->ordersRepository = $ordersRepository;
|
||||
$this->client = $client;
|
||||
$this->logger = $logger;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
|
||||
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$arg1 = $input->getArgument('arg1');
|
||||
|
||||
if ($arg1) {
|
||||
$io->note(sprintf('You passed an argument: %s', $arg1));
|
||||
}
|
||||
|
||||
if ($input->getOption('option1')) {
|
||||
// ...
|
||||
}
|
||||
|
||||
//offene Bestellungen aus Datenbank holen
|
||||
$orders = $this->getOrders();
|
||||
|
||||
if(!$orders) {
|
||||
$io->error('Keine Bestellungen gefunden');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
//Bestelldetails aus SW holen
|
||||
$this->getOrdersFromSW($orders);
|
||||
|
||||
|
||||
$io->success('Done!');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function getOrders():array
|
||||
{
|
||||
return $this->ordersRepository->findAll();
|
||||
}
|
||||
|
||||
private function shopwareAuth(){
|
||||
|
||||
$this->client->withOptions([
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'sw-access-key' => 'SWSCY1NPSKHPSFNHWGDLTMM5NQ'
|
||||
],
|
||||
]);
|
||||
$response = $this->client->request('GET','http://localhost/store-api/context');
|
||||
|
||||
|
||||
$body = $response->getContent();
|
||||
|
||||
var_dump($body);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function getOrdersFromSW(array $orders):void
|
||||
{
|
||||
|
||||
|
||||
try {
|
||||
$response = $this->client->request('POST', $_ENV['SHOPWARE_API_URL'].'/store-api/search', [
|
||||
'headers' => [
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json',
|
||||
'sw-access-key' => $_ENV['SHOPWARE_API_KEY']
|
||||
],
|
||||
'body' => json_encode([
|
||||
'limit' => 100,
|
||||
'page' => 1,
|
||||
'query' => [
|
||||
'filter' => [
|
||||
'type' => 'equals',
|
||||
'field' => 'order.number',
|
||||
'value' => '12345'
|
||||
]
|
||||
]
|
||||
])
|
||||
]);
|
||||
$body = $response->toArray();
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
throw new \Exception($e->getMessage());
|
||||
}
|
||||
|
||||
|
||||
|
||||
var_dump($body);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user