Skip to content

Commit c34628f

Browse files
author
Marcel Hauri
committed
initial commit
0 parents  commit c34628f

28 files changed

+1154
-0
lines changed

Api/Data/MessageInterface.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @category Staempfli
4+
* @package Staempfli_ChatConnector
5+
* @copyright Copyright © Stämpfli AG. All rights reserved.
6+
7+
*/
8+
namespace Staempfli\ChatConnector\Api\Data;
9+
10+
/**
11+
* @api
12+
*/
13+
interface MessageInterface
14+
{
15+
/**
16+
* @param string $url
17+
* @return $this
18+
*/
19+
public function setUrl(string $url);
20+
21+
/**
22+
* @param string $contentType
23+
* @return $this
24+
*/
25+
public function setContentType(string $contentType);
26+
27+
/**
28+
* @param array $requestData
29+
* @return mixed
30+
*/
31+
public function setRequestData(array $requestData);
32+
33+
/**
34+
* @param array $messageData
35+
* @return $this
36+
*/
37+
public function setMessageData(array $messageData);
38+
39+
/**
40+
* @return array
41+
*/
42+
public function getRequestData();
43+
44+
/**
45+
* @return array
46+
*/
47+
public function getMessageData();
48+
}

Api/MessageManagementInterface.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @category Staempfli
4+
* @package Staempfli_ChatConnector
5+
* @copyright Copyright © Stämpfli AG. All rights reserved.
6+
7+
*/
8+
namespace Staempfli\ChatConnector\Api;
9+
10+
use Staempfli\ChatConnector\Api\Data\MessageInterface;
11+
12+
/**
13+
* @api
14+
*/
15+
interface MessageManagementInterface
16+
{
17+
/**
18+
* @param MessageInterface $message
19+
*/
20+
public function send(MessageInterface $message);
21+
}

Cron/ProcessQueuedMessages.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* @category Staempfli
4+
* @package Staempfli_ChatConnector
5+
* @copyright Copyright © Stämpfli AG. All rights reserved.
6+
7+
*/
8+
9+
namespace Staempfli\ChatConnector\Cron;
10+
11+
use Staempfli\ChatConnector\Model\MessageManagement;
12+
use Staempfli\ChatConnector\Model\Queue;
13+
14+
class ProcessQueuedMessages
15+
{
16+
const MESSAGES_LIMIT_PER_CRON_RUN = 45;
17+
18+
/**
19+
* @var Queue
20+
*/
21+
protected $queue;
22+
/**
23+
* @var MessageManagement
24+
*/
25+
protected $messageManagement;
26+
27+
/**
28+
* SendQueuedMessages constructor.
29+
* @param Queue $queue
30+
* @param MessageManagement $messageManagement
31+
*/
32+
public function __construct(
33+
Queue $queue,
34+
MessageManagement $messageManagement
35+
) {
36+
$this->queue = $queue;
37+
$this->messageManagement = $messageManagement;
38+
}
39+
40+
public function execute()
41+
{
42+
$collection = $this->queue->getCollection()
43+
->addOnlyForSendingFilter()
44+
->setPageSize(self::MESSAGES_LIMIT_PER_CRON_RUN)
45+
->setCurPage(1)
46+
->load();
47+
48+
foreach ($collection as $message) {
49+
$result = $this->messageManagement->sendRequest(
50+
$this->decodeData($message->getRequestData()),
51+
$this->decodeData($message->getMessageData())
52+
);
53+
if ($result) {
54+
$message->setProcessedAt(date('Y-m-d H:i:s'))->save();
55+
}
56+
// Slack rate limits, see: https://api.slack.com/docs/rate-limits
57+
sleep(1);
58+
}
59+
}
60+
61+
/**
62+
* @param string $data
63+
* @return array
64+
*/
65+
private function decodeData(string $data)
66+
{
67+
return json_decode($data, true);
68+
}
69+
}

