add Basic Shopware API

add Tests
This commit is contained in:
Marko
2022-06-28 17:20:37 +02:00
parent b5085fc848
commit 460411ab43
13 changed files with 2328 additions and 247 deletions

View File

@@ -3,28 +3,25 @@
namespace App\Command;
use App\Controller\ShopwareController;
use App\Entity\Orders;
use App\Repository\OrdersRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Doctrine\ORM\Mapping as ORM;
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;
use Vin\ShopwareSdk\Client\AdminAuthenticator;
use Vin\ShopwareSdk\Client\GrantType\ClientCredentialsGrantType;
use Vin\ShopwareSdk\Data\Context;
use Vin\ShopwareSdk\Data\Criteria;
use Vin\ShopwareSdk\Data\Entity\EntityCollection;
use Vin\ShopwareSdk\Data\Entity\Order\OrderDefinition;
use Vin\ShopwareSdk\Data\Entity\Product\ProductDefinition;
use Vin\ShopwareSdk\Data\Filter\EqualsAnyFilter;
use Vin\ShopwareSdk\Data\Filter\EqualsFilter;
use Vin\ShopwareSdk\Factory\RepositoryFactory;
@@ -36,60 +33,74 @@ use Vin\ShopwareSdk\Factory\RepositoryFactory;
class SwGetOrdersCommand extends Command
{
private $ordersRepository;
private $client;
private $logger;
private $orderData;
public function __construct(OrdersRepository $ordersRepository, HttpClientInterface $client, LoggerInterface $logger )
public function __construct(OrdersRepository $ordersRepository, 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')
;
// $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')) {
// ...
}
// $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();
$this->orderData = $this->getOrders();
if(!$orders) {
$io->error('Keine Bestellungen gefunden');
return Command::FAILURE;
}
//Bestelldetails aus SW holen
$this->getOrdersFromSW($orders);
$this->getOrdersDataFromSW();
foreach ($this->orderData as $order) {
$io->info('Bestellung ID: '.$order->getId());
$io->info('Bestellung Order-ID: '.$order->getOrderId());
$io->info('Bestellung Data: '.var_export($order->getData(),1));
$this->saveOrdersData($order);
}
$io->success('Done!');
return Command::SUCCESS;
}
/**
* @return array
*/
private function getOrders():array
{
return $this->ordersRepository->findAll();
}
/**
* @return \Vin\ShopwareSdk\Data\AccessToken|void
*/
private function shopwareAuth(){
try{
@@ -99,41 +110,50 @@ class SwGetOrdersCommand extends Command
}catch (\Exception $e){
$this->logger->error($e->getMessage());
}
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws \Exception
* holt alle fehlende Bestelldetails aus SW
*/
private function getOrdersFromSW(array $ordersArr):void
{
private function getOrdersDataFromSW(): void
{
foreach ($this->orderData as $value) {
$context = new Context($_ENV['SHOPWARE_API_URL'], $this->shopwareAuth());
// Bei Shopware API anmelden
$context = new Context($_ENV['SHOPWARE_API_URL'], $this->shopwareAuth());
// Create the repository for the entity
$orderRepository = RepositoryFactory::create(OrderDefinition::ENTITY_NAME);
// Create the repository for the entity
$orderRepository = RepositoryFactory::create(OrderDefinition::ENTITY_NAME);
// Create the criteria
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('orderNumber', '9f002cc7de704a6ab31e1b410547cb97'));
// Create the criteria
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $value->getOrderId()));
$criteria->addAssociation('order.orderDetails.product');
//Beziehungen zu Produkten holen
$criteria->addAssociation('lineItems');
// Using this criteria and the context object that you create from authentication step, you can retrieving the result
$orders = $orderRepository->search($criteria, $context);
var_dump($orders);
try {
$orders = $orderRepository->search($criteria, $context);
$value->setData((array)$orders->getEntities());
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
}
/**
* @param $orderData
* @return void
*/
private function saveOrdersData($orderData): void
{
$orderData->setStatus = 1;
$this->ordersRepository->add($orderData,true);
}
}

View File

@@ -5,6 +5,8 @@ namespace App\Repository;
use App\Entity\Orders;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* @extends ServiceEntityRepository<Orders>
@@ -16,13 +18,21 @@ use Doctrine\Persistence\ManagerRegistry;
*/
class OrdersRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
private ValidatorInterface $validator;
public function __construct(ManagerRegistry $registry,ValidatorInterface $validator)
{
parent::__construct($registry, Orders::class);
$this->validator = $validator;
}
public function add(Orders $entity, bool $flush = false): void
{
$errors = $this->validator->validate($entity);
if (count($errors) > 0) {
var_dump($errors);
}
$this->getEntityManager()->persist($entity);
if ($flush) {
@@ -30,6 +40,18 @@ class OrdersRepository extends ServiceEntityRepository
}
}
public function update(Orders $entity, bool $flush = false): void
{
$order = $this->getEntityManager()->find(Orders::class, $entity->getId());
if(!$order) {
throw $this->createNotFoundException('Order not found: '.$entity->getId());
}
}
public function remove(Orders $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);