Skip to content

Instantly share code, notes, and snippets.

@incrize
Created October 21, 2019 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save incrize/52af853dabfefe7b811344af9bf37b84 to your computer and use it in GitHub Desktop.
Save incrize/52af853dabfefe7b811344af9bf37b84 to your computer and use it in GitHub Desktop.
diff --git a/app/Tygh/Notifications/EventDispatcher.php b/app/Tygh/Notifications/EventDispatcher.php
index f0e9bcfbdc..8097942c9d 100644
--- a/app/Tygh/Notifications/EventDispatcher.php
+++ b/app/Tygh/Notifications/EventDispatcher.php
@@ -62,22 +62,22 @@ class EventDispatcher
}
/**
- * @param string $event_id
- * @param mixed ...$data
+ * @param string $event_id
+ * @param array $data
+ * @param IProvider $id_provider
*
* @throws \Tygh\Exceptions\DeveloperException
*/
- public function dispatch($event_id, ...$data)
+ public function dispatch($event_id, array $data, IProvider $id_provider = null)
{
if (!isset($this->events_schema[$event_id])) {
return;
}
- $id_provider = $this->extractEventIdProvider($data);
$notification_settings = $this->extractEventNotificationSettings($event_id, $data);
foreach ($this->events_schema[$event_id]['receivers'] as $receiver => $transports) {
- foreach ($transports as $transport_id => $message_provider) {
+ foreach ($transports as $transport_id => $schema) {
if (empty($notification_settings['receivers'][$receiver][$transport_id])) {
continue;
}
@@ -89,13 +89,8 @@ class EventDispatcher
$this->saveDispatched($id_provider->getId(), $transport_id, $receiver);
}
- $message = call_user_func_array($message_provider, $data);
- if (!$message instanceof IMessage) {
- throw new DeveloperException();
- }
-
$transport = $this->transport_factory->create($transport_id);
- $transport->process($message);
+ $transport->process2($data, $schema);
}
}
}
diff --git a/app/Tygh/Notifications/Messages/DataValue.php b/app/Tygh/Notifications/Messages/DataValue.php
new file mode 100644
index 0000000000..7e3e265dd5
--- /dev/null
+++ b/app/Tygh/Notifications/Messages/DataValue.php
@@ -0,0 +1,37 @@
+<?php
+
+
+namespace Tygh\Notifications\Messages;
+
+
+use Tygh\Registry;
+
+class DataValue
+{
+ protected $field;
+
+ protected $default_value;
+
+ public function __construct($field, $default_value = null)
+ {
+ $this->field = $field;
+ $this->default_value = $default_value;
+ }
+
+ public static function create($field, $default_value = null)
+ {
+ return new self($field, $default_value);
+ }
+
+
+ public function get(array $data)
+ {
+ Registry::set('runtime.data_value', $data);
+
+ $value = Registry::ifGet('runtime.data_value.' . $this->field, $this->default_value);
+
+ Registry::del('runtime.data_value');
+
+ return $value;
+ }
+}
\ No newline at end of file
diff --git a/app/Tygh/Notifications/Messages/MailMessage.php b/app/Tygh/Notifications/Messages/MailMessage.php
index 4ff6bdeeaf..c2d96b9535 100644
--- a/app/Tygh/Notifications/Messages/MailMessage.php
+++ b/app/Tygh/Notifications/Messages/MailMessage.php
@@ -19,7 +19,7 @@ namespace Tygh\Notifications\Messages;
*
* @package Tygh\Notifications\Messages
*/
-abstract class MailMessage implements IMessage
+class MailMessage implements IMessage
{
/**
* @var array|string
@@ -137,4 +137,11 @@ abstract class MailMessage implements IMessage
{
return $this->area;
}
+
+ public static function createFromArray(array $message)
+ {
+ //TODO
+
+ return new self();
+ }
}
diff --git a/app/Tygh/Notifications/Transports/MailTransport.php b/app/Tygh/Notifications/Transports/MailTransport.php
index d60f056100..795d7c5e3e 100644
--- a/app/Tygh/Notifications/Transports/MailTransport.php
+++ b/app/Tygh/Notifications/Transports/MailTransport.php
@@ -15,6 +15,8 @@
namespace Tygh\Notifications\Transports;
use Tygh\Mailer\Mailer;
+use Tygh\Notifications\Messages\DataValue;
+use Tygh\Notifications\Messages\MailMessage;
/**
* Class MailTransport implements a transport that send emails based on an event message.
@@ -55,4 +57,30 @@ class MailTransport implements ITransport
'company_id' => $message->getCompanyId(),
], $message->getArea(), $message->getLanguageCode());
}
+
+ public function process2(array $data, array $schema)
+ {
+ if (isset($schema['data']) && is_callable($schema['data'])) {
+ $data = call_user_func($schema['data'], $data);
+ }
+
+ foreach ($schema as $key => &$item) {
+ if ($item instanceof DataValue) {
+ $item = $item->get($data);
+ }
+ }
+ unset($item);
+
+ $message = MailMessage::createFromArray($data);
+
+ return $this->mailer->send([
+ 'to' => $message->getTo(),
+ 'from' => $message->getFrom(),
+ 'reply_to' => $message->getReplyTo(),
+ 'data' => $message->getData(),
+ 'template_code' => $message->getTemplateCode(),
+ 'tpl' => $message->getLegacyTemplate(),
+ 'company_id' => $message->getCompanyId(),
+ ], $message->getArea(), $message->getLanguageCode());
+ }
}
diff --git a/app/schemas/notifications/events.php b/app/schemas/notifications/events.php
index 5d28a3be23..8c67ce9130 100644
--- a/app/schemas/notifications/events.php
+++ b/app/schemas/notifications/events.php
@@ -13,6 +13,7 @@
****************************************************************************/
use Tygh\Enum\UserTypes;
+use Tygh\Notifications\Messages\DataValue;
use Tygh\Notifications\Messages\Order\EdpMailMessage;
use Tygh\Notifications\Messages\Order\OrderAdminMailMessage;
use Tygh\Notifications\Messages\Order\OrderCustomerMailMessage;
@@ -459,4 +460,28 @@ if (fn_allowed_for('MULTIVENDOR')) {
}
+$schema['profie.otp_request'] = [
+ 'group' => 'profile',
+ 'name' => [
+ 'template' => 'event.profile.otp_request.name',
+ ],
+ 'receivers' => [
+ UserTypes::CUSTOMER => [
+ MailTransport::getId() => [
+ 'from' => 'default_user_departament',
+ 'to' => DataValue::create('user.email'),
+ 'template_code' => 'otp_request',
+ 'legacy_template' => 'profile/otp_request.tpl',
+ 'area' => 'C',
+ 'lang_code' => DataValue::create('user.lang_code', CART_LANGUAGE),
+ 'company_id' => DataValue::create('user.company_id', 0),
+ 'data' => function ($data) {
+ //TODO get some data
+ return $data;
+ }
+ ]
+ ]
+ ]
+];
+
return $schema;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment