diff --git a/config/schema/sms.data_types.schema.yml b/config/schema/sms.data_types.schema.yml index 0135564..5e63182 100644 --- a/config/schema/sms.data_types.schema.yml +++ b/config/schema/sms.data_types.schema.yml @@ -1,7 +1,4 @@ sms_gateway_settings: type: mapping label: 'SMS gateway settings' - mapping: - gateway_name: - type: string - label: 'Gateway Name' + mapping: [] diff --git a/sms.routing.yml b/sms.routing.yml index fed183a..40bd970 100644 --- a/sms.routing.yml +++ b/sms.routing.yml @@ -98,7 +98,7 @@ sms.bootstrap_admin: _permission: 'administer smsframework' sms.delivery_report: - path: '/sms/deliveryreport/{gateway}' + path: '/sms/deliveryreport/{gateway_name}' defaults: _controller: '\Drupal\sms\DeliveryReportController::acknowledgeDelivery' requirements: diff --git a/src/DeliveryReportController.php b/src/DeliveryReportController.php index 8ccd7b1..df85b7f 100644 --- a/src/DeliveryReportController.php +++ b/src/DeliveryReportController.php @@ -71,12 +71,12 @@ class DeliveryReportController implements ContainerInjectionInterface { * * @return \Symfony\Component\HttpFoundation\Response */ - public function acknowledgeDelivery($gateway) { + public function acknowledgeDelivery($gateway_name) { // The response that will be sent back to the server API. The gateway plugin // can alter this response as needed. $response = new Response(''); /** @var \Drupal\sms\Entity\SmsGateway $sms_gateway */ - $sms_gateway = SmsGateway::load($gateway); + $sms_gateway = SmsGateway::load($gateway_name); $reports = $sms_gateway ->getPlugin() ->parseDeliveryReports($this->requestStack->getCurrentRequest(), $response); diff --git a/src/Entity/SmsGateway.php b/src/Entity/SmsGateway.php index a98cf4f..da2edc1 100644 --- a/src/Entity/SmsGateway.php +++ b/src/Entity/SmsGateway.php @@ -92,8 +92,9 @@ class SmsGateway extends ConfigEntityBase implements SmsGatewayInterface, Entity $this->pluginCollection = new SmsGatewayPluginCollection( \Drupal::service('plugin.manager.sms_gateway'), $this->plugin, + $this->id, // Add the configuration name so the plugin is aware of it. - $this->settings + ['gateway_name' => $this->id()] + $this->settings ); } return $this->pluginCollection; diff --git a/src/Plugin/SmsGatewayPluginBase.php b/src/Plugin/SmsGatewayPluginBase.php index d534c78..921b984 100644 --- a/src/Plugin/SmsGatewayPluginBase.php +++ b/src/Plugin/SmsGatewayPluginBase.php @@ -26,6 +26,13 @@ abstract class SmsGatewayPluginBase extends PluginBase implements SmsGatewayPlug protected $logger; /** + * The machine name of the gateway config entity owning this plugin instance. + * + * @var string + */ + protected $gatewayName; + + /** * Construct a new SmsGateway plugin * * @param array $configuration @@ -43,6 +50,13 @@ abstract class SmsGatewayPluginBase extends PluginBase implements SmsGatewayPlug /** * {@inheritdoc} */ + public function setGatewayName($machine_name) { + $this->gatewayName = $machine_name; + } + + /** + * {@inheritdoc} + */ public function getConfiguration() { return $this->configuration; } @@ -132,8 +146,8 @@ abstract class SmsGatewayPluginBase extends PluginBase implements SmsGatewayPlug } public function getDeliveryReportPath($absolute = TRUE) { - if (isset($this->configuration['gateway_name'])) { - return Url::fromRoute('sms.delivery_report', ['gateway' => $this->configuration['gateway_name']], ['absolute' => $absolute])->toString(); + if (isset($this->gatewayName)) { + return Url::fromRoute('sms.delivery_report', ['gateway_name' => $this->gatewayName], ['absolute' => $absolute])->toString(); } else { return ''; diff --git a/src/Plugin/SmsGatewayPluginCollection.php b/src/Plugin/SmsGatewayPluginCollection.php index 4d3d6c0..0de4ef4 100644 --- a/src/Plugin/SmsGatewayPluginCollection.php +++ b/src/Plugin/SmsGatewayPluginCollection.php @@ -7,10 +7,49 @@ namespace Drupal\sms\Plugin; +use Drupal\Component\Plugin\PluginManagerInterface; use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection; /** - * Provides a container for lazily loading SMS Gateway plugins. + * Provides a container for lazily loading SMS Gateway plugin instances. */ class SmsGatewayPluginCollection extends DefaultSingleLazyPluginCollection { + + /** + * The machine name of the gateway config entity using this plugin collection. + * + * @var string + */ + protected $gatewayName; + + /** + * Constructs a new SearchPluginCollection. + * + * @param \Drupal\Component\Plugin\PluginManagerInterface $manager + * The manager to be used for instantiating plugins. + * @param string $instance_id + * The ID of the plugin instance. + * @param string $gateway_name + * The machine name of the gateway config entity. + * @param array $configuration + * An array of configuration. + */ + public function __construct(PluginManagerInterface $manager, $instance_id, $gateway_name, array $configuration) { + parent::__construct($manager, $instance_id, $configuration); + + $this->gatewayName = $gateway_name; + } + + /** + * {@inheritdoc} + */ + protected function initializePlugin($instance_id) { + parent::initializePlugin($instance_id); + + $plugin_instance = $this->pluginInstances[$instance_id]; + if ($plugin_instance instanceof SmsGatewayPluginInterface) { + $plugin_instance->setGatewayName($this->gatewayName); + } + } + } diff --git a/src/Plugin/SmsGatewayPluginInterface.php b/src/Plugin/SmsGatewayPluginInterface.php index f74ee8a..ee41d86 100644 --- a/src/Plugin/SmsGatewayPluginInterface.php +++ b/src/Plugin/SmsGatewayPluginInterface.php @@ -113,7 +113,7 @@ interface SmsGatewayPluginInterface extends ConfigurablePluginInterface, PluginF const STATUS_ERR_OTHER = 500; /** - * Sends an sms and invokes the corresponding sms receipt method. + * Sends an SMS and invokes the corresponding sms receipt method. * * @param \Drupal\sms\Message\SmsMessageInterface $sms * The sms to be sent. @@ -204,4 +204,12 @@ interface SmsGatewayPluginInterface extends ConfigurablePluginInterface, PluginF */ public function getDeliveryReportPath($absolute = TRUE); + /** + * Sets the machine name of the config entity that owns this plugin instance. + * + * @param string $machine_name + * The machine name of the config entity. + */ + public function setGatewayName($machine_name); + } diff --git a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php index ccdaf36..95123d9 100644 --- a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php +++ b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php @@ -93,7 +93,7 @@ class Memory extends SmsGatewayPluginBase { 'time_sent' => $report['time_sent'], 'time_delivered' => $report['time_delivered'], 'status' => $report['status'], - 'gateway' => $this->configuration['gateway_name'], + 'gateway' => $this->gatewayName, 'gateway_status' => $report['gateway_status'], 'gateway_status_code' => $report['gateway_status_code'], 'gateway_status_description' => $report['gateway_status_description'], @@ -130,7 +130,7 @@ class Memory extends SmsGatewayPluginBase { 'time_sent' => time(), 'time_delivered' => time() + rand(0, 10), 'status' => SmsDeliveryReportInterface::STATUS_SENT, - 'gateway' => $this->configuration['gateway_name'], + 'gateway' => $this->gatewayName, 'gateway_status' => 'SENT', 'gateway_status_code' => '200', 'gateway_status_description' => 'Sent to memory gateway',