custom/plugins/BjerregaardTheme/src/Service/OdataService.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace BjerregaardTheme\Service;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Exception\GuzzleException;
  5. use GuzzleHttp\RequestOptions;
  6. use Psr\Cache\InvalidArgumentException;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  9. use Symfony\Contracts\Cache\ItemInterface;
  10. class OdataService
  11. {
  12.     private Client $Client;
  13.     public function __construct(SystemConfigService $systemConfigService)
  14.     {
  15.         $this->Client = new Client([
  16.             'base_uri' => $systemConfigService->get('BjerregaardTheme.config.bcURL'),
  17.             'headers' => [
  18.                 'accept-language' => 'da-DK,da',
  19.                 "Authorization" => "Bearer " $this->getBearerToken(
  20.                     $systemConfigService->get('BjerregaardTheme.config.bcTenantId'),
  21.                     $systemConfigService->get('BjerregaardTheme.config.bcClientId'),
  22.                     $systemConfigService->get('BjerregaardTheme.config.bcClientSecret')
  23.                 )
  24.             ]
  25.         ]);
  26.     }
  27.     private function getAccessToken(string $tenantIdstring $clientIdstring $clientSecret): mixed
  28.     {
  29.         $request null;
  30.         $params = [
  31.             'grant_type' => 'client_credentials',
  32.             'client_id' => $clientId,
  33.             'client_secret' => $clientSecret,
  34.             'scope' => 'https://api.businesscentral.dynamics.com/.default'
  35.         ];
  36.         try {
  37.             $client = new Client();
  38.             $request $client->request('POST'"https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token", ['form_params' => $params]);
  39.         } catch (GuzzleException $e) {
  40.             echo $e->getMessage();
  41.         }
  42.         return json_decode($request->getBody()->getContents());
  43.     }
  44.     private function getBearerToken(string $tenantIdstring $clientIdstring $clientSecret): string
  45.     {
  46.         $cache = new FilesystemAdapter();
  47.         /** @var ItemInterface $bearertoken */
  48.         $bearertoken $cache->getItem("bearer_token");
  49.         if (!$bearertoken->isHit())
  50.         {
  51.             $accesstoken $this->getAccessToken($tenantId$clientId$clientSecret);
  52.             $bearertoken->set($accesstoken);
  53.             $bearertoken->expiresAfter($accesstoken->expires_in 600);
  54.             $cache->save($bearertoken);
  55.         }
  56.         return $bearertoken->get()->access_token;
  57.     }
  58.     /**
  59.      * @param string $service
  60.      * @param array<string, mixed>|null $query
  61.      * @return mixed
  62.      * @throws GuzzleException
  63.      */
  64.     public function get(string $service, array $query null): mixed
  65.     {
  66.         $data $this->Client->request('GET'$service, ['query' => $query]);
  67.         $return json_decode($data->getBody()->getContents());
  68.         if (empty($return)) {
  69.             return $data;
  70.         }
  71.         return $return;
  72.     }
  73. }