Skip to content

Commit

Permalink
Move classes from Payum\Core\Bridge\Symfony into PayumBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredup committed Apr 3, 2024
1 parent 522642d commit a183720
Show file tree
Hide file tree
Showing 40 changed files with 1,092 additions and 31 deletions.
72 changes: 72 additions & 0 deletions Action/GetHttpRequestAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Payum\Bundle\PayumBundle\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Request\GetHttpRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class GetHttpRequestAction implements ActionInterface
{
/**
* @var Request
*/
protected $httpRequest;

/**
* @var RequestStack
*/
protected $httpRequestStack;

/**
* @deprecated
*/
public function setHttpRequest(Request $httpRequest = null): void
{
$this->httpRequest = $httpRequest;
}

public function setHttpRequestStack(RequestStack $httpRequestStack = null): void
{
$this->httpRequestStack = $httpRequestStack;
}

public function execute($request): void
{
/** @var GetHttpRequest $request */
RequestNotSupportedException::assertSupports($this, $request);

if ($this->httpRequest instanceof Request) {
$this->updateRequest($request, $this->httpRequest);
} elseif ($this->httpRequestStack instanceof RequestStack) {
# BC Layer for Symfony 4 (Simplify after support for Symfony < 5 is dropped)
if (method_exists($this->httpRequestStack, 'getMainRequest')) {
$mainRequest = $this->httpRequestStack->getMainRequest();
} else {
$mainRequest = $this->httpRequestStack->getMasterRequest();
}
if (null !== $mainRequest) {
$this->updateRequest($request, $mainRequest);
}
}
}

public function supports($request)
{
return $request instanceof GetHttpRequest;
}

protected function updateRequest(GetHttpRequest $request, Request $httpRequest): void
{
$request->query = $httpRequest->query->all();
$request->request = $httpRequest->request->all();
$request->headers = $httpRequest->headers->all();
$request->method = $httpRequest->getMethod();
$request->uri = $httpRequest->getUri();
$request->clientIp = $httpRequest->getClientIp();
$request->userAgent = $httpRequest->headers->get('User-Agent');
$request->content = $httpRequest->getContent();
}
}
132 changes: 132 additions & 0 deletions Action/ObtainCreditCardAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace Payum\Bundle\PayumBundle\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Bundle\PayumBundle\Form\Type\CreditCardType;
use Payum\Bundle\PayumBundle\Reply\HttpResponse;
use Payum\Core\Exception\LogicException;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Model\CreditCardInterface;
use Payum\Core\Request\ObtainCreditCard;
use Payum\Core\Request\RenderTemplate;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

class ObtainCreditCardAction implements ActionInterface, GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* @var FormFactoryInterface
*/
protected $formFactory;

/**
* @var ?Request
*/
protected $httpRequest;

/**
* @var RequestStack
*/
protected $httpRequestStack;

/**
* @var string
*/
protected $templateName;

/**
* @param string $templateName
*/
public function __construct(FormFactoryInterface $formFactory, $templateName)
{
$this->formFactory = $formFactory;
$this->templateName = $templateName;
}

/**
* @deprecated
*/
public function setRequest(Request $request = null): void
{
$this->httpRequest = $request;
}

public function setRequestStack(RequestStack $requestStack = null): void
{
$this->httpRequestStack = $requestStack;
}

/**
* @param ObtainCreditCard $request
*/
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);

$httpRequest = null;
if ($this->httpRequest instanceof Request) {
$httpRequest = $this->httpRequest;
} elseif ($this->httpRequestStack instanceof RequestStack) {
# BC Layer for Symfony 4 (Simplify after support for Symfony < 5 is dropped)
if (method_exists($this->httpRequestStack, 'getMainRequest')) {
$httpRequest = $this->httpRequestStack->getMainRequest();
} else {
$httpRequest = $this->httpRequestStack->getMasterRequest();
}
}

if (! $httpRequest) {
throw new LogicException('The action can be run only when http request is set.');
}

$form = $this->createCreditCardForm();

$form->handleRequest($httpRequest);
if ($form->isSubmitted()) {
/** @var CreditCardInterface $card */
$card = $form->getData();
$card->secure();

if ($form->isValid()) {
$request->set($card);

return;
}
}

$renderTemplate = new RenderTemplate($this->templateName, [
'model' => $request->getModel(),
'firstModel' => $request->getFirstModel(),
'form' => $form->createView(),
'actionUrl' => $request->getToken() ? $request->getToken()->getTargetUrl() : null,
]);
$this->gateway->execute($renderTemplate);

