This commit is contained in:
buddy
2022-07-19 09:45:45 +00:00
parent 3d8cca58dd
commit 52fae1a695
8 changed files with 99 additions and 92 deletions

View File

@@ -25,7 +25,6 @@ use Vin\ShopwareSdk\Data\Filter\EqualsAnyFilter;
use Vin\ShopwareSdk\Data\Filter\EqualsFilter;
use Vin\ShopwareSdk\Factory\RepositoryFactory;
#[AsCommand(
name: 'sw:get-orders',
description: 'Holt alle offenen Bestellungen von SW ab',
@@ -37,7 +36,7 @@ class SwGetOrdersCommand extends Command
private $orderData;
public function __construct(OrdersRepository $ordersRepository, LoggerInterface $logger )
public function __construct(OrdersRepository $ordersRepository, LoggerInterface $logger)
{
$this->ordersRepository = $ordersRepository;
$this->logger = $logger;
@@ -80,7 +79,7 @@ class SwGetOrdersCommand extends Command
*/
private function getOrders():array
{
return $this->ordersRepository->findAll();
return $this->ordersRepository->findAll();
}
/**
@@ -89,57 +88,55 @@ class SwGetOrdersCommand extends Command
public function getOrderDetails(): void
{
//Bestelldetails aus SW holen
$this->getOrdersDataFromSW();
$this->getOrdersDataFromSW();
foreach ($this->orderData as $order) {
$this->saveOrdersData($order);
}
foreach ($this->orderData as $order) {
$this->saveOrdersData($order);
}
}
/**
* @return \Vin\ShopwareSdk\Data\AccessToken|void
*/
private function shopwareAuth(){
private function shopwareAuth()
{
try{
$grantType = new ClientCredentialsGrantType($_ENV['SHOPWARE_API_ID'], $_ENV['SHOPWARE_API_KEY']);
$adminClient = new AdminAuthenticator($grantType, $_ENV['SHOPWARE_API_URL']);
return $adminClient->fetchAccessToken();
}catch (\Exception $e){
$this->logger->error($e->getMessage());
}
try {
$grantType = new ClientCredentialsGrantType($_ENV['SHOPWARE_API_ID'], $_ENV['SHOPWARE_API_KEY']);
$adminClient = new AdminAuthenticator($grantType, $_ENV['SHOPWARE_API_URL']);
return $adminClient->fetchAccessToken();
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
/**
* holt alle fehlende Bestelldetails aus SW
*/
private function getOrdersDataFromSW(): void
{
foreach ($this->orderData as $value) {
private function getOrdersDataFromSW(): void
{
foreach ($this->orderData as $value) {
// Bei Shopware API anmelden
$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('id', $value->getOrderId()));
// Create the criteria
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $value->getOrderId()));
//Beziehungen zu Produkten holen
$criteria->addAssociation('lineItems');
//Beziehungen zu Produkten holen
$criteria->addAssociation('lineItems');
try {
$orders = $orderRepository->search($criteria, $context);
try {
$orders = $orderRepository->search($criteria, $context);
$value->setData((array)$orders->getEntities());
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
$value->setData((array)$orders->getEntities());
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
}
}
/**
@@ -149,9 +146,6 @@ class SwGetOrdersCommand extends Command
private function saveOrdersData($orderData): void
{
$orderData->setStatus = 1;
$this->ordersRepository->add($orderData,true);
$this->ordersRepository->add($orderData, true);
}
}