Browse code

Implement API authentication and replace test api command to show Shopware status

Benjamin Roth authored on20/10/2024 22:11:52
Showing4 changed files
... ...
@@ -22,7 +22,8 @@
22 22
         "contao/core-bundle": "^4.13 || ^5.3",
23 23
         "symfony/config": "^5.4 || ^6.4",
24 24
         "symfony/dependency-injection": "^5.4 || ^6.4",
25
-        "symfony/http-kernel": "^5.4 || ^6.4"
25
+        "symfony/http-kernel": "^5.4 || ^6.4",
26
+        "symfony/serializer": "^5.4 || ^6.4"
26 27
     },
27 28
     "require-dev": {
28 29
         "bamarni/composer-bin-plugin": "^1.5",
... ...
@@ -13,17 +13,23 @@ declare(strict_types=1);
13 13
 namespace vonRotenberg\ShopwareApiBundle\API;
14 14
 
15 15
 use Contao\System;
16
+use Symfony\Component\Serializer\SerializerInterface;
16 17
 use Symfony\Contracts\HttpClient\HttpClientInterface;
17 18
 
18 19
 class Shopware
19 20
 {
21
+    protected $serializer;
20 22
     protected $httpClient;
21 23
     protected $clientId;
22 24
     protected $clientSecret;
23 25
     protected $apiEndpoint;
24 26
 
25
-    public function __construct(HttpClientInterface $httpClient)
27
+    private $token;
28
+    private $tokenType;
29
+
30
+    public function __construct(SerializerInterface $serializer, HttpClientInterface $httpClient)
26 31
     {
32
+        $this->serializer = $serializer;
27 33
         $this->httpClient = $httpClient;
28 34
 
29 35
         $this->clientId = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.client_id');
... ...
@@ -58,7 +64,7 @@ class Shopware
58 64
         return $this->httpClient->request($method,$this->getApiEndpoint().$relEndpoint,$options);
59 65
     }
60 66
 
61
-    public function testApiRequest()
67
+    protected function getAccessToken()
62 68
     {
63 69
         $options = [
64 70
             'body' => json_encode([
... ...
@@ -80,4 +86,34 @@ class Shopware
80 86
 
81 87
         return null;
82 88
     }
89
+
90
+    protected function getAuthentication()
91
+    {
92
+        if (($Token = $this->getAccessToken()) !== null)
93
+        {
94
+            return 'Authorization: ' . $Token->token_type . ' ' . $Token->access_token;
95
+        }
96
+
97
+        return null;
98
+    }
99
+
100
+    public function isShopwareRunning()
101
+    {
102
+        $options = [
103
+            'headers' => [
104
+                $this->getAuthentication(),
105
+                'Content-Type: application/json',
106
+                'Accept: application/json'
107
+            ],
108
+        ];
109
+
110
+        $response = $this->sendRequest('_info/health-check',$options,'GET');
111
+
112
+        if ($response->getStatusCode() == 200)
113
+        {
114
+            return true;
115
+        }
116
+
117
+        return false;
118
+    }
83 119
 }
84 120
new file mode 100644
... ...
@@ -0,0 +1,62 @@
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 Symfony\Component\Console\Style\SymfonyStyle;
21
+use vonRotenberg\ShopwareApiBundle\API\Shopware;
22
+
23
+class ShopwareSystemStatus extends Command
24
+{
25
+    protected static $defaultName = 'shopware:system-status';
26
+
27
+    protected $framework;
28
+
29
+    public function __construct(ContaoFramework $framework)
30
+    {
31
+        $this->framework = $framework;
32
+
33
+        parent::__construct();
34
+    }
35
+
36
+    protected function configure(): void
37
+    {
38
+        $this
39
+            ->setName(self::$defaultName)
40
+            ->setDescription('Show Shopware status')
41
+        ;
42
+    }
43
+    protected function execute(InputInterface $input, OutputInterface $output): int
44
+    {
45
+        $this->framework->initialize();
46
+
47
+        $io = new SymfonyStyle($input, $output);
48
+        $io->title('Shopware system status');
49
+
50
+        /** @var Shopware $Shopware */
51
+        $Shopware = System::getContainer()->get(Shopware::class);
52
+
53
+        if ($Shopware->isShopwareRunning())
54
+        {
55
+            $io->success('Shopware system is running');
56
+        } else {
57
+            $io->error('Shopware system is not running properly');
58
+        }
59
+
60
+        return 0;
61
+    }
62
+}
0 63
deleted file mode 100644
... ...
@@ -1,53 +0,0 @@
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
-}