validateThenMapAttributes()
last analyzed

Size

Total Lines 1

Duplication

Lines 1
Ratio 100 %

Importance

Changes 0
Metric Value
dl 1
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace AmmitPhp\Ammit\UI\Resolver;
5
6
use AmmitPhp\Ammit\UI\Resolver\Validator\RequestAttributeValueValidator;
7
use AmmitPhp\Ammit\UI\Resolver\Validator\RawValueValidator;
8
use AmmitPhp\Ammit\UI\Resolver\Exception\CommandMappingException;
9
use AmmitPhp\Ammit\UI\Resolver\Exception\UIValidationCollectionException;
10
use AmmitPhp\Ammit\UI\Resolver\Validator\RequestQueryStringValueValidator;
11
use Psr\Http\Message\ServerRequestInterface;
12
13
/**
14
 * Helper easing Command Resolver (mapping + UI Validation) implementation
15
 */
16 View Code Duplication
abstract class AbstractPureCommandResolver
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /** @var UIValidationEngine */
19
    protected $validationEngine;
20
21
    /** @var RawValueValidator */
22
    protected $rawValueValidator;
23
24
    /** @var RequestAttributeValueValidator */
25
    protected $attributeValueValidator;
26
27
    /** @var RequestQueryStringValueValidator */
28
    protected $queryStringValueValidator;
29
30
    public function __construct(UIValidationEngine $validationEngine = null, RawValueValidator $rawValueValidator = null, RequestAttributeValueValidator $attributeValueValidator = null, RequestQueryStringValueValidator $queryStringValueValidator = null)
31
    {
32 1
        if (null === $validationEngine) {
33 1
            $validationEngine = UIValidationEngine::initialize();
34
        }
35
36 1
        if (null === $rawValueValidator) {
37 1
            $rawValueValidator = new RawValueValidator($validationEngine);
38
        }
39
40 1
        if (null === $attributeValueValidator) {
41 1
            $attributeValueValidator = new RequestAttributeValueValidator(
42
                $rawValueValidator
43
            );
44
        }
45
46 1
        if (null === $queryStringValueValidator) {
47 1
            $queryStringValueValidator = new RequestQueryStringValueValidator(
48
                $rawValueValidator
49
            );
50
        }
51
52 1
        $this->validationEngine = $validationEngine;
53 1
        $this->rawValueValidator = $rawValueValidator;
54 1
        $this->attributeValueValidator = $attributeValueValidator;
55 1
        $this->queryStringValueValidator = $queryStringValueValidator;
56 1
    }
57
58
    /**
59
     * @api
60
     * Create a Command from a Request
61
     * Perform the UI Validation (simple validation)
62
     * Complex Validation will be done in the Domain
63
     * @param ServerRequestInterface $request PSR7 Request
64
     *
65
     * @return object Immutable Command (DTO)
66
     * @throws UIValidationCollectionException If any Validation fail
67
     */
68
    abstract public function resolve(ServerRequestInterface $request);
69
70
    /**
71
     * @api
72
     * Map a Command attributes from a Request into an array
73
     * Perform the UI Validation (simple validation)
74
     *
75
     * @return mixed[] Attributes used to create the Command
76
     * @throws CommandMappingException If any mapping validation failed
77
     * @throws UIValidationCollectionException If any UI validation failed
78
     */
79
    protected function resolveRequestAsArray(ServerRequestInterface $request): array
80
    {
81 1
        $values = $this->validateThenMapAttributes(
82
            $request
83
        );
84
85 1
        $this->validationEngine->guardAgainstAnyUIValidationException();
86
87 1
        return $values;
88
    }
89
90
    /**
91
     * @api
92
     * Resolve implementation
93
     * @param ServerRequestInterface $request PSR-7 Request
94
     *
95
     * @return mixed[]
96
     */
97
    abstract protected function validateThenMapAttributes(ServerRequestInterface $request): array;
98
}
99