|
- <?php
-
- namespace Lc\CaracoleBundle\Builder\User;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Factory\User\UserMerchantFactory;
- use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
- use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
- use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
- use Lc\CaracoleBundle\Repository\User\UserMerchantStore;
- use Lc\SovBundle\Model\User\UserInterface;
-
- class UserMerchantBuilder
- {
- protected EntityManagerInterface $entityManager;
- protected UserMerchantStore $userMerchantStore;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- UserMerchantStore $userMerchantStore
- ) {
- $this->entityManager = $entityManager;
- $this->userMerchantStore = $userMerchantStore;
- }
-
- public function createIfNotExist(UserInterface $user, MerchantInterface $merchant): UserMerchantInterface
- {
- $userMerchant = $this->userMerchantStore
- ->setMerchant($merchant)
- ->getOneByUser($user);
-
- if (!$userMerchant) {
- $userMerchantFactory = new UserMerchantFactory();
- $userMerchant = $userMerchantFactory
- ->setMerchant($merchant)
- ->create($user);
- }
-
- return $userMerchant;
- }
-
- public function init(UserInterface $user, MerchantInterface $merchant, bool $active = true, bool $creditActive = false, float $credit = null, bool $persist = true)
- {
- $userMerchant = $this->createIfNotExist($user, $merchant);
-
- $userMerchant->setActive($active);
- $userMerchant->setCreditActive($creditActive) ;
- $userMerchant->setCredit($credit) ;
-
- if($persist) {
- $this->entityManager->persist($userMerchant);
- $this->entityManager->flush();
- }
-
- return $userMerchant;
- }
-
- public function updateCreditActive(
- UserInterface $user,
- MerchantInterface $merchant,
- $creditActive = true
- ): UserMerchantInterface {
-
- $userMerchant = $this->createIfNotExist($user, $merchant);
-
- $userMerchant->setCreditActive($creditActive);
-
- $this->entityManager->persist($userMerchant);
- $this->entityManager->flush();
-
- return $userMerchant;
- }
-
- public function activeCredit(UserInterface $user, MerchantInterface $merchant): UserMerchantInterface
- {
- return $this->updateCreditActive($user, $merchant, true);
- }
-
- public function unactiveCredit(UserInterface $user, MerchantInterface $merchant): UserMerchantInterface
- {
- return $this->updateCreditActive($user, $merchant, false);
- }
-
- public function updateCredit(
- UserMerchantInterface $userMerchant,
- CreditHistoryInterface $creditHistory,
- float $amount
- ): UserMerchantInterface {
- $userMerchant->setCredit($amount);
-
- $this->entityManager->persist($creditHistory);
- $this->entityManager->persist($userMerchant);
- $this->entityManager->flush();
-
- return $userMerchant;
- }
-
- }
|