Browse Source

[Technique] Refactoring UserManager en UserModule #1327

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
80caea510f
4 changed files with 57 additions and 24 deletions
  1. +6
    -19
      common/logic/AbstractModule.php
  2. +1
    -2
      common/logic/AbstractSingleton.php
  3. +1
    -1
      common/logic/ModuleInterface.php
  4. +49
    -2
      common/logic/User/User/Wrapper/UserModule.php

+ 6
- 19
common/logic/AbstractModule.php View File

@@ -6,11 +6,14 @@ use yii\base\ErrorException;

abstract class AbstractModule extends AbstractSingleton implements ModuleInterface
{
protected ContainerInterface $container;
public function getEntityFqcn(): string
{
return $this->getDefinition()->getEntityFqcn();
}

public function __call($method, $args)
{
foreach($this->getContainer()->getServices() as $serviceClass) {
foreach($this->getServices() as $serviceClass) {
if(method_exists($serviceClass, $method)) {
return call_user_func_array(
[$serviceClass::getInstance(), $method],
@@ -19,22 +22,6 @@ abstract class AbstractModule extends AbstractSingleton implements ModuleInterfa
}
}

throw new ErrorException('La méthode '.$method.' est introuvable dans les services du container '.get_class($this->container));
}

protected function setContainer(ContainerInterface $container): void
{
$this->container = $container;
}

public function getContainer(): ContainerInterface
{
return $this->container;
}

public function initContainer(): void
{
$containerFqcn = $this->getContainerFqcn();
$this->setContainer($containerFqcn::getInstance());
throw new ErrorException('La méthode '.$method.' est introuvable dans les services du module '.get_class($this));
}
}

+ 1
- 2
common/logic/AbstractSingleton.php View File

@@ -25,8 +25,7 @@ abstract class AbstractSingleton
if (!isset(self::$instances[$c])) {
self::$instances[$c] = new $c;

if(in_array(ManagerInterface::class, class_implements(self::$instances[$c]))
|| in_array(ModuleInterface::class, class_implements(self::$instances[$c]))) {
if(in_array(ManagerInterface::class, class_implements(self::$instances[$c]))) {
self::$instances[$c]->initContainer();
}


+ 1
- 1
common/logic/ModuleInterface.php View File

@@ -4,5 +4,5 @@ namespace common\logic;

interface ModuleInterface
{
public function getContainerFqcn(): string;
public function getServices(): array;
}

+ 49
- 2
common/logic/User/User/Wrapper/UserModule.php View File

@@ -3,7 +3,9 @@
namespace common\logic\User\User\Wrapper;

use common\logic\AbstractModule;
use common\logic\User\User\Model\User;
use common\logic\User\User\Repository\UserRepository;
use common\logic\User\User\Repository\UserRepositoryQuery;
use common\logic\User\User\Service\AuthorizationChecker;
use common\logic\User\User\Service\NewsletterManager;
use common\logic\User\User\Service\UserBuilder;
@@ -24,9 +26,54 @@ use common\logic\User\User\Service\UserSolver;
*/
class UserModule extends AbstractModule
{
public function getContainerFqcn(): string
public function getServices(): array
{
return UserContainer::class;
return [
UserDefinition::class,
UserSolver::class,
UserRepositoryQuery::class,
UserRepository::class,
UserBuilder::class,
UserNotifier::class,
UsersCreditCsvGenerator::class,
NewsletterManager::class,
AuthorizationChecker::class,
];
}

public function getDefinition(): UserDefinition
{
return UserDefinition::getInstance();
}

public function getSolver(): UserSolver
{
return UserSolver::getInstance();
}

public function getRepository(): UserRepository
{
return UserRepository::getInstance();
}

public function getBuilder(): UserBuilder
{
return UserBuilder::getInstance();
}

public function getNotifier(): UserNotifier
{
return UserNotifier::getInstance();
}

public function getUsersCreditCsvGenerator(): UsersCreditCsvGenerator
{
return UsersCreditCsvGenerator::getInstance();
}

public function getNewsletterManager(): NewsletterManager
{
return NewsletterManager::getInstance();
}

public function getAuthorizationChecker(): AuthorizationChecker

Loading…
Cancel
Save