Browse Source

Refactoring services #885

refactoring
Guillaume Bourgeois 1 year ago
parent
commit
9fd162f267
5 changed files with 41 additions and 43 deletions
  1. +6
    -0
      common/components/BusinessLogicContainerTrait.php
  2. +2
    -2
      common/logic/Producer/Producer/ProducerRepository.php
  3. +15
    -24
      frontend/controllers/SiteController.php
  4. +2
    -2
      frontend/controllers/UserController.php
  5. +16
    -15
      frontend/forms/SignupForm.php

+ 6
- 0
common/components/BusinessLogicContainerTrait.php View File

@@ -48,11 +48,17 @@ use common\logic\User\User\UserManager;
use common\logic\User\UserGroup\UserGroupContainer;
use common\logic\User\UserGroup\UserGroupManager;
use common\logic\User\UserProducer\UserProducerContainer;
use common\logic\User\UserProducer\UserProducerManager;
use common\logic\User\UserUserGroup\UserUserGroupContainer;
use common\logic\User\UserUserGroup\UserUserGroupManager;

trait BusinessLogicContainerTrait
{
public function getUserProducerManager(): UserProducerManager
{
return new UserProducerManager();
}

public function getOrderManager(): OrderManager
{
return new OrderManager();

+ 2
- 2
common/logic/Producer/Producer/ProducerRepository.php View File

@@ -52,7 +52,7 @@ class ProducerRepository extends BaseService implements RepositoryInterface
return Producer::searchOne(['slug' => $slug]);
}

public function queryProducerActive()
public function queryProducersActive()
{
return Producer::find()
->where([
@@ -73,7 +73,7 @@ class ProducerRepository extends BaseService implements RepositoryInterface
/**
* Retourne la liste des établissements pour l'initialisation d'une listesélective.
*/
public static function getPopulateDropdown(): array
public static function populateProducerDropdown(): array
{
$producers = Producer::find()
->where([

+ 15
- 24
frontend/controllers/SiteController.php View File

@@ -127,8 +127,7 @@ class SiteController extends FrontendController
public function actionIndex()
{
return $this->render('index', [
'producerDemoAccount' => $this->getLogic()->getProducerContainer()
->getRepository()->getOneDemoAccount(),
'producerDemoAccount' => $this->getProducerManager()->findOneProducerDemoAccount(),
'dataProviderPrices' => $this->getDataProviderPrices()
]);
}
@@ -146,8 +145,7 @@ class SiteController extends FrontendController
public function getDataProviderPrices()
{
return new ActiveDataProvider([
'query' => $this->getLogic()->getProducerPriceRangeContainer()
->getRepository()->query(),
'query' => $this->getProducerPriceRangeManager()->queryProducerPriceRanges(),
'pagination' => [
'pageSize' => 100,
],
@@ -160,8 +158,7 @@ class SiteController extends FrontendController
public function actionProducers()
{
$dataProviderProducers = new ActiveDataProvider([
'query' => $this->getLogic()->getProducerContainer()
->getRepository()->queryActive(),
'query' => $this->getProducerManager()->queryProducersActive(),
'pagination' => [
'pageSize' => 100,
],
@@ -189,11 +186,7 @@ class SiteController extends FrontendController
return $this->redirect($returnUrl);
}
else {
$userProducerArray = $this->getLogic()
->getUserProducerContainer()
->getRepository()
->getBy(GlobalParam::getCurrentUser());

$userProducerArray = $this->getUserProducerManager()->findUserProducersByUser(GlobalParam::getCurrentUser());
if ($userProducerArray
&& is_array($userProducerArray)
&& count($userProducerArray) == 1) {
@@ -257,7 +250,7 @@ class SiteController extends FrontendController
public function actionSignup()
{
$model = new SignupForm();
$producerRepository = \Yii::$app->logic->getProducerContainer()->getRepository();
$producerManager = $this->getProducerManager();

if ($model->load(Yii::$app->request->post())) {
$user = $model->signup();
@@ -268,9 +261,9 @@ class SiteController extends FrontendController
$this->redirect(Yii::$app->urlManagerBackend->createAbsoluteUrl(['site/index']));
}
else {
$producer = $producerRepository->getOneById($model->id_producer);
$producer = $producerManager->findOneProducerById($model->id_producer);
if ($producer) {
$this->redirect(Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index', 'slug_producer' => $producer->slug]));
$this->redirect($this->getUrlManagerProducer()->createAbsoluteUrl(['site/index', 'slug_producer' => $producer->slug]));
}
else {
$this->redirect(['site/index']);
@@ -281,7 +274,7 @@ class SiteController extends FrontendController
}

// Liste des producteurs disponibles
$producersArray = $producerRepository->getPopulateDropdown();
$producersArray = $producerManager->populateProducerDropdown();
$dataProducers = $producersArray['data'];
$optionsProducers = $producersArray['options'];

@@ -348,8 +341,8 @@ class SiteController extends FrontendController
*/
public function actionProducerCode(int $id)
{
$producerContainer = $this->getLogic()->getProducerContainer();
$producer = $producerContainer->getRepository()->getOneById($id);
$producerManager = $this->getProducerManager();
$producer = $producerManager->findOneProducerById($id);

if (!$producer) {
throw new \yii\web\HttpException(404, 'Producteur introuvable');
@@ -361,7 +354,7 @@ class SiteController extends FrontendController
if ($producerCodeForm->load($this->getRequest()->post())
&& $producerCodeForm->validate()) {

$producerContainer->getBuilder()->addUser(GlobalParam::getCurrentUser(), $producer);
$producerManager->addUser(GlobalParam::getCurrentUser(), $producer);

$this->redirect($this->getUrlManagerProducer()->createAbsoluteUrl(['site/index', 'slug_producer' => $producer->slug]));
}
@@ -380,9 +373,8 @@ class SiteController extends FrontendController
{
$loginForm = new LoginForm();
$signupForm = new SignupForm();
$producerContainer = $this->getLogic()->getProducerContainer();

$producer = $producerContainer->getRepository()->getOneById($id);
$producerManager = $this->getProducerManager();
$producer = $producerManager->findOneProducerById($id);
$loginForm->id_producer = $id;
$signupForm->id_producer = $id;
$signupForm->option_user_producer = 'user';
@@ -391,7 +383,7 @@ class SiteController extends FrontendController
if ($this->getUser()->isGuest) {
if ($loginForm->load($this->getRequest()->post()) && $loginForm->login()) {
if (!strlen($producer->code)) {
$producerContainer->getBuilder()->addUser(GlobalParam::getCurrentUser(), $producer);
$producerManager->addUser(GlobalParam::getCurrentUser(), $producer);
}

$this->redirect($returnUrl);
@@ -421,8 +413,7 @@ class SiteController extends FrontendController
public function actionProducerOffline(int $id)
{
return $this->render('producer_offline', [
'producer' => $this->getLogic()->getProducerContainer()
->getRepository()->getOneById($id),
'producer' => $this->getProducerManager()->findOneProducerById($id),
]);
}


+ 2
- 2
frontend/controllers/UserController.php View File

@@ -129,9 +129,9 @@ class UserController extends FrontendController
*/
protected function findModel($id)
{
$userRepository = $this->getLogic()->getUserContainer()->getRepository();
$userRepository = $this->getUserManager();

if (($model = $userRepository->getOneById($id)) !== null) {
if (($model = $userRepository->findOneUserById($id)) !== null) {
return $model;
}
else {

+ 16
- 15
frontend/forms/SignupForm.php View File

@@ -39,8 +39,10 @@
namespace frontend\forms;

use common\logic\Producer\Producer\Producer;
use common\logic\Producer\Producer\ProducerManager;
use common\logic\User\User\User;
use common\logic\Config\TaxRate\TaxRate;
use common\logic\User\User\UserManager;
use yii\base\Model;

/**
@@ -231,12 +233,12 @@ class SignupForm extends Model
*/
public function signup()
{
$userContainer = \Yii::$app->logic->getUserContainer();
$producerContainer = \Yii::$app->logic->getProducerContainer();
$userManager = \Yii::$app->logic->getUserManager();
$producerManager = \Yii::$app->logic->getProducerManager();

if ($this->validate()) {

$user = $userContainer->getFactory()->create();
$user = $userManager->instanciateUser();
$this->populateUser($user);

if ($this->isProducer()) {
@@ -246,15 +248,15 @@ class SignupForm extends Model
$idProducer = $this->id_producer;
}

$producer = $producerContainer->getRepository()->getOneById($idProducer);
$producer = $producerManager->findOneProducerById($idProducer);

if ($user->save() && $producer) {
// Liaison User / Producer
$producerContainer->getBuilder()->addUser($user->id, $idProducer);
$producerManager->addUser($user->id, $idProducer);

// Envoi d'un email de bienvenue à l'utilisateur
if ($this->isCustomer()) {
$userContainer->getUtils()->sendEmailSignup($user, $producer);
$userManager->sendEmailSignup($user, $producer);
}

return $user;
@@ -276,38 +278,37 @@ class SignupForm extends Model

public function processFormProducer($user)
{
$userContainer = \Yii::$app->logic->getUserContainer();
$producerContainer = \Yii::$app->logic->getProducerContainer();
$userManager = new UserManager();
$producerManager = new ProducerManager();

// Création du producteur
$producer = $producerContainer->getFactory()->createInstance();
$producer = $producerManager->instanciateProducer();
$this->populateProducer($producer);
$producerContainer->getBuilder()->init($producer);
$producerManager->initProducer($producer);
$producer->save();

/*
* Envoi d'un email à l'administrateur pour le prévenir
* qu'un nouveau producteur s'est inscrit
*/
$producerContainer->getUtils()->sendEmailNewProducer($producer);
$producerManager->sendEmailNewProducer($producer);

// Initialisation de l'utilisateur (statut et liaison au producteur)
$userContainer->getBuilder()->initProducer($user, $producer);
$userManager->initProducer($user, $producer);

return $producer;
}

public function populateUser($user)
{
$userContainer = \Yii::$app->logic->getUserContainer();
$userManager = new UserManager();
$userManager->initPassword($user, $this->password);

$user->username = $this->email;
$user->email = $this->email;
$user->name = $this->name;
$user->lastname = $this->lastname;
$user->phone = $this->phone;

$userContainer->getBuilder()->initPassword($user, $this->password);
}

public function populateProducer($producer)

Loading…
Cancel
Save