Commit 4700b118 authored by narendraR's avatar narendraR Committed by Tim Plunkett
Browse files

Issue #3279510 by narendraR: Reevaluate or remove DrupalOrgClient

parent d52965ac
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -3,4 +3,4 @@ services:
    class: Drupal\project_browser\Commands\UpdateFixtureCommands
    tags:
      - { name: drush.command }
    arguments: ['@logger.factory', '@logger.dblog', '@http_client', '@state', '@database']
    arguments: ['@logger.factory', '@logger.dblog', '@project_browser.enabled_source', '@event_dispatcher']
+10 −7
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@

use Composer\Semver\Semver;
use Drupal\Component\Serialization\Json;
use Drupal\project_browser\DrupalOrg\DrupalOrgClient;
use Drupal\project_browser\DrupalOrg\DrupalOrgProjects;
use Drupal\Core\Database\Database;

@@ -237,8 +236,8 @@ function project_browser_hacky_fixture_maker(&$sandbox) {
    $sandbox['projects'] = [];
  }

  $drupal_org_client = \Drupal::classResolver(DrupalOrgClient::class);

  $current_source = \Drupal::service('project_browser.enabled_source')->getCurrentSource();
  if ($current_source && $current_source->getPluginId() === 'drupalorg_mockapi') {
    $query = [
      'page' => $sandbox['current_page'],
      'field_project_type' => 'full',
@@ -251,7 +250,7 @@ function project_browser_hacky_fixture_maker(&$sandbox) {
      'direction' => 'DESC',
    ];
    $eariest_possible_timestamp_reached = NULL;
  $drupal_org_response = $drupal_org_client->getProjects($query);
    $drupal_org_response = $current_source->getProjectsFromSource($query);
    $returned_projects = new DrupalOrgProjects($drupal_org_response['list']);

    if ($returned_projects) {
@@ -271,9 +270,9 @@ function project_browser_hacky_fixture_maker(&$sandbox) {

      // Rewrite the projects array so each project has added release data and
      // unnecessary values are removed to conserve space.
    $projects_to_store = array_map(function ($a_project) use ($drupal_org_client) {
      $projects_to_store = array_map(function ($a_project) use ($current_source) {
        $the_project = (array) $a_project;
      $releases = $drupal_org_client->getProjectReleases($the_project['field_project_machine_name']);
        $releases = $current_source->getProjectReleasesFromSource($the_project['field_project_machine_name']);
        if (!empty($releases['releases'])) {
          $compatible_releases = array_filter($releases['releases'], function ($release) {
            if (!empty($release['core_compatibility'])) {
@@ -307,7 +306,7 @@ function project_browser_hacky_fixture_maker(&$sandbox) {
            'core_compatibility' => $release['core_compatibility'],
          ];
        }, $compatible_releases);
      $drupal_org_client->truncateProjectData($the_project);
        $current_source->truncateProjectData($the_project);
        return $the_project;
      }, $projects_to_store);

@@ -319,6 +318,10 @@ function project_browser_hacky_fixture_maker(&$sandbox) {
    else {
      $sandbox['#finished'] = TRUE;
    }
  }
  else {
    $sandbox['#finished'] = TRUE;
  }

  if ($sandbox['#finished'] === TRUE) {
    $module_path = \Drupal::service('module_handler')->getModule('project_browser')->getPath();
+7 −0
Original line number Diff line number Diff line
@@ -5,3 +5,10 @@ services:
  plugin.manager.project_browser.source:
    class: Drupal\project_browser\Plugin\ProjectBrowserSourceManager
    parent: default_plugin_manager
  project_browser.enabled_source:
    class: Drupal\project_browser\EnabledSourceHandler
    arguments: ['@logger.channel.project_browser', '@config.factory', '@plugin.manager.project_browser.source']
  project_browser.update_fixture_subscriber:
    class: '\Drupal\project_browser\EventSubscriber\UpdateFixtureSubscriber'
    tags:
      - { name: 'event_subscriber' }
+30 −18
Original line number Diff line number Diff line
@@ -2,14 +2,14 @@

namespace Drupal\project_browser\Commands;

use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\project_browser\DrupalOrg\DrupalOrgClient;
use Drupal\project_browser\EnabledSourceHandler;
use Drupal\project_browser\Event\ProjectBrowserEvents;
use Drupal\project_browser\Event\UpdateFixtureEvent;
use Drush\Commands\DrushCommands;
use GuzzleHttp\ClientInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * A Drush commandfile.
@@ -40,6 +40,20 @@ class UpdateFixtureCommands extends DrushCommands {
   */
  private $loggerChannelFactory;

  /**
   * The EnabledSourceHandler.
   *
   * @var \Drupal\project_browser\EnabledSourceHandler
   */
  protected $enabledSource;

  /**
   * The event dispatcher service.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Constructs a new UpdateFixtureCommands object.
   *
@@ -47,20 +61,17 @@ class UpdateFixtureCommands extends DrushCommands {
   *   Logger service.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \GuzzleHttp\ClientInterface $http_client
   *   A Guzzle client object.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state object.
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection to be used.
   * @param \Drupal\project_browser\EnabledSourceHandler $enabled_source
   *   The enabled source.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   Event dispatcher service.
   */
  public function __construct(LoggerChannelFactoryInterface $loggerChannelFactory, LoggerInterface $logger, ClientInterface $http_client, StateInterface $state, Connection $database) {
  public function __construct(LoggerChannelFactoryInterface $loggerChannelFactory, LoggerInterface $logger, EnabledSourceHandler $enabled_source, EventDispatcherInterface $event_dispatcher) {
    parent::__construct();
    $this->loggerChannelFactory = $loggerChannelFactory;
    $this->logger = $logger;
    $this->httpClient = $http_client;
    $this->state = $state;
    $this->database = $database;
    $this->enabledSource = $enabled_source;
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
@@ -71,13 +82,14 @@ class UpdateFixtureCommands extends DrushCommands {
   *
   * @usage update:project-modules
   */
  public function updateProjectModules($type = '') {
    // 1. Log the start of the script.
  public function updateProjectModules() {
    // Log the start of the script.
    $this->loggerChannelFactory->get('project_browser')->info($this->t('Update fixture batch operations start'));
    $this->logger->notice($this->t('Starting...'));

    $drupal_org_client = new DrupalOrgClient($this->httpClient, $this->state, $this->database);
    $drupal_org_client->updateMostRecentChanges();
    // Dispatch the event so that event listeners of other source can update their fixture.
    $event = new UpdateFixtureEvent($this->enabledSource);
    $this->eventDispatcher->dispatch(ProjectBrowserEvents::UPDATE_FIXTURE, $event);

    $this->logger->notice($this->t('Completed.'));
  }
+12 −55
Original line number Diff line number Diff line
@@ -3,11 +3,8 @@
namespace Drupal\project_browser\Controller;

use Drupal\Core\Cache\CacheableResponseInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\project_browser\Plugin\ProjectBrowserSourceInterface;
use Drupal\project_browser\Plugin\ProjectBrowserSourceManager;
use Psr\Log\LoggerInterface;
use Drupal\project_browser\EnabledSourceHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
@@ -19,40 +16,20 @@ use Symfony\Component\HttpFoundation\Response;
class ProjectBrowserEndpointController extends ControllerBase {

  /**
   * A logger instance.
   * The EnabledSourceHandler.
   *
   * @var \Psr\Log\LoggerInterface
   * @var \Drupal\project_browser\EnabledSourceHandler
   */
  protected $logger;

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

  /**
   * The ProjectBrowserSourceManager.
   *
   * @var \Drupal\project_browser\plugin\ProjectBrowserSourceManager
   */
  private $pluginManager;
  protected $enabledSource;

  /**
   * ProjectBrowserEndpointController constructor.
   *
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\project_browser\plugin\ProjectBrowserSourceManager $plugin_manager
   *   The plugin manager.
   * @param \Drupal\project_browser\EnabledSourceHandler $enabled_source
   *   The enabled source.
   */
  public function __construct(LoggerInterface $logger, ConfigFactoryInterface $config_factory, ProjectBrowserSourceManager $plugin_manager) {
    $this->logger = $logger;
    $this->configFactory = $config_factory;
    $this->pluginManager = $plugin_manager;
  public function __construct(EnabledSourceHandler $enabled_source) {
    $this->enabledSource = $enabled_source;
  }

  /**
@@ -60,30 +37,10 @@ class ProjectBrowserEndpointController extends ControllerBase {
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('logger.factory')->get('project_browser'),
      $container->get('config.factory'),
      $container->get('plugin.manager.project_browser.source'),
      $container->get('project_browser.enabled_source'),
    );
  }

  /**
   * Returns a plugin instance corresponding to the enabled_source config.
   *
   * @return \Drupal\project_browser\Plugin\ProjectBrowserSourceInterface|null
   *   The Project Browser source plugin, or NULL.
   */
  protected function getCurrentSource(): ?ProjectBrowserSourceInterface {
    $config = $this->configFactory->get('project_browser.admin_settings');
    $plugin_id = $config->get('enabled_source');
    if (!$this->pluginManager->hasDefinition($plugin_id)) {
      // Ignore if the plugin does not exist, but log it.
      $this->logger->warning('Project browser tried to load the enabled source %source, but the plugin does not exist. Make sure you have run update.php after updating the Project Browser module.', ['%source' => $plugin_id]);
      return NULL;
    }

    return $this->pluginManager->createInstance($plugin_id);
  }

  /**
   * Responds to GET requests.
   *
@@ -96,7 +53,7 @@ class ProjectBrowserEndpointController extends ControllerBase {
   *   Typically a project listing.
   */
  public function getAllProjects(Request $request) {
    $current_source = $this->getCurrentSource();
    $current_source = $this->enabledSource->getCurrentSource();
    if (!$current_source) {
      return new JsonResponse([], Response::HTTP_ACCEPTED);
    }
@@ -113,7 +70,7 @@ class ProjectBrowserEndpointController extends ControllerBase {
   *   The request.
   */
  public function getProjectReleases(Request $request) {
    $current_source = $this->getCurrentSource();
    $current_source = $this->enabledSource->getCurrentSource();
    if (!$current_source) {
      return new JsonResponse([], Response::HTTP_ACCEPTED);
    }
@@ -141,7 +98,7 @@ class ProjectBrowserEndpointController extends ControllerBase {
   *   The request.
   */
  public function getAllCategories(Request $request) {
    $current_source = $this->getCurrentSource();
    $current_source = $this->enabledSource->getCurrentSource();
    if (!$current_source) {
      return new JsonResponse([], Response::HTTP_ACCEPTED);
    }
Loading