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

use common\logic\User\UserGroup\UserGroupContainer; use common\logic\User\UserGroup\UserGroupContainer;
use common\logic\User\UserGroup\UserGroupManager; use common\logic\User\UserGroup\UserGroupManager;
use common\logic\User\UserProducer\UserProducerContainer; use common\logic\User\UserProducer\UserProducerContainer;
use common\logic\User\UserProducer\UserProducerManager;
use common\logic\User\UserUserGroup\UserUserGroupContainer; use common\logic\User\UserUserGroup\UserUserGroupContainer;
use common\logic\User\UserUserGroup\UserUserGroupManager; use common\logic\User\UserUserGroup\UserUserGroupManager;


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

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

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

return Producer::searchOne(['slug' => $slug]); return Producer::searchOne(['slug' => $slug]);
} }


public function queryProducerActive()
public function queryProducersActive()
{ {
return Producer::find() return Producer::find()
->where([ ->where([
/** /**
* Retourne la liste des établissements pour l'initialisation d'une listesélective. * 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() $producers = Producer::find()
->where([ ->where([

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

public function actionIndex() public function actionIndex()
{ {
return $this->render('index', [ return $this->render('index', [
'producerDemoAccount' => $this->getLogic()->getProducerContainer()
->getRepository()->getOneDemoAccount(),
'producerDemoAccount' => $this->getProducerManager()->findOneProducerDemoAccount(),
'dataProviderPrices' => $this->getDataProviderPrices() 'dataProviderPrices' => $this->getDataProviderPrices()
]); ]);
} }
public function getDataProviderPrices() public function getDataProviderPrices()
{ {
return new ActiveDataProvider([ return new ActiveDataProvider([
'query' => $this->getLogic()->getProducerPriceRangeContainer()
->getRepository()->query(),
'query' => $this->getProducerPriceRangeManager()->queryProducerPriceRanges(),
'pagination' => [ 'pagination' => [
'pageSize' => 100, 'pageSize' => 100,
], ],
public function actionProducers() public function actionProducers()
{ {
$dataProviderProducers = new ActiveDataProvider([ $dataProviderProducers = new ActiveDataProvider([
'query' => $this->getLogic()->getProducerContainer()
->getRepository()->queryActive(),
'query' => $this->getProducerManager()->queryProducersActive(),
'pagination' => [ 'pagination' => [
'pageSize' => 100, 'pageSize' => 100,
], ],
return $this->redirect($returnUrl); return $this->redirect($returnUrl);
} }
else { else {
$userProducerArray = $this->getLogic()
->getUserProducerContainer()
->getRepository()
->getBy(GlobalParam::getCurrentUser());

$userProducerArray = $this->getUserProducerManager()->findUserProducersByUser(GlobalParam::getCurrentUser());
if ($userProducerArray if ($userProducerArray
&& is_array($userProducerArray) && is_array($userProducerArray)
&& count($userProducerArray) == 1) { && count($userProducerArray) == 1) {
public function actionSignup() public function actionSignup()
{ {
$model = new SignupForm(); $model = new SignupForm();
$producerRepository = \Yii::$app->logic->getProducerContainer()->getRepository();
$producerManager = $this->getProducerManager();


if ($model->load(Yii::$app->request->post())) { if ($model->load(Yii::$app->request->post())) {
$user = $model->signup(); $user = $model->signup();
$this->redirect(Yii::$app->urlManagerBackend->createAbsoluteUrl(['site/index'])); $this->redirect(Yii::$app->urlManagerBackend->createAbsoluteUrl(['site/index']));
} }
else { else {
$producer = $producerRepository->getOneById($model->id_producer);
$producer = $producerManager->findOneProducerById($model->id_producer);
if ($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 { else {
$this->redirect(['site/index']); $this->redirect(['site/index']);
} }


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


*/ */
public function actionProducerCode(int $id) public function actionProducerCode(int $id)
{ {
$producerContainer = $this->getLogic()->getProducerContainer();
$producer = $producerContainer->getRepository()->getOneById($id);
$producerManager = $this->getProducerManager();
$producer = $producerManager->findOneProducerById($id);


if (!$producer) { if (!$producer) {
throw new \yii\web\HttpException(404, 'Producteur introuvable'); throw new \yii\web\HttpException(404, 'Producteur introuvable');
if ($producerCodeForm->load($this->getRequest()->post()) if ($producerCodeForm->load($this->getRequest()->post())
&& $producerCodeForm->validate()) { && $producerCodeForm->validate()) {


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


$this->redirect($this->getUrlManagerProducer()->createAbsoluteUrl(['site/index', 'slug_producer' => $producer->slug])); $this->redirect($this->getUrlManagerProducer()->createAbsoluteUrl(['site/index', 'slug_producer' => $producer->slug]));
} }
{ {
$loginForm = new LoginForm(); $loginForm = new LoginForm();
$signupForm = new SignupForm(); $signupForm = new SignupForm();
$producerContainer = $this->getLogic()->getProducerContainer();

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


$this->redirect($returnUrl); $this->redirect($returnUrl);
public function actionProducerOffline(int $id) public function actionProducerOffline(int $id)
{ {
return $this->render('producer_offline', [ 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

*/ */
protected function findModel($id) 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; return $model;
} }
else { else {

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

namespace frontend\forms; namespace frontend\forms;


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


/** /**
*/ */
public function signup() 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()) { if ($this->validate()) {


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


if ($this->isProducer()) { if ($this->isProducer()) {
$idProducer = $this->id_producer; $idProducer = $this->id_producer;
} }


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


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


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


return $user; return $user;


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


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


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


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


return $producer; return $producer;
} }


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


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

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


public function populateProducer($producer) public function populateProducer($producer)

Loading…
Cancel
Save