Passed
Push — master ( 99d0e8...9e7049 )
by Ondra
03:35
created

Request::setParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Pixidos package.
5
 *
6
 *  (c) Ondra Votava <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 *
11
 */
12
13
declare(strict_types=1);
14
15
namespace Pixidos\GPWebPay\Data;
16
17
use Pixidos\GPWebPay\Exceptions\InvalidArgumentException;
18
use Pixidos\GPWebPay\Param\DepositFlag;
19
use Pixidos\GPWebPay\Param\IParam;
20
use Pixidos\GPWebPay\Param\MerchantNumber;
21
use Pixidos\GPWebPay\Param\Utils\DigestParamsFilter;
22
use Pixidos\GPWebPay\Param\Utils\Sorter;
23
use UnexpectedValueException;
24
25
class Request implements RequestInterface
26
{
27
    private OperationInterface $operation;
28
    /**
29
     * @var array<string, string> $params
30
     */
31
    private array $params = [];
32
33
    private string $url;
34
35
    /**
36
     * @param OperationInterface $operation
37
     * @param MerchantNumber     $merchantNumber
38
     * @param DepositFlag        $depositFlag
39
     * @param string             $url
40
     *
41
     * @throws InvalidArgumentException
42
     * @throws UnexpectedValueException
43
     */
44 8
    public function __construct(
45
        OperationInterface $operation,
46
        MerchantNumber $merchantNumber,
47
        DepositFlag $depositFlag,
48
        string $url
49
    ) {
50 8
        $this->operation = $operation;
51 8
        $this->url = $url;
52 8
        $this->setParam($merchantNumber);
53 8
        $this->setParam($depositFlag);
54
55 8
        $this->setParams();
56
    }
57
58
    /**
59
     * @return array<string, string>
60
     */
61 8
    public function getParams(): array
62
    {
63 8
        return $this->params;
64
    }
65
66 8
    public function sortParams(): void
67
    {
68 8
        $params = Sorter::sortRequestParams($this->params);
69 8
        $this->params = $params;
70
    }
71
72
73
    /**
74
     * @return array<string, string>
75
     */
76 7
    public function getDigestParams(): array
77
    {
78 7
        $this->sortParams();
79
80 7
        return DigestParamsFilter::filter($this->params);
81
    }
82
83
    /**
84
     * @param IParam $param
85
     */
86 8
    public function setParam(IParam $param): void
87
    {
88 8
        $this->params[$param->getParamName()] = (string)$param;
89
    }
90
91 6
    public function getRequestUrl(bool $asPost = false): string
92
    {
93 6
        if ($asPost) {
94
            return $this->url;
95
        }
96
97 6
        return $this->url . '?' . http_build_query($this->getParams());
98
    }
99
100
101
    /**
102
     * Sets params to array
103
     */
104 8
    private function setParams(): void
105
    {
106 8
        foreach ($this->operation->getParams() as $param) {
107 8
            $this->setParam($param);
108
        }
109
    }
110
}
111