Skip to content

Commit

Permalink
code refactoring - decouple repec service from factory
Browse files Browse the repository at this point in the history
  • Loading branch information
subhojit777 committed Mar 4, 2019
1 parent 9aa0ea7 commit 786657a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/Series/Base.php
Expand Up @@ -43,6 +43,13 @@ abstract class Base {
*/
protected $messenger;

/**
* Bundle specific setting in repec settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $bundleSettings;

/**
* Base constructor.
*
Expand All @@ -54,12 +61,15 @@ abstract class Base {
* Messenger.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
* @param array|null $bundle_settings
* Bundle specific setting in repec settings.
*/
public function __construct(ImmutableConfig $settings, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, ContentEntityInterface $entity) {
public function __construct(ImmutableConfig $settings, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, ContentEntityInterface $entity, array $bundle_settings) {
$this->settings = $settings;
$this->entityTypeManager = $entity_type_manager;
$this->messenger = $messenger;
$this->entity = $entity;
$this->bundleSettings = $bundle_settings;
}

/**
Expand All @@ -79,14 +89,8 @@ abstract public function getDefault() : array;
* @throws \Drupal\repec\Series\CreateException
*/
public function create(array $template) {
/** @var array $bundle_settings */
// The following is already done in
// \Drupal\repec\Repec::getEntityBundleSettings
// TODO: Move this to a parent service or something.
$bundle_settings = unserialize($this->settings->get("repec_bundle.{$this->entity->getEntityTypeId()}.{$this->entity->bundle()}"));

/** @var string $serie_directory_config */
$serie_directory_config = $bundle_settings['serie_directory'];
$serie_directory_config = $this->bundleSettings['serie_directory'];

$archive_directory = "public://{$this->settings->get('base_path')}/{$this->settings->get('archive_code')}/";

Expand Down
6 changes: 5 additions & 1 deletion src/TemplateFactory.php
Expand Up @@ -63,7 +63,11 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Con
*/
public function create($series_type, ContentEntityInterface $entity) : Base {
$template_class = "Drupal\\repec\\Series\\$series_type\\Template";
return new $template_class($this->configFactory->get('repec.settings'), $this->entityTypeManager, $this->messenger, $entity);
/** @var \Drupal\Core\Config\ImmutableConfig $repec_settings */
$repec_settings = $this->configFactory->get('repec.settings');
/** @var array|null $bundle_settings */
$bundle_settings = unserialize($repec_settings->get("repec_bundle.{$entity->getEntityTypeId()}.{$entity->bundle()}"));
return new $template_class($repec_settings, $this->entityTypeManager, $this->messenger, $entity, $bundle_settings);
}

}

0 comments on commit 786657a

Please sign in to comment.