Slack notify

This commit is contained in:
Marko
2023-06-14 17:02:24 +02:00
parent 0579a62931
commit 30a8e73f21
33 changed files with 1334 additions and 1137 deletions

View File

@@ -0,0 +1,82 @@
<?php
namespace App\Command;
use App\Helper\HiltesImport;
use App\Helper\Jtl;
use App\Repository\ProductRepository;
use App\Repository\StockRepository;
use App\Repository\WarehouseRepository;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'jtl:export',
description: 'Create a export file for JTL Ameise',
)]
class JtlExportCommand extends Command
{
private $stockRepository;
private $warehouseRepository;
private $productRepository;
private $logger;
public function __construct(
ProductRepository $productRepository,
StockRepository $stockRepository,
WarehouseRepository $warehouseRepository,
LoggerInterface $logger
)
{
$this->productRepository = $productRepository;
$this->stockRepository = $stockRepository;
$this->warehouseRepository = $warehouseRepository;
$this->logger = $logger;
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
// $arg1 = $input->getArgument('arg1');
//
// if ($arg1) {
// $io->note(sprintf('You passed an argument: %s', $arg1));
// }
//
// if ($input->getOption('option1')) {
// // ...
// }
$io = new SymfonyStyle($input, $output);
$io->success('Start JTL Export');
/**
* @var HiltesImport
*/
$jtl = new Jtl($this->productRepository,$this->warehouseRepository,$this->stockRepository,$this->logger);
$data = $jtl->getProducts();
$jtl->createExportFile($data);
$io->success('Ende JTL Export');
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Notifier\Chatter;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\ChatterInterface;
use Symfony\Component\Notifier\Transport;
#[AsCommand(
name: 'jtl:import',
description: 'Add a short description for your command',
)]
class JtlImportCommand extends Command
{
protected function configure(): void
{
$this
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$arg1 = $input->getArgument('arg1');
if ($arg1) {
$io->note(sprintf('You passed an argument: %s', $arg1));
}
if ($input->getOption('option1')) {
// ...
}
$chatter = new Chatter(new Transport('xoxb-1234-56789abcdefghijklmnopqrstuv'));
$message = (new ChatMessage('You got a new invoice for 15 EUR.'))
// if not set explicitly, the message is sent to the
// default transport (the first one configured)
->transport('slack');
$sentMessage = $chatter->send($message);
$io->success('You have a new command! Now make it your own! Pass --help to see your options.');
return Command::SUCCESS;
}
}

View File

@@ -3,7 +3,7 @@
namespace App\Command;
use App\Controller\ShopwareController;
use App\Entity\Orders;
use App\Entity\Order;
use App\Helper\Shopware;
use App\Repository\OrdersRepository;
@@ -32,17 +32,17 @@ use Vin\ShopwareSdk\Factory\RepositoryFactory;
)]
class SwGetOrdersCommand extends Command
{
private $ordersRepository;
private $orderRepository;
private $logger;
private $sw;
private $orderData;
public function __construct(OrdersRepository $ordersRepository, LoggerInterface $logger)
public function __construct(OrdersRepository $orderRepository, LoggerInterface $logger)
{
$this->ordersRepository = $ordersRepository;
$this->orderRepository = $orderRepository;
$this->logger = $logger;
$this->sw = new Shopware();
$this->sw = new Shopware($logger);
parent::__construct();
}
@@ -82,7 +82,7 @@ class SwGetOrdersCommand extends Command
*/
private function getOrders():array
{
return $this->ordersRepository->findAll();
return $this->orderRepository->findAll();
}
/**
@@ -121,4 +121,4 @@ class SwGetOrdersCommand extends Command
$orderData->setStatus = 1;
$this->ordersRepository->add($orderData, true);
}
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\ProductRepository;
use App\Repository\StockRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
@@ -93,4 +94,11 @@ class Product
return $this;
}
}
public function setStock(Stock $stock): self
{
$this->stock = $stock;
return $this;
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\EventSubscriber;
use ApiPlatform\Symfony\EventListener\EventPriorities;
use App\Helper\Slack;
use App\Entity\Order;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Notifier\ChatterInterface;
class SlackNotifySubscriber implements EventSubscriberInterface
{
/**
* @var Slack
*/
public $slack;
public function __construct(ChatterInterface $chatter)
{
$this->slack = new Slack($chatter);
}
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;
}
$msg = "Order {$order->getId()} has been created: {$order->getStatus()}";
$this->slack->sendMessage($msg);
}
}

85
src/Helper/Jtl.php Normal file
View File

@@ -0,0 +1,85 @@
<?php
namespace App\Helper;
use App\Repository\ProductRepository;
use App\Repository\StockRepository;
use App\Repository\WarehouseRepository;
use Psr\Log\LoggerInterface;
use League\Csv\Writer;
class Jtl
{
private $productRepository;
private $stockRepository;
private $warehouseRepository;
private $logger;
public function __construct(ProductRepository $productRepository,WarehouseRepository $warehouseRepository,StockRepository $stockRepository, LoggerInterface $logger)
{
$this->productRepository = $productRepository;
$this->warehouseRepository = $warehouseRepository;
$this->stockRepository = $stockRepository;
$this->logger = $logger;
}
/**
* Holt alle Produkte und deren Lagerbestände
* @return array
*/
public function getProducts(): array
{
$r = $this->productRepository->findAll();
$data = array();
foreach($r as $product){
$stock = $this->stockRepository->findBy(['product_id'=>$product->getId()]);
if($stock){
foreach($stock as $s){
$warehouse = $s->getWarehouse();
$warehouseId = $warehouse->getId();
$data[$product->getId().$warehouseId] = [
'gtin' => $product->getGtin(),
'stock' => $s->getInstock(),
'warehouse' => $warehouseId
];
}
}else{
$data[$product->getId()] = [
'gtin' => $product->getGtin(),
'stock' => 0,
'warehouse' => 0
];
$this->logger->info('No stock for product '.$product->getId());
}
// dump($data);
}
return $data;
}
/**
* @param $data
* @return void
*/
public function createExportFile($data): void
{
try {
$writer = Writer::createFromPath('./jtl/cds-export.csv', 'x');
$writer->insertAll(new \ArrayIterator($data));
}catch (\Exception $e){
$this->logger->error($e->getMessage());
}
}
}

32
src/Helper/Slack.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace App\Helper;
use Symfony\Component\Notifier\Chatter;
use Symfony\Component\Notifier\ChatterInterface;
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
use Symfony\Component\Notifier\Message\ChatMessage;
class Slack
{
private $chatter;
public function __construct(ChatterInterface $chatter)
{
$this->chatter = $chatter;
}
public function sendMessage(String $message): void
{
$message = (new ChatMessage($message))
->transport('slack');
try {
$sentMessage = $this->chatter->send($message);
dump($sentMessage);
} catch (TransportExceptionInterface $e) {
}
}
}