Passed
Push — master ( b40bc2...29ef41 )
by Mr
07:40
created

PaymentServiceValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 10
c 0
b 0
f 0
nc 1
nop 10
dl 0
loc 22
ccs 0
cts 22
cp 0
crap 2
rs 9.9332

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/money-interop project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Money\Validator;
10
11
use Assert\Assert;
12
use Daikon\Boot\Middleware\Action\ValidatorInterface;
13
use Daikon\Boot\Middleware\Action\ValidatorTrait;
14
use Daikon\Boot\Middleware\ActionHandler;
15
use Daikon\Money\Service\PaymentServiceInterface;
16
use Daikon\Money\Service\PaymentServiceMap;
17
18
final class PaymentServiceValidator implements ValidatorInterface
19
{
20
    use ValidatorTrait;
0 ignored issues
show
Bug introduced by
The trait Daikon\Boot\Middleware\Action\ValidatorTrait requires the property $import which is not provided by Daikon\Money\Validator\PaymentServiceValidator.
Loading history...
21
22
    private PaymentServiceMap $paymentServiceMap;
23
24
    private string $input;
25
26
    /** @var null|bool|string */
27
    private $export;
28
29
    /** @var mixed */
30
    private $default;
31
32
    private bool $required;
33
34
    private int $severity;
35
36
    private string $payload;
37
38
    private string $exportErrors;
39
40
    private string $exportErrorCode;
41
42
    private string $exportErrorSeverity;
43
44
    /**
45
     * @param mixed $export
46
     * @param mixed $default
47
     */
48
    public function __construct(
49
        PaymentServiceMap $paymentServiceMap,
50
        string $input,
51
        $export = null,
52
        $default = null,
53
        bool $required = true,
54
        int $severity = self::SEVERITY_ERROR,
55
        string $payload = ActionHandler::ATTR_PAYLOAD,
56
        string $exportErrors = ActionHandler::ATTR_ERRORS,
57
        string $exportErrorCode = ActionHandler::ATTR_ERROR_CODE,
58
        string $exportErrorSeverity = ActionHandler::ATTR_ERROR_SEVERITY
59
    ) {
60
        $this->paymentServiceMap = $paymentServiceMap;
61
        $this->input = $input;
62
        $this->export = $export;
63
        $this->default = $default;
64
        $this->required = $required;
65
        $this->severity = $severity;
66
        $this->payload = $payload;
67
        $this->exportErrors = $exportErrors;
68
        $this->exportErrorCode = $exportErrorCode;
69
        $this->exportErrorSeverity = $exportErrorSeverity;
70
    }
71
72
    /** @param mixed $input */
73
    private function validate(string $name, $input): PaymentServiceInterface
74
    {
75
        Assert::that($input)
76
            ->string('Must be a string.')
77
            ->notBlank('Must not be empty.')
78
            ->satisfy([$this->paymentServiceMap, 'has'], 'Unknown service.');
79
80
        /** @var PaymentServiceInterface $service  */
81
        $service = $this->paymentServiceMap->get($input);
82
83
        return $service;
84
    }
85
}
86