|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Koality\ShopwarePlugin\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Koality\ShopwarePlugin\Collector\CollectorContainer; |
|
6
|
|
|
use Koality\ShopwarePlugin\Exception\ForbiddenException; |
|
7
|
|
|
use Koality\ShopwarePlugin\Formatter\KoalityFormatter; |
|
8
|
|
|
use Koality\ShopwarePlugin\KoalityShopwarePlugin; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Shopware\Core\Framework\Context; |
|
11
|
|
|
use Shopware\Core\Framework\Routing\Annotation\RouteScope; |
|
12
|
|
|
use Shopware\Core\System\SystemConfig\SystemConfigService; |
|
13
|
|
|
use Shopware\Storefront\Controller\StorefrontController; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class HealthApiController |
|
21
|
|
|
* |
|
22
|
|
|
* @package Koality\ShopwarePlugin\Controller |
|
23
|
|
|
* |
|
24
|
|
|
* @author Nils Langner <[email protected]> |
|
25
|
|
|
* created 2020-12-28 |
|
26
|
|
|
* |
|
27
|
|
|
* @RouteScope(scopes={"storefront"}) |
|
28
|
|
|
*/ |
|
29
|
|
|
class HealthApiController extends StorefrontController |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Get the health status of the online shop. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Request $request |
|
35
|
|
|
* @param Context $context |
|
36
|
|
|
* |
|
37
|
|
|
* @return JsonResponse |
|
38
|
|
|
* |
|
39
|
|
|
* @RouteScope(scopes={"storefront"}) |
|
40
|
|
|
* @Route("_koality/sales/metrics/{apiKey}", name="koality.sales.metrics", methods={"GET"}, defaults={"csrf_protected"=false, "XmlHttpRequest"=true}) |
|
41
|
|
|
*/ |
|
42
|
|
|
public function healthSalesApi(Request $request, Context $context): JsonResponse |
|
43
|
|
|
{ |
|
44
|
|
|
$currentApiKey = $request->get('apiKey'); |
|
45
|
|
|
|
|
46
|
|
|
if (is_null($currentApiKey)) { |
|
47
|
|
|
return new JsonResponse(['status' => 'failure', 'message' => 'API key is missing. Please run the install routine again.'], Response::HTTP_INTERNAL_SERVER_ERROR); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$pluginConfig = $this->getPluginConfig($currentApiKey); |
|
52
|
|
|
} catch (ForbiddenException $e) { |
|
53
|
|
|
return new JsonResponse(['status' => 'failure', 'message' => $e->getMessage()], Response::HTTP_UNAUTHORIZED); |
|
54
|
|
|
} catch (\Exception $e) { |
|
55
|
|
|
return new JsonResponse(['status' => 'failure', 'message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$formatter = $this->collectResults($pluginConfig, $context); |
|
59
|
|
|
|
|
60
|
|
|
$response = new JsonResponse($formatter->getFormattedResults()); |
|
61
|
|
|
|
|
62
|
|
|
$response->setEncodingOptions($response->getEncodingOptions() | JSON_PRETTY_PRINT); |
|
63
|
|
|
|
|
64
|
|
|
return $response; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the plugin version. |
|
69
|
|
|
* |
|
70
|
|
|
* @return JsonResponse |
|
71
|
|
|
* |
|
72
|
|
|
* @RouteScope(scopes={"storefront"}) |
|
73
|
|
|
* @Route("_koality/version", name="koality.version", methods={"GET"}, defaults={"csrf_protected"=false, "XmlHttpRequest"=true}) |
|
74
|
|
|
*/ |
|
75
|
|
|
public function healthSalesApiVersion(): JsonResponse |
|
76
|
|
|
{ |
|
77
|
|
|
$response = new JsonResponse(['version' => KoalityShopwarePlugin::VERSION]); |
|
78
|
|
|
$response->setEncodingOptions($response->getEncodingOptions() | JSON_PRETTY_PRINT); |
|
79
|
|
|
return $response; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Collect all health results. |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $pluginConfig |
|
86
|
|
|
* @param Context $context |
|
87
|
|
|
* |
|
88
|
|
|
* @return KoalityFormatter |
|
89
|
|
|
*/ |
|
90
|
|
|
private function collectResults(array $pluginConfig, Context $context): KoalityFormatter |
|
91
|
|
|
{ |
|
92
|
|
|
$collectorContainer = $this->get(CollectorContainer::class); |
|
93
|
|
|
$collectorContainer->init($pluginConfig, $context); |
|
94
|
|
|
return $collectorContainer->run(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the plugin configuration. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $currentApiKey |
|
101
|
|
|
* |
|
102
|
|
|
* @return string[] |
|
103
|
|
|
* |
|
104
|
|
|
* @throws ForbiddenException |
|
105
|
|
|
* @throws RuntimeException |
|
106
|
|
|
*/ |
|
107
|
|
|
private function getPluginConfig(string $currentApiKey): array |
|
108
|
|
|
{ |
|
109
|
|
|
/** @var SystemConfigService $configService */ |
|
110
|
|
|
$configService = $this->get(SystemConfigService::class); |
|
111
|
|
|
|
|
112
|
|
|
$pluginConfigArray = $configService->get(KoalityShopwarePlugin::PLUGIN_NAME); |
|
113
|
|
|
|
|
114
|
|
|
if (!is_array($pluginConfigArray) || !array_key_exists('config', $pluginConfigArray)) { |
|
115
|
|
|
throw new RuntimeException('The plugin is not configured yet. Please run the configuration first.'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$pluginConfig = $pluginConfigArray['config']; |
|
119
|
|
|
|
|
120
|
|
|
if ($currentApiKey !== $pluginConfig[KoalityShopwarePlugin::CONFIG_KEY_API_KEY]) { |
|
121
|
|
|
throw new ForbiddenException('The API key is not valid.'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $pluginConfig; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|