22
33namespace Picqer \Shopware6Plugin \Subscriber ;
44
5+ use Exception ;
56use Picqer \Shopware6Plugin \Client \PicqerClient ;
6- use Picqer \Shopware6Plugin \Exception \InvalidConfigException ;
7+ use Picqer \Shopware6Plugin \Exception \IncompleteConfigurationException ;
8+ use Picqer \Shopware6Plugin \Exception \OrderNotFoundException ;
79use Picqer \Shopware6Plugin \Exception \RequestFailedException ;
810use Psr \Log \LoggerInterface ;
11+ use Shopware \Core \Checkout \Order \OrderEntity ;
912use Shopware \Core \Checkout \Order \OrderEvents ;
13+ use Shopware \Core \Framework \DataAbstractionLayer \EntityRepository ;
1014use Shopware \Core \Framework \DataAbstractionLayer \Event \EntityWrittenEvent ;
15+ use Shopware \Core \Framework \DataAbstractionLayer \Search \Criteria ;
1116use Shopware \Core \System \SystemConfig \SystemConfigService ;
1217use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
1318
1419final class EventSubscriber implements EventSubscriberInterface
1520{
21+ /**
22+ * @var SystemConfigService
23+ */
1624 private $ configService ;
25+
26+ /**
27+ * @var PicqerClient
28+ */
1729 private $ client ;
30+
31+ /**
32+ * @var EntityRepository
33+ */
34+ private $ orderRepository ;
35+
36+ /**
37+ * @var LoggerInterface
38+ */
1839 private $ logger ;
1940
20- public function __construct (SystemConfigService $ configService , PicqerClient $ client , LoggerInterface $ logger )
21- {
22- $ this ->client = $ client ;
41+ public function __construct (
42+ SystemConfigService $ configService ,
43+ PicqerClient $ client ,
44+ EntityRepository $ orderRepository ,
45+ LoggerInterface $ logger
46+ ) {
2347 $ this ->configService = $ configService ;
48+ $ this ->client = $ client ;
49+ $ this ->orderRepository = $ orderRepository ;
2450 $ this ->logger = $ logger ;
2551 }
2652
2753 public static function getSubscribedEvents (): array
2854 {
2955 return [
30- OrderEvents::ORDER_WRITTEN_EVENT => 'pushOrder ' ,
56+ OrderEvents::ORDER_WRITTEN_EVENT => 'handle ' ,
3157 ];
3258 }
3359
34- public function pushOrder (EntityWrittenEvent $ event ): void
60+ public function handle (EntityWrittenEvent $ event ): void
3561 {
36- if (!isset ($ event ->getIds ()[0 ])) {
62+ if (! isset ($ event ->getIds ()[0 ])) {
3763 return ;
3864 }
3965
40- $ id = $ event ->getIds ()[0 ];
66+ $ orderId = $ event ->getIds ()[0 ];
4167
4268 try {
43- $ this ->client ->pushOrder (
44- $ this ->getConfigurationValue ('subdomain ' ),
45- $ this ->getConfigurationValue ('connectionkey ' ),
46- $ id
47- );
48- } catch (InvalidConfigException $ e ) {
49- /*
50- * It is possible that the configuration values are not yet set
51- * due to the fact that a plugin needs to be installed and activated before you can configure these.
52- *
53- * Picqer will check the webshop on interval as well.
54- * When a call fails there might be a delay in orders showing up in Picqer, but they will not get lost.
55- *
56- * We choose to silently fail to prevent any disturbances of the webshop.
57- */
58-
59- $ this ->logger ->info ('[Picqer] Plugin configuration is invalid ' , [
60- 'message ' => $ e ->getMessage (),
61- ]);
62- } catch (RequestFailedException $ e ) {
63- $ this ->logger ->info ('[Picqer] Could not call webhook ' , [
69+ $ order = $ this ->orderRepository ->search (new Criteria ([$ orderId ]), $ event ->getContext ())->first ();
70+ if (! $ order instanceof OrderEntity) {
71+ throw new OrderNotFoundException (sprintf ('Order [%s] not found ' , $ orderId ));
72+ }
73+
74+ $ salesChannelId = $ order ->getSalesChannelId ();
75+ $ disabled = $ this ->configService ->getBool ($ this ->buildConfigKey ('disabled ' ), $ salesChannelId );
76+ if ($ disabled ) {
77+ return ;
78+ }
79+
80+ $ subdomain = $ this ->configService ->getString ($ this ->buildConfigKey ('subdomain ' ), $ salesChannelId );
81+ $ connectionKey = $ this ->configService ->getString ($ this ->buildConfigKey ('connectionkey ' ), $ salesChannelId );
82+ $ debug = $ this ->configService ->getBool ($ this ->buildConfigKey ('debug ' ), $ salesChannelId );
83+
84+ $ this ->pushOrderToPicqer ($ subdomain , $ connectionKey , $ order , $ debug , $ salesChannelId );
85+ } catch (Exception $ e ) {
86+ $ this ->logger ->error ('[Picqer] Caught unexpected exception ' , [
87+ 'exception ' => get_class ($ e ),
6488 'message ' => $ e ->getMessage (),
89+ 'orderId ' => $ orderId ,
6590 ]);
6691 }
6792 }
6893
69- private function getConfigurationValue (string $ type ): string
94+ private function buildConfigKey (string $ key ): string
7095 {
71- $ value = $ this ->configService ->get (sprintf ('PicqerExtendedIntegration.config.%s ' , $ type ));
72- if (!is_string ($ value )) {
73- throw new InvalidConfigException (sprintf ('%s not set ' , ucfirst ($ type )));
74- }
96+ return sprintf ('PicqerExtendedIntegration.config.%s ' , $ key );
97+ }
98+
99+ private function pushOrderToPicqer (
100+ string $ subdomain ,
101+ string $ connectionKey ,
102+ OrderEntity $ order ,
103+ bool $ debug ,
104+ string $ salesChannelId
105+ ): void {
106+ try {
107+ if (empty ($ subdomain ) || empty ($ connectionKey )) {
108+ throw new IncompleteConfigurationException ($ subdomain , $ connectionKey );
109+ }
110+
111+ $ this ->client ->pushOrder (
112+ $ subdomain ,
113+ $ connectionKey ,
114+ $ order ->getId ()
115+ );
116+ } catch (IncompleteConfigurationException $ e ) {
117+ if (! $ debug ) {
118+ return ;
119+ }
120+
121+ $ this ->logger ->error ('[Picqer] Subdomain and/or connection-key not configured ' , [
122+ 'subdomain ' => $ e ->getSubdomain (),
123+ 'connectionKey ' => $ e ->getConnectionKey (),
124+ 'orderId ' => $ order ->getId (),
125+ 'salesChannelId ' => $ salesChannelId ,
126+ ]);
127+ } catch (RequestFailedException $ e ) {
128+ if (! $ debug ) {
129+ return ;
130+ }
75131
76- return $ value ;
132+ $ this ->logger ->error ('[Picqer] Could not call webhook ' , [
133+ 'endpoint ' => $ e ->getEndpoint (),
134+ 'message ' => $ e ->getMessage (),
135+ 'orderId ' => $ order ->getId (),
136+ 'salesChannelId ' => $ salesChannelId ,
137+ ]);
138+ }
77139 }
78140}
0 commit comments