custom/plugins/OrcaOci/src/Service/OciService.php line 80

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Orcamultimedia\OciPunchout\Service;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Checkout\Customer\Exception\BadCredentialsException;
  7. use Shopware\Core\Checkout\Customer\Exception\CustomerNotFoundException;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Write\CloneBehavior;
  13. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  14. use Shopware\Core\SalesChannelRequest;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. class OciService
  19. {
  20.     /**
  21.      * @var RequestStack
  22.      */
  23.     private RequestStack $requestStack;
  24.     /**
  25.      * @var LoggerInterface
  26.      */
  27.     private LoggerInterface $logger;
  28.     /**
  29.      * @var EntityRepository
  30.      */
  31.     private EntityRepository $customerRepository;
  32.     public function __construct(
  33.         RequestStack              $requestStack,
  34.         LoggerInterface           $logger,
  35.         EntityRepository $customerRepository
  36.     )
  37.     {
  38.         $this->requestStack $requestStack;
  39.         $this->logger $logger;
  40.         $this->customerRepository $customerRepository;
  41.     }
  42.     public function init(Request $requestRequestDataBag $data): void
  43.     {
  44.         $user $data->get('email'$data->get('username'$request->query->get('email'$request->query->get('username'))));
  45.         $hookUrl $data->get('HOOK_URL'$request->query->get('HOOK_URL'));
  46.         $returnTarget $data->get('returntarget'$request->query->get('returntarget'));
  47.         $this->logger->info('Initializing new OCI session', ['user' => $user'hookUrl' => $hookUrl'returnTarget' => $returnTarget'requestMethod' => $request->getMethod(), 'requestContent' => $request->getContent(), 'requestQuery' => $request->getQueryString()]);
  48.         $request->getSession()->set('oci', ['hookUrl' => $hookUrl'returnTarget' => $returnTarget]);
  49.     }
  50.     public function isPunchout(): bool
  51.     {
  52.         $master $this->requestStack->getMainRequest();
  53.         if (!$master) {
  54.             return false;
  55.         }
  56.         if (!$master->attributes->get(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST)) {
  57.             return false;
  58.         }
  59.         if (!$master->hasSession()) {
  60.             return false;
  61.         }
  62.         if (!$oci $master->getSession()->get('oci')) {
  63.             return false;
  64.         }
  65.         if (isset($oci['hookUrl'])) {
  66.             return true;
  67.         }
  68.         return false;
  69.     }
  70.     public function getCustomerByLogin(string $emailstring $passwordSalesChannelContext $context): CustomerEntity
  71.     {
  72.         $customer $this->getCustomerByEmail($email$context);
  73.         if (!password_verify($password$customer->getPassword())) {
  74.             throw new BadCredentialsException();
  75.         }
  76.         return $customer;
  77.     }
  78.     public function getCustomerByEmail(string $emailSalesChannelContext $context): CustomerEntity
  79.     {
  80.         $criteria = new Criteria();
  81.         $criteria->addFilter(new EqualsFilter('customer.email'$email));
  82.         $criteria->addFilter(new EqualsFilter('customer.guest'0));
  83.         $customers $this->customerRepository->search($criteria$context->getContext());
  84.         $customerCount $customers->count();
  85.         if ($customerCount === 1) {
  86.             return $customers->first();
  87.         }
  88.         throw new CustomerNotFoundException($email);
  89.     }
  90.     public function createCustomer(CustomerEntity $customerContext $context, array $overwrites = []): void
  91.     {
  92.         $behavior = new CloneBehavior(
  93.             $overwrites
  94.         );
  95.         $this->customerRepository->clone($customer->getId(), $contextnull$behavior);
  96.     }
  97.     public function getLogger(): LoggerInterface
  98.     {
  99.         return $this->logger;
  100.     }
  101. }