<?php
declare(strict_types=1);
namespace Orcamultimedia\OciPunchout\Service;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\Exception\BadCredentialsException;
use Shopware\Core\Checkout\Customer\Exception\CustomerNotFoundException;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Write\CloneBehavior;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\SalesChannelRequest;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class OciService
{
/**
* @var RequestStack
*/
private RequestStack $requestStack;
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @var EntityRepository
*/
private EntityRepository $customerRepository;
public function __construct(
RequestStack $requestStack,
LoggerInterface $logger,
EntityRepository $customerRepository
)
{
$this->requestStack = $requestStack;
$this->logger = $logger;
$this->customerRepository = $customerRepository;
}
public function init(Request $request, RequestDataBag $data): void
{
$user = $data->get('email', $data->get('username', $request->query->get('email', $request->query->get('username'))));
$hookUrl = $data->get('HOOK_URL', $request->query->get('HOOK_URL'));
$returnTarget = $data->get('returntarget', $request->query->get('returntarget'));
$this->logger->info('Initializing new OCI session', ['user' => $user, 'hookUrl' => $hookUrl, 'returnTarget' => $returnTarget, 'requestMethod' => $request->getMethod(), 'requestContent' => $request->getContent(), 'requestQuery' => $request->getQueryString()]);
$request->getSession()->set('oci', ['hookUrl' => $hookUrl, 'returnTarget' => $returnTarget]);
}
public function isPunchout(): bool
{
$master = $this->requestStack->getMainRequest();
if (!$master) {
return false;
}
if (!$master->attributes->get(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST)) {
return false;
}
if (!$master->hasSession()) {
return false;
}
if (!$oci = $master->getSession()->get('oci')) {
return false;
}
if (isset($oci['hookUrl'])) {
return true;
}
return false;
}
public function getCustomerByLogin(string $email, string $password, SalesChannelContext $context): CustomerEntity
{
$customer = $this->getCustomerByEmail($email, $context);
if (!password_verify($password, $customer->getPassword())) {
throw new BadCredentialsException();
}
return $customer;
}
public function getCustomerByEmail(string $email, SalesChannelContext $context): CustomerEntity
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('customer.email', $email));
$criteria->addFilter(new EqualsFilter('customer.guest', 0));
$customers = $this->customerRepository->search($criteria, $context->getContext());
$customerCount = $customers->count();
if ($customerCount === 1) {
return $customers->first();
}
throw new CustomerNotFoundException($email);
}
public function createCustomer(CustomerEntity $customer, Context $context, array $overwrites = []): void
{
$behavior = new CloneBehavior(
$overwrites
);
$this->customerRepository->clone($customer->getId(), $context, null, $behavior);
}
public function getLogger(): LoggerInterface
{
return $this->logger;
}
}