22
33namespace Picqer \Shopware6Plugin \Subscriber ;
44
5+ use Exception ;
56use Picqer \Shopware6Plugin \Client \PicqerClient ;
67use Picqer \Shopware6Plugin \Exception \IncompleteConfigurationException ;
8+ use Picqer \Shopware6Plugin \Exception \OrderNotFoundException ;
79use Picqer \Shopware6Plugin \Exception \RequestFailedException ;
810use Psr \Log \LoggerInterface ;
911use Shopware \Core \Checkout \Order \OrderEntity ;
@@ -51,32 +53,56 @@ public function __construct(
5153 public static function getSubscribedEvents (): array
5254 {
5355 return [
54- OrderEvents::ORDER_WRITTEN_EVENT => 'pushOrder ' ,
56+ OrderEvents::ORDER_WRITTEN_EVENT => 'handle ' ,
5557 ];
5658 }
5759
58- public function pushOrder (EntityWrittenEvent $ event ): void
60+ public function handle (EntityWrittenEvent $ event ): void
5961 {
6062 if (! isset ($ event ->getIds ()[0 ])) {
6163 return ;
6264 }
6365
64- $ order = $ this ->orderRepository ->search (new Criteria ([$ event ->getIds ()[0 ]]), $ event ->getContext ())->first ();
65- if (! $ order instanceof OrderEntity) {
66- return ;
67- }
66+ $ orderId = $ event ->getIds ()[0 ];
6867
69- $ salesChannelId = $ order ->getSalesChannelId ();
68+ try {
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+ }
7073
71- $ disabled = $ this ->configService ->getBool ($ this ->buildConfigKey ('disabled ' ), $ salesChannelId );
72- if ($ disabled ) {
73- return ;
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 ),
88+ 'message ' => $ e ->getMessage (),
89+ 'orderId ' => $ orderId ,
90+ ]);
7491 }
92+ }
7593
76- $ subdomain = $ this ->configService ->getString ($ this ->buildConfigKey ('subdomain ' ), $ salesChannelId );
77- $ connectionKey = $ this ->configService ->getString ($ this ->buildConfigKey ('connectionkey ' ), $ salesChannelId );
78- $ debug = $ this ->configService ->getBool ($ this ->buildConfigKey ('debug ' ), $ salesChannelId );
94+ private function buildConfigKey (string $ key ): string
95+ {
96+ return sprintf ('PicqerExtendedIntegration.config.%s ' , $ key );
97+ }
7998
99+ private function pushOrderToPicqer (
100+ string $ subdomain ,
101+ string $ connectionKey ,
102+ OrderEntity $ order ,
103+ bool $ debug ,
104+ string $ salesChannelId
105+ ): void {
80106 try {
81107 if (empty ($ subdomain ) || empty ($ connectionKey )) {
82108 throw new IncompleteConfigurationException ($ subdomain , $ connectionKey );
@@ -111,9 +137,4 @@ public function pushOrder(EntityWrittenEvent $event): void
111137 ]);
112138 }
113139 }
114-
115- private function buildConfigKey (string $ key ): string
116- {
117- return sprintf ('PicqerExtendedIntegration.config.%s ' , $ key );
118- }
119140}
0 commit comments