smsplatform-1.0.x-dev/src/Routing/RouteSubscriber.php

src/Routing/RouteSubscriber.php
<?php

declare(strict_types=1);

namespace Drupal\smsplatform\Routing;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\smsplatform\Entity\SmsGateway;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * Subscriber for SMS Platform routes.
 */
class RouteSubscriber implements ContainerInjectionInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new SMS Platform RouteSubscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The gateway manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory')
    );
  }

  /**
   * Returns a set of route objects.
   *
   * @return \Symfony\Component\Routing\RouteCollection
   *   A route collection.
   */
  public function routes() {
    $sms_settings = $this->configFactory->get('smsplatform.settings');
    $collection = new RouteCollection();

    // Phone number verification.
    $path_verify = $sms_settings->get('page.verify');
    // String length must include at least a slash + another character.
    if (isset($path_verify) && mb_strlen($path_verify) >= 2) {
      $collection->add('smsplatform.phone.verify', new Route(
        $path_verify,
        [
          '_form' => '\Drupal\smsplatform\Form\VerifyPhoneNumberForm',
          '_title' => 'Verify a phone number',
        ],
        [
          '_permission' => 'sms verify phone number',
        ]
      ));
    }

    /** @var \Drupal\smsplatform\Entity\SmsGatewayInterface $gateway */
    foreach (SmsGateway::loadMultiple() as $id => $gateway) {
      if ($gateway->supportsReportsPush()) {
        $path = $gateway->getPushReportPath();
        if (isset($path) && mb_strlen($path) >= 2 && mb_substr($path, 0, 1) == '/') {
          $route = (new Route($path))
            ->setDefault('_controller', '\Drupal\smsplatform\DeliveryReportController::processDeliveryReport')
            ->setDefault('_sms_gateway_push_endpoint', $id)
            ->setRequirement('_smsplatform_gateway_supports_pushed_reports', 'TRUE');
          $collection->add('sms.delivery_report.receive.' . $id, $route);
        }
      }

      if ($gateway->autoCreateIncomingRoute()) {
        $path = $gateway->getPushIncomingPath();
        if (isset($path) && mb_strlen($path) >= 2 && mb_substr($path, 0, 1) == '/') {
          $parameters['smsplatform_gateway']['type'] = 'entity:smsplatform_gateway';
          $route = (new Route($path))
            ->setDefault('smsplatform_gateway', $id)
            ->setDefault('_controller', '\Drupal\smsplatform\SmsIncomingController::processIncoming')
            ->setRequirement('_access', 'TRUE')
            ->setOption('parameters', $parameters)
            ->setMethods(['POST']);
          $collection->add('sms.incoming.receive.' . $id, $route);
        }
      }
    }

    return $collection;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc