<?php

namespace Drupal\nodehive_api_key_management\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Class JsonApiRoutesAddPermission.
 *
 * Adds a specific permission to JSON:API routes.
 */
class JsonApiRoutesAddPermission extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($collection as $route) {
      $defaults = $route->getDefaults();
      if (!empty($defaults['_is_jsonapi'])) {
        $route->setRequirement('_permission', 'access api endpoints');
      }
    }
  }

}
