<?php declare(strict_types=1); /* * This file is part of vonRotenberg Shopware API Bundle. * * (c) vonRotenberg * * @license proprietary */ namespace vonRotenberg\ShopwareApiBundle\API; use Contao\System; use Symfony\Contracts\HttpClient\HttpClientInterface; class Shopware { protected $httpClient; protected $clientId; protected $clientSecret; protected $apiEndpoint; public function __construct(HttpClientInterface $httpClient) { $this->httpClient = $httpClient; $this->clientId = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.client_id'); $this->clientSecret = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.client_secret'); $this->apiEndpoint = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.api_endpoint'); } protected function getClientId() { return $this->clientId; } protected function getClientSecret() { return $this->clientSecret; } protected function getApiEndpoint() { return rtrim($this->apiEndpoint,'/'); } protected function sendRequest(string $relEndpoint, array $options, string $method = 'GET',bool $blnFQDNEndpoint=false) { if ($blnFQDNEndpoint) { return $this->httpClient->request($method,$relEndpoint,$options); } $relEndpoint = '/' . ltrim($relEndpoint,'/'); return $this->httpClient->request($method,$this->getApiEndpoint().$relEndpoint,$options); } public function testApiRequest() { $options = [ 'body' => json_encode([ 'grant_type' => 'client_credentials', 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret ]), 'headers' => ['Content-Type: application/json', 'Accept: application/json'], ]; $response = $this->sendRequest('oauth/token',$options,'POST'); if ($response->getStatusCode() == 200) { $content = $response->getContent(); return json_decode($content); } return null; } }