Commit bd8da47e authored by Dieter Holvoet's avatar Dieter Holvoet
Browse files

Issue #3298434 by DieterHolvoet, diegors, Joel Guerreiro Borghi Filho,...

Issue #3298434 by DieterHolvoet, diegors, Joel Guerreiro Borghi Filho, jobsons, gquisini, sophiavs: Replace static code with dependency injection
parent 95dc8cd2
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -14,3 +14,7 @@ services:
  content_planner.content_moderation_service:
    class: Drupal\content_planner\ContentModerationService
    arguments: ['@entity_type.manager']

  content_planner.user_profile_image:
    class: Drupal\content_planner\UserProfileImage
    arguments: ['@entity_type.manager']
+4 −0
Original line number Diff line number Diff line
services:
  content_calendar.content_service:
    class: Drupal\content_calendar\ContentService
    arguments: ['@entity_type.manager']

  content_calendar.content_type_config_service:
    class: Drupal\content_calendar\ContentTypeConfigService
    arguments: ['@config.factory']
+39 −25
Original line number Diff line number Diff line
@@ -3,82 +3,92 @@
namespace Drupal\content_calendar;

use Drupal\content_calendar\Entity\ContentTypeConfig;
use Drupal\node\Entity\Node;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
 * Implements UserProfileImage class.
 * Implements ContentService class.
 */
class ContentService {

  /**
   * Type Configuration.
   * The entity type manager.
   *
   * @var int
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  private $contentTypeConfig;
  protected $entityTypeManager;

  /**
   * ContentService constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct() {
    $this->contentTypeConfig = ContentTypeConfig::loadMultiple();
  public function __construct(
    EntityTypeManagerInterface $entity_type_manager
  ) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Get Content Configuration Type.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]|static[]
   *   Returns an static interface.
   * @return ContentTypeConfig[]
   */
  public function getContentTypeConfig() {
    return $this->contentTypeConfig;
  public function getContentTypeConfig(): array {
    return $this->entityTypeManager
      ->getStorage('content_type_config')
      ->loadMultiple();
  }

  /**
   * Get the recent content.
   */
  public function getRecentContent($limit) {
  public function getRecentContent($limit): array {
    $configs = $this->getContentTypeConfig();
    $types = [];

    if (is_array($configs)) {
    foreach ($configs as $config) {
      $types[] = $config->getOriginalId();
    }
    }

    if (empty($types)) {
      return [];
    }

    $ids = \Drupal::entityQuery('node')
    $storage = $this->entityTypeManager
        ->getStorage('node');
    $ids = $storage->getQuery()
      ->condition('status', 1)
      ->condition('type', $types, 'IN')
      ->sort('created', 'DESC')
      ->range(0, $limit)
      ->execute();

    return Node::loadMultiple($ids);
    if ($ids === []) {
      return [];
    }

    return $storage->loadMultiple($ids);
  }

  /**
   * Get the following content.
   */
  public function getFollowingContent($limit) {
  public function getFollowingContent($limit): array {
    $configs = $this->getContentTypeConfig();
    $types = [];

    if (is_array($configs)) {
    foreach ($configs as $config) {
      $types[] = $config->getOriginalId();
    }
    }

    if (empty($types)) {
      return [];
    }

    $ids = \Drupal::entityQuery('node')
    $storage = $this->entityTypeManager
        ->getStorage('node');
    $ids = $storage->getQuery()
      ->condition('status', 0)
      ->condition('type', $types, 'IN')
      ->condition('publish_on', NULL, 'IS NOT NULL')
@@ -86,7 +96,11 @@ class ContentService {
      ->range(0, $limit)
      ->execute();

    return Node::loadMultiple($ids);
    if ($ids === []) {
      return [];
    }

    return $storage->loadMultiple($ids);
  }

}
+10 −33
Original line number Diff line number Diff line
@@ -3,18 +3,12 @@
namespace Drupal\content_calendar\Controller;

use Drupal\content_calendar\Component\Calendar;
use Drupal\content_calendar\ContentCalendarService;
use Drupal\content_calendar\DateTimeHelper;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\content_calendar\ContentTypeConfigService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Implements CalendarController class.
@@ -56,37 +50,20 @@ class CalendarController extends ControllerBase {
   */
  protected $redirectDestination;

  /**
   * Constructs a new CalendarController object.
   */
  public function __construct(
    ThemeManagerInterface $theme_manager,
    RequestStack $request_stack,
    ContentTypeConfigService $content_type_config_service,
    ContentCalendarService $content_calendar_service,
    AccountProxyInterface $current_user,
    RedirectDestinationInterface $redirect_destination
  ) {
    $this->themeManager = $theme_manager;
    $this->request = $request_stack->getCurrentRequest();
    $this->contentTypeConfigService = $content_type_config_service;
    $this->contentCalendarService = $content_calendar_service;
    $this->currentUser = $current_user;
    $this->redirectDestination = $redirect_destination;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('theme.manager'),
      $container->get('request_stack'),
      $container->get('content_calendar.content_type_config_service'),
      $container->get('content_calendar.content_calendar_service'),
      $container->get('current_user'),
      $container->get('redirect.destination')
    );

    $instance = parent::create($container);
    $instance->themeManager = $container->get('theme.manager');
    $instance->request = $container->get('request_stack')->getCurrentRequest();
    $instance->contentTypeConfigService = $container->get('content_calendar.content_type_config_service');
    $instance->contentCalendarService = $container->get('content_calendar.content_calendar_service');
    $instance->currentUser = $container->get('current_user');
    $instance->redirectDestination = $container->get('redirect.destination');

    return $instance;
  }

  /**
+1 −1
Original line number Diff line number Diff line
@@ -166,7 +166,7 @@ abstract class DateTimeHelper {
   */
  public static function getDayOfWeekByDate(\DateTime $dateTime) {
    $weekdays = self::getWeekdays();
    $dayName = $this->t($dateTime->format('l'));
    $dayName = t($dateTime->format('l'));
    return array_search($dayName, $weekdays, FALSE) + 1;
  }

Loading