add Basic Shopware API

This commit is contained in:
Marko 2022-06-28 09:17:30 +02:00
parent f2107b024e
commit b5085fc848
5 changed files with 744 additions and 162 deletions

6
.env
View File

@ -31,7 +31,7 @@ DATABASE_URL="mysql://root:root@mysql:3306/cds_connector?serverVersion=5.7&chars
CORS_ALLOW_ORIGIN='^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$'
###< nelmio/cors-bundle ###
SHOPWARE_API_URL="http://development.dev.localhost"
SHOPWARE_API_KEY="dkVsVndWV2M4dVJLZEhMbkdMNWlLSXJsTXVjMW5jdVNrNzlBOFE"
SHOPWARE_API_ID="SWIAVTJ1ZDLLY2TJT1D0ZEDBAQ"
SHOPWARE_API_URL="development.dev.localhost"
SHOPWARE_API_KEY="S2hySDNyZ214dGRVQmFBSURQbkRvajIyZldxOHB4RktZakxaYmo"
SHOPWARE_API_ID="SWUAB1JGAFHTMME5DUGWDWN1EQ"

View File

@ -93,6 +93,14 @@
<path value="$PROJECT_DIR$/vendor/sensio/framework-extra-bundle" />
<path value="$PROJECT_DIR$/vendor/symfony/http-client" />
<path value="$PROJECT_DIR$/vendor/symfony/http-client-contracts" />
<path value="$PROJECT_DIR$/vendor/ralouphie/getallheaders" />
<path value="$PROJECT_DIR$/vendor/psr/http-factory" />
<path value="$PROJECT_DIR$/vendor/psr/http-client" />
<path value="$PROJECT_DIR$/vendor/psr/http-message" />
<path value="$PROJECT_DIR$/vendor/guzzlehttp/promises" />
<path value="$PROJECT_DIR$/vendor/guzzlehttp/guzzle" />
<path value="$PROJECT_DIR$/vendor/guzzlehttp/psr7" />
<path value="$PROJECT_DIR$/vendor/vin-sw/shopware-sdk" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.1" />

View File

@ -32,7 +32,8 @@
"symfony/serializer": "6.1.*",
"symfony/twig-bundle": "6.1.*",
"symfony/validator": "6.1.*",
"symfony/yaml": "6.1.*"
"symfony/yaml": "6.1.*",
"vin-sw/shopware-sdk": "^1.4"
},
"require-dev": {
"symfony/debug-bundle": "6.1.*",

822
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -19,6 +19,14 @@ 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\Order\OrderDefinition;
use Vin\ShopwareSdk\Data\Entity\Product\ProductDefinition;
use Vin\ShopwareSdk\Data\Filter\EqualsFilter;
use Vin\ShopwareSdk\Factory\RepositoryFactory;
#[AsCommand(
@ -84,18 +92,15 @@ class SwGetOrdersCommand extends Command
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');
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());
}
$body = $response->getContent();
var_dump($body);
}
/**
@ -106,39 +111,29 @@ class SwGetOrdersCommand extends Command
* @throws ClientExceptionInterface
* @throws \Exception
*/
private function getOrdersFromSW(array $orders):void
private function getOrdersFromSW(array $ordersArr):void
{
$context = new Context($_ENV['SHOPWARE_API_URL'], $this->shopwareAuth());
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();
// Create the repository for the entity
$orderRepository = RepositoryFactory::create(OrderDefinition::ENTITY_NAME);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw new \Exception($e->getMessage());
}
// Create the criteria
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('orderNumber', '9f002cc7de704a6ab31e1b410547cb97'));
$criteria->addAssociation('order.orderDetails.product');
// 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);
var_dump($body);
}
}