slackWebhookUrl = $slackWebhookUrl; $this->productRepository = $productRepository; $this->stockRepository = $stockRepository; $this->warehouseRepository = $warehouseRepository; } public static function getSubscribedEvents(): array { return [ KernelEvents::VIEW => ['sendSlack', EventPriorities::POST_WRITE], ]; } /** * @param ViewEvent $event * @return void */ public function sendSlack(ViewEvent $event): void { $order = $event->getControllerResult(); $method = $event->getRequest()->getMethod(); //wenn es keine Bestellung ist oder es kein POST Request ist, dann return if (!$order instanceof Order || Request::METHOD_POST !== $method) { return; } foreach ($order->getData()[0]['positions'] as $item) { if ($item['menge'] > 0) { $warehouse = $this->getWarehouseByGtin($item['gtin']); $slack = new Client($this->slackWebhookUrl, [ 'username' => 'CDS-Notify', 'channel' => '#online_verkäufe' . '_' . $warehouse, 'link_names' => true, 'icon' => ':robot_face:', 'allow_markdown' => true, ]); $msg = "Bestellung {$order->getOrderId()}: \n"; $msg .= $item['marke']; $msg .= $item['name']; $msg .= ' ' . $item['han']; $msg .= ' *Menge: ' . $item['menge'] . "* \n"; if ($order->getStatus() == 1) { $slack->from('CDS-Notify')->send($msg); } } } } /** * @param string $gtin * @return string|null */ private function getWarehouseByGtin(string $gtin) { if ($gtin == null) { return false; } $product = $this->productRepository->findOneBy(['gtin' => $gtin]); if ($product) { $stock = $this->stockRepository->findOneBy(['product_id' => $product->getId()]); if ($stock) { $warehouse = $this->warehouseRepository->findOneBy(['id' => $stock->getWarehouse()->getId()]); return $warehouse->getName(); } } return null; } }