Cron/RemoveProcessedMessages.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* @category Staempfli
4+
* @package Staempfli_ChatConnector
5+
* @copyright Copyright © Stämpfli AG. All rights reserved.
6+
7+
*/
8+
9+
namespace Staempfli\ChatConnector\Cron;
10+
11+
use Staempfli\ChatConnector\Model\Queue;
12+
13+
class RemoveProcessedMessages
14+
{
15+
/**
16+
* @var Queue
17+
*/
18+
private $queue;
19+
20+
/**
21+
* SendQueuedMessages constructor.
22+
* @param Queue $queue
23+
*/
24+
public function __construct(
25+
Queue $queue
26+
) {
27+
$this->queue = $queue;
28+
}
29+
30+
public function execute()
31+
{
32+
$this->queue->removeProcessedMessages();
33+
}
34+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* @category Staempfli
4+
* @package Staempfli_ChatConnector
5+
* @copyright Copyright © Stämpfli AG. All rights reserved.
6+
7+
*/
8+
9+
namespace Staempfli\ChatConnector\Events\Admin;
10+
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Staempfli\ChatConnector\Events\Events;
14+
15+
class AdminUserAuthenticate extends Events implements ObserverInterface
16+
{
17+
/**
18+
* @param Observer $observer
19+
* @return void
20+
*/
21+
public function execute(\Magento\Framework\Event\Observer $observer)
22+
{
23+
if (!$observer->getResult()) {
24+
$this->notify(__(
25+
"<strong>Admin user login failed!</strong>\n Username: %1 \n Password: %2",
26+
$observer->getUsername(),
27+
$observer->getPassword()
28+
));
29+
}
30+
}
31+
}

Events/Admin/AdminUserSave.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* @category Staempfli
4+
* @package Staempfli_ChatConnector
5+
* @copyright Copyright © Stämpfli AG. All rights reserved.
6+
7+
*/
8+
9+
namespace Staempfli\ChatConnector\Events\Admin;
10+
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Staempfli\ChatConnector\Events\Events;
14+
15+
class AdminUserSave extends Events implements ObserverInterface
16+
{
17+
/**
18+
* @param Observer $observer
19+
* @return void
20+
*/
21+
public function execute(\Magento\Framework\Event\Observer $observer)
22+
{
23+
if (!$observer->getResult()) {
24+
$adminUser = $observer->getData('object');
25+
if (!$adminUser->getCreated()) {
26+
$this->notify(__(
27+
"<strong>New Admin User was created!</strong>\n Username: %1 \n First Name: %2 \n Last Name: %3 \n E-Mail: %4",
28+
$adminUser->getUsername(),
29+
$adminUser->getFirtname(),
30+
$adminUser->getLastname(),
31+
$adminUser->getEmail()
32+
));
33+
}
34+
}
35+
}
36+
}

Events/Events.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Staempfli\ChatConnector\Events;
4+
5+
use Magento\Framework\Event\ManagerInterface;
6+
use Magento\Framework\App\Config\ScopeConfigInterface;
7+
use Staempfli\ChatConnector\Model\Config;
8+
9+
/**
10+
* @category Staempfli
11+
* @package Staempfli_ChatConnector
12+
* @copyright Copyright © Stämpfli AG. All rights reserved.
13+
14+
*/
15+
abstract class Events
16+
{
17+
/**
18+
* @var ManagerInterface
19+
*/
20+
private $eventManager;
21+
/**
22+
* @var Config
23+
*/
24+
private $config;
25+
26+
/**
27+
* Events constructor.
28+
* @param ManagerInterface $eventManager
29+
* @param Config $config
30+
*/
31+
public function __construct(
32+
ManagerInterface $eventManager,
33+
Config $config
34+
) {
35+
$this->eventManager = $eventManager;
36+
$this->config = $config;
37+
}
38+
39+
/**
40+
* @param string $message
41+
*/
42+
public function notify(string $message)
43+
{
44+
if ($this->config->isNotificationAllowed($this)) {
45+
$this->eventManager->dispatch(
46+
'chatconnector_notification',
47+
[
48+
'message' => $message,
49+
]
50+
);
51+
}
52+
}
53+
}

Events/Sales/SalesNewOrder.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* @category Staempfli
4+
* @package Staempfli_ChatConnector
5+
* @copyright Copyright © Stämpfli AG. All rights reserved.
6+
7+
*/
8+
9+
namespace Staempfli\ChatConnector\Events\Sales;
10+
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Staempfli\ChatConnector\Events\Events;
14+
15+
class SalesNewOrder extends Events implements ObserverInterface
16+
{
17+
/**
18+
* @param Observer $observer
19+
* @return void
20+
*/
21+
public function execute(\Magento\Framework\Event\Observer $observer)
22+
{
23+
$order = $observer->getOrder();
24+
$this->notify(__(
25+
"<strong>A new order has been placed.</strong>\n<strong>Order ID:</strong> %1\n<strong>Name:</strong> %2\n<strong>Subtotal:</strong> %3\n<strong>Shipping & Handling:</strong> %4\n <strong>Grand Total:</strong> %5",
26+
$order->getIncrementId(),
27+
$this->getCustomerName($order),
28+
$order->formatPrice($order->getBaseSubtotalInclTax()),
29+
$order->formatPrice($order->getBaseShippingInclTax()),
30+
$order->formatPrice($order->getBaseGrandTotal())
31+
));
32+
}
33+
34+
/**
35+
* @param $order
36+
* @return string
37+
*/
38+
private function getCustomerName($order)
39+
{
40+
if ($order->getCustomerFirstname()) {
41+
$customerName = $order->getCustomerFirstname() . ' ' . $order->getCustomerLastname();
42+
} else {
43+
$customerName = $order->getBillingAddress()->getFirstname() . ' ' . $order->getBillingAddress()->getLastname();
44+
}
45+
46+
if ($order->getCustomerIsGuest()) {
47+
$customerName = sprintf('%s (Guest)', $customerName);
48+
}
49+
50+
return $customerName;
51+
}
52+
}

0 commit comments

Comments
 (0)