AbstractPureCommandResolver::resolve()
last analyzed

Size

Total Lines 1

Duplication

Lines 1
Ratio 100 %

Importance

Changes 0
Metric Value
dl 1
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\UI\Resolver;
5
6
use Imedia\Ammit\UI\Resolver\Validator\RequestAttributeValueValidator;
7
use Imedia\Ammit\UI\Resolver\Validator\RawValueValidator;
8
use Imedia\Ammit\UI\Resolver\Exception\CommandMappingException;
9
use Imedia\Ammit\UI\Resolver\Exception\UIValidationCollectionException;
10
use Imedia\Ammit\UI\Resolver\Validator\RequestQueryStringValueValidator;
11
use Psr\Http\Message\ServerRequestInterface;
12
13
/**
14
 * Helper easing Command Resolver (mapping + UI Validation) implementation
15
 *
16
 * @author Guillaume MOREL <[email protected]>
17
 */
18 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...
19
{
20
    /** @var UIValidationEngine */
21
    protected $validationEngine;
22
23
    /** @var RawValueValidator */
24
    protected $rawValueValidator;
25
26
    /** @var RequestAttributeValueValidator */
27
    protected $attributeValueValidator;
28
29
    /** @var RequestQueryStringValueValidator */
30
    protected $queryStringValueValidator;
31
32
    public function __construct(UIValidationEngine $validationEngine = null, RawValueValidator $rawValueValidator = null, RequestAttributeValueValidator $attributeValueValidator = null, RequestQueryStringValueValidator $queryStringValueValidator = null)
33
    {
34 1
        if (null === $validationEngine) {
35 1
            $validationEngine = UIValidationEngine::initialize();
36
        }
37
38 1
        if (null === $rawValueValidator) {
39 1
            $rawValueValidator = new RawValueValidator($validationEngine);
40
        }
41
42 1
        if (null === $attributeValueValidator) {
43 1
            $attributeValueValidator = new RequestAttributeValueValidator(
44
                $rawValueValidator
45
            );
46
        }
47
48 1
        if (null === $queryStringValueValidator) {
49 1
            $queryStringValueValidator = new RequestQueryStringValueValidator(
50
                $rawValueValidator
51
            );
52
        }
53
54 1
        $this->validationEngine = $validationEngine;
55 1
        $this->rawValueValidator = $rawValueValidator;
56 1
        $this->attributeValueValidator = $attributeValueValidator;
57 1
        $this->queryStringValueValidator = $queryStringValueValidator;
58 1
    }
59
60
    /**
61
     * @api
62
     * Create a Command from a Request
63
     * Perform the UI Validation (simple validation)
64
     * Complex Validation will be done in the Domain
65
     * @param ServerRequestInterface $request PSR7 Request
66
     *
67
     * @return object Immutable Command (DTO)
68
     * @throws UIValidationCollectionException If any Validation fail
69
     */
70
    abstract public function resolve(ServerRequestInterface $request);
71
72
    /**
73
     * @api
74
     * Map a Command attributes from a Request into an array
75
     * Perform the UI Validation (simple validation)
76
     *
77
     * @return mixed[] Attributes used to create the Command
78
     * @throws CommandMappingException If any mapping validation failed
79
     * @throws UIValidationCollectionException If any UI validation failed
80
     */
81
    protected function resolveRequestAsArray(ServerRequestInterface $request): array
82
    {
83 1
        $values = $this->validateThenMapAttributes(
84
            $request
85
        );
86
87 1
        $this->validationEngine->guardAgainstAnyUIValidationException();
88
89 1
        return $values;
90
    }
91
92
    /**
93
     * @api
94
     * Resolve implementation
95
     * @param ServerRequestInterface $request PSR-7 Request
96
     *
97
     * @return mixed[]
98
     */
99
    abstract protected function validateThenMapAttributes(ServerRequestInterface $request): array;
100
}
101