add Basic Shopware API

This commit is contained in:
Marko
2022-06-27 10:50:23 +02:00
parent f35f8adceb
commit f2107b024e
12 changed files with 723 additions and 27 deletions

View 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);
}
}

View File

@@ -2,13 +2,18 @@
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use App\Repository\OrdersRepository;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
#[ORM\Entity(repositoryClass: OrdersRepository::class)]
#[ApiResource(itemOperations: ["GET"])]
#[ApiResource(
itemOperations: ["GET"],
description: "Manage orders",
normalizationContext: ["groups" => "read"]
)]
class Orders
{
#[ORM\Id]
@@ -19,12 +24,16 @@ class Orders
/** Shopware Order ID */
#[ORM\Column(type: 'string', length: 255)]
#[Assert\NotBlank]
#[ApiProperty(example: "467e9804347c4071942c99b55b108142")]
#[Group("read")]
private $order_id;
#[ORM\Column(type: 'integer')]
#[ApiProperty(example: "1")]
private $status;
#[ORM\Column(type: 'json')]
#[ApiProperty(writable: false)]
private $data = [];
public function getId(): ?int

View File

@@ -39,28 +39,33 @@ class OrdersRepository extends ServiceEntityRepository
}
}
// /**
// * @return Orders[] Returns an array of Orders objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('o')
// ->andWhere('o.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('o.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
/**
* @return Orders[] Returns an array of Orders objects
*/
public function findByStatus($status): array
{
return $this->createQueryBuilder('o')
->andWhere('o.status = :val')
->setParameter('val', $status)
->orderBy('o.id', 'ASC')
->setMaxResults(100)
->getQuery()
->getResult()
;
}
// public function findOneBySomeField($value): ?Orders
// {
// return $this->createQueryBuilder('o')
// ->andWhere('o.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
/**
* @param $orderId
* @return Orders|null
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneByOrderId($orderId): ?Orders
{
return $this->createQueryBuilder('o')
->andWhere('o.order_id = :val')
->setParameter('val', $orderId)
->getQuery()
->getOneOrNullResult()
;
}
}