Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit 26bc0cc

Browse files
Add ErrorCode form type and update ErrorCode middleware
1 parent ce4d89c commit 26bc0cc

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Dotpayds project.
5+
* (c) Krzysztof Piasecki <krzysiekpiasecki@gmail.com>
6+
*
7+
* @license https://opensource.org/licenses/MIT The MIT License
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
/*
13+
* This file is part of Dotpayds project.
14+
* (c) Krzysztof Piasecki <krzysiekpiasecki@gmail.com>
15+
*
16+
* @license https://opensource.org/licenses/MIT The MIT License
17+
*/
18+
19+
namespace Dotpay\Response;
20+
21+
use Dotpay\Response\Validator\Constraint\ErrorCodeConstraint;
22+
use Symfony\Component\Form\Extension\Core\Type\BaseType;
23+
use Symfony\Component\Form\Extension\Core\Type\TextType;
24+
use Symfony\Component\Form\FormBuilderInterface;
25+
use Symfony\Component\OptionsResolver\OptionsResolver;
26+
27+
class ResponseErrorCodeType extends BaseType
28+
{
29+
public function configureOptions(OptionsResolver $resolver)
30+
{
31+
$resolver->setDefault('data_class', ResponseErrorCodeBag::class);
32+
}
33+
34+
public function buildForm(FormBuilderInterface $builder, array $options)
35+
{
36+
$builder
37+
->add(
38+
'error_code',
39+
TextType::class,
40+
[
41+
'constraints' => [
42+
new ErrorCodeConstraint(),
43+
],
44+
]
45+
);
46+
}
47+
}

src/Dotpay/Server/ErrorCode.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818

1919
namespace Dotpay\Server;
2020

21+
use Dotpay\Response\ResponseErrorCodeBag;
2122
use Psr\Http\Message\ResponseInterface;
2223
use Psr\Http\Message\ServerRequestInterface;
2324
use Psr\Http\Server\MiddlewareInterface;
2425
use Psr\Http\Server\RequestHandlerInterface;
2526
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
27+
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
28+
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
29+
use Symfony\Component\Form\Forms;
30+
use Symfony\Component\Validator\Validation;
2631

2732
class ErrorCode implements MiddlewareInterface
2833
{
@@ -41,7 +46,27 @@ public function process(
4146
): ResponseInterface {
4247
$httpFoundationFactory = new HttpFoundationFactory();
4348
$symfonyRequest = $httpFoundationFactory->createRequest($request);
49+
$formFactory = Forms::createFormFactoryBuilder()
50+
->addExtension(new HttpFoundationExtension())
51+
->addExtension(new ValidatorExtension(Validation::createValidator()))
52+
->getFormFactory();
53+
54+
$responseErrorCodeBag = new ResponseErrorCodeBag();
55+
$form = $formFactory->createNamed(
56+
null,
57+
ResponseErrorCodeBag::class,
58+
$responseErrorCodeBag
59+
);
60+
61+
$form->submit([
62+
'error_code' => $request->getAttribute('error_code')
63+
]);
64+
65+
if (!$form->isValid()) {
66+
throw new \RuntimeException('Invalid form');
67+
}
4468

4569
$handler->handle($request);
70+
4671
}
4772
}

0 commit comments

Comments
 (0)