Browse code

Basic setup for API service class and test api command

Benjamin Roth authored on18/10/2024 14:20:37
Showing3 changed files
... ...
@@ -7,3 +7,6 @@ services:
7 7
     vonRotenberg\ShopwareApiBundle\:
8 8
         resource: ../src
9 9
         exclude: ../src/{VonrotenbergShopwareApiBundle.php,ContaoManager,Entity,Migrations,Model,Resources,Tests,Widget}
10
+
11
+    vonRotenberg\ShopwareApiBundle\API\Shopware:
12
+        public: true
10 13
new file mode 100644
... ...
@@ -0,0 +1,83 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of vonRotenberg Shopware API Bundle.
7
+ *
8
+ * (c) vonRotenberg
9
+ *
10
+ * @license proprietary
11
+ */
12
+
13
+namespace vonRotenberg\ShopwareApiBundle\API;
14
+
15
+use Contao\System;
16
+use Symfony\Contracts\HttpClient\HttpClientInterface;
17
+
18
+class Shopware
19
+{
20
+    protected $httpClient;
21
+    protected $clientId;
22
+    protected $clientSecret;
23
+    protected $apiEndpoint;
24
+
25
+    public function __construct(HttpClientInterface $httpClient)
26
+    {
27
+        $this->httpClient = $httpClient;
28
+
29
+        $this->clientId = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.client_id');
30
+        $this->clientSecret = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.client_secret');
31
+        $this->apiEndpoint = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.api_endpoint');
32
+    }
33
+
34
+    protected function getClientId()
35
+    {
36
+        return $this->clientId;
37
+    }
38
+
39
+    protected function getClientSecret()
40
+    {
41
+        return $this->clientSecret;
42
+    }
43
+
44
+    protected function getApiEndpoint()
45
+    {
46
+        return rtrim($this->apiEndpoint,'/');
47
+    }
48
+
49
+    protected function sendRequest(string $relEndpoint, array $options, string $method = 'GET',bool $blnFQDNEndpoint=false)
50
+    {
51
+        if ($blnFQDNEndpoint)
52
+        {
53
+            return $this->httpClient->request($method,$relEndpoint,$options);
54
+        }
55
+
56
+        $relEndpoint = '/' . ltrim($relEndpoint,'/');
57
+
58
+        return $this->httpClient->request($method,$this->getApiEndpoint().$relEndpoint,$options);
59
+    }
60
+
61
+    public function testApiRequest()
62
+    {
63
+        $options = [
64
+            'body' => json_encode([
65
+                'grant_type' => 'client_credentials',
66
+                'client_id' => $this->clientId,
67
+                'client_secret' => $this->clientSecret
68
+            ]),
69
+            'headers' => ['Content-Type: application/json', 'Accept: application/json'],
70
+        ];
71
+
72
+        $response = $this->sendRequest('oauth/token',$options,'POST');
73
+
74
+        if ($response->getStatusCode() == 200)
75
+        {
76
+            $content = $response->getContent();
77
+
78
+            return json_decode($content);
79
+        }
80
+
81
+        return null;
82
+    }
83
+}
0 84
new file mode 100644
... ...
@@ -0,0 +1,53 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of vonRotenberg Shopware API Bundle.
7
+ *
8
+ * (c) vonRotenberg
9
+ *
10
+ * @license proprietary
11
+ */
12
+
13
+namespace vonRotenberg\ShopwareApiBundle\Command;
14
+
15
+use Contao\CoreBundle\Framework\ContaoFramework;
16
+use Contao\System;
17
+use Symfony\Component\Console\Command\Command;
18
+use Symfony\Component\Console\Input\InputInterface;
19
+use Symfony\Component\Console\Output\OutputInterface;
20
+use vonRotenberg\ShopwareApiBundle\API\Shopware;
21
+
22
+class TestApiConnection extends Command
23
+{
24
+    protected static $defaultName = 'shopware:test-connection';
25
+
26
+    protected $framework;
27
+
28
+    public function __construct(ContaoFramework $framework)
29
+    {
30
+        $this->framework = $framework;
31
+
32
+        parent::__construct();
33
+    }
34
+
35
+    protected function configure(): void
36
+    {
37
+        $this
38
+            ->setName(self::$defaultName)
39
+            ->setDescription('Test connection')
40
+        ;
41
+    }
42
+    protected function execute(InputInterface $input, OutputInterface $output): int
43
+    {
44
+        $this->framework->initialize();
45
+
46
+        /** @var Shopware $Shopware */
47
+        $Shopware = System::getContainer()->get(Shopware::class);
48
+
49
+        dump($Shopware->testApiRequest());
50
+
51
+        return 0;
52
+    }
53
+}