Ver código fonte

Refactoring services #885

refactoring
Guillaume 1 ano atrás
pai
commit
9f302c1840
3 arquivos alterados com 53 adições e 54 exclusões
  1. +6
    -6
      common/logic/PointSale/PointSale/PointSaleRepository.php
  2. +20
    -20
      producer/controllers/CreditController.php
  3. +27
    -28
      producer/controllers/SubscriptionController.php

+ 6
- 6
common/logic/PointSale/PointSale/PointSaleRepository.php Ver arquivo

@@ -21,12 +21,12 @@ class PointSaleRepository extends BaseService implements RepositoryInterface
];
}

public function getOneById(int $id): ?PointSale
public function findOnePointSaleById(int $id): ?PointSale
{
return PointSale::searchOne(['id' => $id]);
}

public function getByDistribution(Distribution $distribution)
public function findPointSalesByDistribution(Distribution $distribution)
{
return PointSale::find()
->with(['pointSaleDistribution' => function ($q) use ($distribution) {
@@ -41,19 +41,19 @@ class PointSaleRepository extends BaseService implements RepositoryInterface
/**
* Retourne le nombre de points de vente pour le producteur courant.
*/
public static function count(): int
public static function countPointSales(): int
{
return PointSale::searchCount(['id_producer' => GlobalParam::getCurrentProducerId()]);
}

public function get()
public function findPointSales()
{
return PointSale::find()
->where('id_producer = ' . GlobalParam::getCurrentProducerId())
->all();;
}

public function queryPublic(Producer $producer)
public function queryPointSalesPublic(Producer $producer)
{
return PointSale::find()
->where([
@@ -68,7 +68,7 @@ class PointSaleRepository extends BaseService implements RepositoryInterface
public function populatePointSaleDropdownList(): array
{
$pointSalesArrayDropdown = ['' => '--'];
$pointSalesArray = $this->get();
$pointSalesArray = $this->findPointSales();

foreach ($pointSalesArray as $pointSale) {
$pointSalesArrayDropdown[$pointSale['id']] = $pointSale['name'];

+ 20
- 20
producer/controllers/CreditController.php Ver arquivo

@@ -94,7 +94,7 @@ class CreditController extends ProducerBaseController
$searchModel = new CreditHistorySearch();
$searchModel->id_user = GlobalParam::getCurrentUserId();
$dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
$userProducer = $this->getLogic()->getUserProducerContainer()->getRepository()->getOne(GlobalParam::getCurrentUser(), $producer);
$userProducer = $this->getUserProducerManager()->findOneUserProducer(GlobalParam::getCurrentUser(), $producer);

if (strlen($returnPayment)) {
if ($returnPayment == 'success') {
@@ -127,7 +127,7 @@ class CreditController extends ProducerBaseController
$user = GlobalParam::getCurrentUser();

\Stripe\Stripe::setApiKey(
$this->getLogic()->getProducerContainer()->getSolver()->getPrivateKeyApiStripe($producer)
$this->getProducerManager()->getPrivateKeyApiStripe($producer)
);

$checkout_session = \Stripe\Checkout\Session::create([
@@ -182,11 +182,11 @@ class CreditController extends ProducerBaseController

public function actionStripeVerification()
{
$creditHistoryBuilder = $this->getLogic()->getCreditHistoryContainer()->getBuilder();
$producerSolver = $this->getLogic()->getProducerContainer()->getSolver();
$userSolver = $this->getLogic()->getUserContainer()->getSolver();
$creditHistoryManager = $this->getCreditHistoryManager();
$producerManager = $this->getProducerManager();
$userManager = $this->getUserManager();
$producer = $this->getProducer();
$contactProducer = $producer->getMainContact();
$contactProducer = $producerManager->getMainContact($producer);

$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
@@ -195,7 +195,7 @@ class CreditController extends ProducerBaseController
$event = \Stripe\Webhook::constructEvent(
$payload,
$sig_header,
$producerSolver->getPrivateKeyEndpointStripe($producer)
$producerManager->getPrivateKeyEndpointStripe($producer)
);
} catch (\UnexpectedValueException $e) {
// Invalid payload
@@ -211,12 +211,12 @@ class CreditController extends ProducerBaseController
$paymentIntentMetadata = $paymentIntent->metadata;
$amount = $paymentIntent->amount / 100;
$idUser = $paymentIntentMetadata->user_id;
$user = $this->getLogic()->getUserContainer()->getRepository()->getOneById($paymentIntentMetadata->user_id);
$user = $userManager->findOneUserById($paymentIntentMetadata->user_id);
$idProducer = $paymentIntentMetadata->producer_id;

if (isset($paymentIntentMetadata->order_id)) {
$order = $this->getOrderManager()->findOneById($paymentIntentMetadata->order_id);
$pointSale = $this->getLogic()->getPointSaleContainer()->getRepostory()->getOneById($order->id_point_sale);
$pointSale = $this->getPointSaleManager()->findOnePointSaleById($order->id_point_sale);
$distribution = $this->getDistributionManager()->getOneById($order->id_distribution);
}

@@ -235,7 +235,7 @@ class CreditController extends ProducerBaseController

if (!$creditHistoryExist) {

$creditHistoryBuilder->create(
$creditHistoryManager->createCreditHistory(
CreditHistory::TYPE_CREDIT,
$amount,
$producer,
@@ -246,7 +246,7 @@ class CreditController extends ProducerBaseController

if (isset($order) && $order) {

$creditHistoryBuilder->create(
$creditHistoryManager->createCreditHistory(
CreditHistory::TYPE_PAYMENT,
$amount,
$producer,
@@ -258,10 +258,10 @@ class CreditController extends ProducerBaseController

// client : envoi d'un email de confirmation de paiement
$paramsEmail = [
'from_email' => $producerSolver->getEmailOpendistrib($producer),
'from_email' => $producerManager->getEmailOpendistrib($producer),
'from_name' => $producer->name,
'to_email' => $user->email,
'to_name' => $userSolver->getUsername($user),
'to_name' => $userManager->getUsername($user),
'subject' => '[' . $producer->name . '] Confirmation de paiement',
'content_view_text' => '@common/mail/paymentOrderConfirm-text.php',
'content_view_html' => '@common/mail/paymentOrderConfirm-html.php',
@@ -275,7 +275,7 @@ class CreditController extends ProducerBaseController

// producteur : mail de confirmation
Mailjet::sendMail([
'from_email' => $producerSolver->getEmailOpendistrib($producer),
'from_email' => $producerManager->getEmailOpendistrib($producer),
'from_name' => $producer->name,
'to_email' => $contactProducer->email,
'to_name' => $contactProducer->name,
@@ -291,13 +291,13 @@ class CreditController extends ProducerBaseController
]
]);
} else {
$userProducer = $this->getLogic()->getUserProducerContainer()->getRepository()->getOne($user, $producer);
$userProducer = $this->getUserProducerManager()->findOneUserProducer($user, $producer);

Mailjet::sendMail([
'from_email' => $producerSolver->getEmailOpendistrib($producer),
'from_email' => $producerManager->getEmailOpendistrib($producer),
'from_name' => $producer->name,
'to_email' => $user->email,
'to_name' => $userSolver->getUsername($user),
'to_name' => $userManager->getUsername($user),
'subject' => '[' . $producer->name . '] Alimentation de votre crédit',
'content_view_text' => '@common/mail/creditConfirm-text.php',
'content_view_html' => '@common/mail/creditConfirm-html.php',
@@ -317,10 +317,10 @@ class CreditController extends ProducerBaseController

// client
Mailjet::sendMail([
'from_email' => $producerSolver->getEmailOpendistrib($producer),
'from_email' => $producerManager->getEmailOpendistrib($producer),
'from_name' => $producer->name,
'to_email' => $user->email,
'to_name' => $userSolver->getUsername($user),
'to_name' => $userManager->getUsername($user),
'subject' => '[' . $producer->name . '] Erreur de paiement',
'content_view_text' => '@common/mail/paymentError-text.php',
'content_view_html' => '@common/mail/paymentError-html.php',
@@ -334,7 +334,7 @@ class CreditController extends ProducerBaseController
// producteur
if (isset($order) && $order) {
Mailjet::sendMail([
'from_email' => $producerSolver->getEmailOpendistrib($producer),
'from_email' => $producerManager->getEmailOpendistrib($producer),
'from_name' => $producer->name,
'to_email' => $contactProducer->email,
'to_name' => $contactProducer->name,

+ 27
- 28
producer/controllers/SubscriptionController.php Ver arquivo

@@ -90,7 +90,7 @@ class SubscriptionController extends ProducerBaseController

public function actionAjaxProcess()
{
$subscriptionRepository = $this->getSubscriptionContainer()->getRepository();
$subscriptionManager = $this->getSubscriptionManager();
$model = new SubscriptionForm();
$model->id_producer = GlobalParam::getCurrentProducerId();
$model->id_user = GlobalParam::getCurrentUserId();
@@ -100,7 +100,7 @@ class SubscriptionController extends ProducerBaseController
$idSubscription = (int)$posts['idSubscription'];
$isUpdate = false;
if ($idSubscription) {
$subscription = $subscriptionRepository->getOneById($idSubscription);
$subscription = $subscriptionManager->findOneSubscriptionById($idSubscription);

if ($subscription) {
$model->id = $idSubscription;
@@ -110,7 +110,7 @@ class SubscriptionController extends ProducerBaseController

if ($model->load($posts) && $model->validate() && $model->save()) {

$subscription = $subscriptionRepository->getOneById($model->id);
$subscription = $subscriptionManager->findOneSubscriptionById($model->id);
$this->getOrderManager()->updateOrdersIncomingDistributionsFromSubscription($subscription, $isUpdate);

if ($isUpdate) {
@@ -146,9 +146,9 @@ class SubscriptionController extends ProducerBaseController
return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id]));
}

$subscriptionRepository = $this->getSubscriptionContainer()->getRepository();
$subscriptionManager = $this->getSubscriptionManager();
$model = new SubscriptionForm;
$subscription = $subscriptionRepository->getOneById($id);
$subscription = $subscriptionManager->findOneSubscriptionById($id);

if ($subscription) {
$model->id = $id;
@@ -183,7 +183,7 @@ class SubscriptionController extends ProducerBaseController
throw new NotFoundHttpException('L\'abonnement est introuvable.', 404);
}

$productsArray = $this->getProductContainer()->getRepository()->get();
$productsArray = $this->getProductManager()->findProducts();

if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
if (!strlen($model->date_end)) {
@@ -192,8 +192,8 @@ class SubscriptionController extends ProducerBaseController
if ($model->save()) {
$this->setFlash('success', 'Abonnement modifié');

$subscription = $subscriptionRepository->getOneById($model->id);
$matchedDistributionsArray = $subscriptionRepository->searchMatchedIncomingDistributions($subscription);
$subscription = $subscriptionManager->findOneSubscriptionById($model->id);
$matchedDistributionsArray = $this->getDistributionManager()->findDistributionsIncomingMatchWithSubscrtiption($subscription);

if (count($matchedDistributionsArray)) {
return $this->redirect(
@@ -225,9 +225,9 @@ class SubscriptionController extends ProducerBaseController
return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id]));
}

$subscriptionContainer = $this->getSubscriptionContainer();
$subscription = $subscriptionContainer->getRepository()->getOneById($id);
$subscriptionContainer->getBuilder()->delete($subscription);
$subscriptionManager = $this->getSubscriptionManager();
$subscription = $subscriptionManager->findOneSubscriptionById($id);
$subscriptionManager->deleteSubscription($subscription);

// @TODO : gérer via événements
$this->getOrderManager()->deleteOrdersIncomingDistributionsFromSubscription($subscription);
@@ -241,10 +241,10 @@ class SubscriptionController extends ProducerBaseController
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$params = [];
$productSolver = $this->getProductContainer()->getSolver();
$subscriptionRepositpry = $this->getSubscriptionContainer()->getRepository();
$productManager = $this->getProductManager();
$subscriptionManager = $this->getSubscriptionManager();
$user = GlobalParam::getCurrentUser();
$userProducer = $this->getUserProducerContainer()->getRepository()->getOne(
$userProducer = $this->getUserProducerManager()->findOneUserProducer(
GlobalParam::getCurrentUser(),
GlobalParam::getCurrentProducer()
);
@@ -255,15 +255,14 @@ class SubscriptionController extends ProducerBaseController
'id_subscription' => $idSubscription
]);

$subscription = $subscriptionRepositpry->getOneById($idSubscription);
$subscription = $subscriptionManager->findOneSubscriptionById($idSubscription);
if ($subscription && $subscription->id_point_sale) {
$pointSale = $this->getPointSaleContainer()->getRepostory()
->getOneById($subscription->id_point_sale);
$pointSale = $this->getPointSaleManager()->findOnePointSaleById($subscription->id_point_sale);
}
}

// Produits
$productsArray = $this->getProductContainer()->getRepository()->get();
$productsArray = $productManager->findProducts();
$indexProduct = 0;
foreach ($productsArray as &$product) {
$quantity = 0;
@@ -283,9 +282,9 @@ class SubscriptionController extends ProducerBaseController
'index' => $indexProduct++,
'quantity_form' => $quantity,
'coefficient_unit' => $coefficientUnit,
'wording_unit' => $productSolver->strUnit($product->unit, 'wording_unit', true),
'wording_short' => $productSolver->strUnit($product->unit, 'wording_short'),
'price_with_tax' => $productSolver->getPriceWithTax($product, [
'wording_unit' => $productManager->strUnit($product->unit, 'wording_unit', true),
'wording_short' => $productManager->strUnit($product->unit, 'wording_short'),
'price_with_tax' => $productManager->getPriceWithTax($product, [
'user' => $user,
'user_producer' => $userProducer,
'point_sale' => $pointSale
@@ -296,7 +295,7 @@ class SubscriptionController extends ProducerBaseController

$params['products'] = $productsArray;
$pointsSaleArray = $this->getPointSaleContainer()->getRepostory()->get();
$pointsSaleArray = $this->getPointSaleManager()->findPointSales();
foreach ($pointsSaleArray as &$pointSale) {
$pointSale = array_merge($pointSale->getAttributes(), [
'userPointSale' => ($pointSale->userPointSale ? $pointSale->userPointSale[0] : '')
@@ -309,7 +308,7 @@ class SubscriptionController extends ProducerBaseController
$params['points_sale'] = $pointsSaleArray;

if ($idSubscription > 0) {
$subscription = $subscriptionRepositpry->getOneById($idSubscription);
$subscription = $subscriptionManager->findOneSubscriptionById($idSubscription);

if (!$subscription || $subscription->id_user != GlobalParam::getCurrentUserId()) {
throw new UserException('Abonnement introuvable');
@@ -327,13 +326,13 @@ class SubscriptionController extends ProducerBaseController
public function actionAjaxValidateCodePointSale($idPointSale, $code)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$pointSaleManager = $this->getPointSaleManager();
$pointSale = $pointSaleManager->findOnePointSaleById($idPointSale);

$pointSale = $this->getPointSaleContainer()->getRepostory()->getOneById($idPointSale);
if ($pointSale) {
if ($this->getPointSaleContainer()->getSolver()->validateCode($pointSale, $code)) {
return 1;
}
if ($pointSale && $pointSaleManager->validateCode($pointSale, $code)) {
return 1;
}

return 0;
}
}

Carregando…
Cancelar
Salvar