Skip to content
Snippets Groups Projects

Resolve #3475418 "Wba modernize services"

1 unresolved thread
Files
11
+ 22
42
@@ -5,8 +5,9 @@ namespace Drupal\workbench_access;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\State\StateInterface;
use Drupal\user\RoleStorageInterface;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
use Symfony\Component\DependencyInjection\Attribute\AutowireServiceClosure;
/**
* Defines a role-section storage that uses the State API.
@@ -15,43 +16,16 @@ class RoleSectionStorage implements RoleSectionStorageInterface {
use DependencySerializationTrait;
/**
* State.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new RoleSectionStorage object.
*
* @param \Drupal\Core\State\StateInterface $state
* State service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager.
*/
public function __construct(StateInterface $state, EntityTypeManagerInterface $entityTypeManager) {
$this->state = $state;
$this->entityTypeManager = $entityTypeManager;
}
/**
* Gets section storage.
*
* @return \Drupal\workbench_access\SectionAssociationStorageInterface
* Section storage.
* @phpstan-param (\Closure(): \Drupal\workbench_access\UserSectionStorageInterface) $userSectionStorage
*/
protected function sectionStorage() {
// The entity build process takes place too early in the call stack and we
// have test fails if we add this to the __construct().
return $this->entityTypeManager->getStorage('section_association');
final public function __construct(
private readonly EntityTypeManagerInterface $entityTypeManager,
#[AutowireServiceClosure(UserSectionStorageInterface::class)]
private readonly \Closure $userSectionStorage,
) {
}
/**
@@ -78,8 +52,7 @@ class RoleSectionStorage implements RoleSectionStorageInterface {
$section_association = $this->sectionStorage()->create($values);
}
$section_association->save();
// @phpstan-ignore-next-line
\Drupal::service('workbench_access.user_section_storage')->resetCache($scheme);
($this->userSectionStorage)()->resetCache($scheme);
}
}
@@ -101,8 +74,7 @@ class RoleSectionStorage implements RoleSectionStorageInterface {
}
$section_association->save();
}
// @phpstan-ignore-next-line
\Drupal::service('workbench_access.user_section_storage')->resetCache($scheme);
($this->userSectionStorage)()->resetCache($scheme);
}
}
@@ -208,12 +180,20 @@ class RoleSectionStorage implements RoleSectionStorageInterface {
* The entity build process takes place too early in the call stack so we
* end up with a stale reference to the user_role storage handler if we do
* this in the constructor.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* Role storage.
*/
protected function roleStorage() {
protected function roleStorage(): RoleStorageInterface {
/** @var \Drupal\user\RoleStorageInterface */
return $this->entityTypeManager->getStorage('user_role');
}
/**
* Gets section storage.
*/
private function sectionStorage(): SectionAssociationStorageInterface {
// The entity build process takes place too early in the call stack and we
// have test fails if we add this to the __construct().
/** @var \Drupal\workbench_access\SectionAssociationStorageInterface */
return $this->entityTypeManager->getStorage('section_association');
}
}
Loading