throw new HttpResponse(new Response($renderTemplate->getResult(), 200, [
'Cache-Control' => 'no-store, no-cache, max-age=0, post-check=0, pre-check=0',
'X-Status-Code' => 200,
'Pragma' => 'no-cache',
]));
}

public function supports($request)
{
return $request instanceof ObtainCreditCard;
}

/**
* @return FormInterface
*/
protected function createCreditCardForm()
{
return $this->formFactory->create(CreditCardType::class);
}
}
57 changes: 57 additions & 0 deletions Action/RenderTemplateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Payum\Bundle\PayumBundle\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Request\RenderTemplate;
use Symfony\Component\Templating\EngineInterface;

class RenderTemplateAction implements ActionInterface
{
/**
* @var string
*/
protected $layout;

private EngineInterface $templating;

public function __construct(EngineInterface $templating, $layout = null)
{
$this->templating = $templating;
$this->layout = $layout;
}

/**
* @param mixed $request
*
* @throws RequestNotSupportedException if the action dose not support the request.
*/
public function execute($request): void
{
/** @var RenderTemplate $request */
RequestNotSupportedException::assertSupports($this, $request);

$request->setResult(
$this->templating->render(
$request->getTemplateName(),
array_replace(
[
'layout' => $this->layout,
],
$request->getParameters()
)
)
);
}

/**
* @param mixed $request
*
* @return boolean
*/
public function supports($request)
{
return $request instanceof RenderTemplate;
}
}
29 changes: 29 additions & 0 deletions Builder/CoreGatewayFactoryBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Payum\Bundle\PayumBundle\Builder;

use Payum\Bundle\PayumBundle\ContainerAwareCoreGatewayFactory;
use Payum\Core\GatewayFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

class CoreGatewayFactoryBuilder implements ContainerAwareInterface
{
use ContainerAwareTrait;

public function __invoke()
{
return call_user_func_array([$this, 'build'], func_get_args());
}

/**
* @return GatewayFactoryInterface
*/
public function build(array $defaultConfig)
{
$coreGatewayFactory = new ContainerAwareCoreGatewayFactory($defaultConfig);
$coreGatewayFactory->setContainer($this->container);

return $coreGatewayFactory;
}
}
36 changes: 36 additions & 0 deletions Builder/GatewayFactoryBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Payum\Bundle\PayumBundle\Builder;

use Payum\Core\GatewayFactoryInterface;

class GatewayFactoryBuilder
{
/**
* @var string
*/
private $gatewayFactoryClass;

/**
* @param string $gatewayFactoryClass
*/
public function __construct($gatewayFactoryClass)
{
$this->gatewayFactoryClass = $gatewayFactoryClass;
}

public function __invoke()
{
return call_user_func_array([$this, 'build'], func_get_args());
}

/**
* @return GatewayFactoryInterface
*/
public function build(array $defaultConfig, GatewayFactoryInterface $coreGatewayFactory)
{
$gatewayFactoryClass = $this->gatewayFactoryClass;

return new $gatewayFactoryClass($defaultConfig, $coreGatewayFactory);
}
}
24 changes: 24 additions & 0 deletions Builder/HttpRequestVerifierBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Payum\Bundle\PayumBundle\Builder;

use Payum\Bundle\PayumBundle\Security\HttpRequestVerifier;
use Payum\Core\Security\HttpRequestVerifierInterface;
use Payum\Core\Security\TokenInterface;
use Payum\Core\Storage\StorageInterface;

class HttpRequestVerifierBuilder
{
public function __invoke(): HttpRequestVerifierInterface
{
return $this->build(...func_get_args());
}

/**
* @param StorageInterface<TokenInterface> $tokenStorage
*/
public function build(StorageInterface $tokenStorage): HttpRequestVerifierInterface
{
return new HttpRequestVerifier($tokenStorage);
}
}
37 changes: 37 additions & 0 deletions Builder/ObtainCreditCardActionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Payum\Bundle\PayumBundle\Builder;

use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Bundle\PayumBundle\Action\ObtainCreditCardAction;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class ObtainCreditCardActionBuilder
{
private FormFactoryInterface $formFactory;

private RequestStack $requestStack;

public function __construct(FormFactoryInterface $formFactory, RequestStack $requestStack)
{
$this->formFactory = $formFactory;
$this->requestStack = $requestStack;
}

public function __invoke()
{
return call_user_func_array([$this, 'build'], func_get_args());
}

/**
* @return ObtainCreditCardAction
*/
public function build(ArrayObject $config)
{
$action = new ObtainCreditCardAction($this->formFactory, $config['payum.template.obtain_credit_card']);
$action->setRequestStack($this->requestStack);

return $action;
}
}
Loading

0 comments on commit a183720

Please sign in to comment.