|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?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, $flush = true): UserMerchantInterface
- {
- $userMerchant = $this->userMerchantStore
- ->setMerchant($merchant)
- ->getOneByUser($user);
-
- if (!$userMerchant) {
- $userMerchantFactory = new UserMerchantFactory();
- $userMerchant = $userMerchantFactory->create($merchant, $user);
- $this->entityManager->create($userMerchant);
- if($flush) {
- $this->entityManager->flush();
- }
- }elseif(!$userMerchant->getActive()){
- $userMerchant->setActive(true);
- $this->entityManager->update($userMerchant);
- if($flush) {
- $this->entityManager->flush();
- }
- }
-
- return $userMerchant;
- }
-
- public function init(
- UserInterface $user,
- MerchantInterface $merchant,
- bool $active = true,
- bool $creditActive = false,
- float $credit = null,
- bool $persist = true
- ): UserMerchantInterface {
- $userMerchant = $this->createIfNotExist($user, $merchant);
-
- $userMerchant->setActive($active);
- $userMerchant->setCreditActive($creditActive);
- $userMerchant->setCredit($credit);
-
- if ($persist) {
- //TODO create ou update ???
- $this->entityManager->persist($userMerchant);
- $this->entityManager->flush();
- }
-
- return $userMerchant;
- }
-
- public function activeCredit(UserMerchantInterface $userMerchant): UserMerchantInterface
- {
- return $this->updateCreditActive($userMerchant, true);
- }
-
- public function unactiveCredit(UserMerchantInterface $userMerchant): UserMerchantInterface
- {
- return $this->updateCreditActive($userMerchant, false);
- }
-
- public function updateCredit(
- UserMerchantInterface $userMerchant,
- CreditHistoryInterface $creditHistory,
- float $amount
- ): UserMerchantInterface {
- $userMerchant->setCredit($amount);
-
- $this->entityManager->update($creditHistory);
- $this->entityManager->update($userMerchant);
- $this->entityManager->flush();
-
- return $userMerchant;
- }
-
- public function updateCreditActive(
- UserMerchantInterface $userMerchant,
- $creditActive = true
- ): UserMerchantInterface {
-
- $userMerchant->setCreditActive($creditActive);
-
- //TODO create ou update ???
- $this->entityManager->persist($userMerchant);
- $this->entityManager->flush();
-
- return $userMerchant;
- }
-
- }
-
|