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

Operation::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 4
nop 5
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
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\Enum\Operation as OperationEnum;
18
use Pixidos\GPWebPay\Enum\Param as ParamEnum;
19
use Pixidos\GPWebPay\Exceptions\InvalidArgumentException;
20
use Pixidos\GPWebPay\Param\Currency;
21
use Pixidos\GPWebPay\Param\IAmount;
22
use Pixidos\GPWebPay\Param\IParam;
23
use Pixidos\GPWebPay\Param\Md;
24
use Pixidos\GPWebPay\Param\Operation as OperationParam;
25
use Pixidos\GPWebPay\Param\OrderNumber;
26
use Pixidos\GPWebPay\Param\ResponseUrl;
27
28
/**
29
 * Class Operation
30
 * @package Pixidos\GPWebPay
31
 * @author  Ondra Votava <[email protected]>
32
 */
33
class Operation implements OperationInterface
34
{
35
    private ?string $gateway = null;
36
37
    /**
38
     * @var IParam[]
39
     */
40
    private array $params = [];
41
42
43 8
    public function __construct(
44
        OrderNumber $orderNumber,
45
        IAmount $amount,
46
        Currency $currency,
47
        ?string $gateway = null,
48
        ?ResponseUrl $responseUrl = null
49
    ) {
50 8
        $this->addParam(new OperationParam(OperationEnum::CREATE_ORDER()));
51 8
        $this->addParam($amount);
52 8
        $this->addParam($orderNumber);
53 8
        $this->addParam($currency);
54
55 8
        if (null !== $gateway) {
56 4
            $gateway = strtolower($gateway);
57 4
            $this->gateway = $gateway;
58 4
            $this->addParam(new Md($gateway));
59
        }
60
61 8
        if (null !== $responseUrl) {
62 2
            $this->addParam($responseUrl);
63
        }
64
    }
65
66 9
    public function getGateway(): ?string
67
    {
68 9
        return $this->gateway;
69
    }
70
71
    /**
72
     * @param IParam $param
73
     *
74
     * @return $this
75
     * @throws InvalidArgumentException
76
     */
77 8
    public function addParam(IParam $param): OperationInterface
78
    {
79 8
        if (($param instanceof Md) && $this->gateway !== (string)$param) {
80 2
            $param = new Md($this->gateway . '|' . $param);
81
        }
82
83 8
        $this->params[$param->getParamName()] = $param;
84
85 8
        return $this;
86
    }
87
88
    /**
89
     * @param ParamEnum $param
90
     *
91
     * @return IParam|null
92
     */
93 9
    public function getParam(ParamEnum $param): ?IParam
94
    {
95 9
        return $this->params[(string)$param] ?? null;
96
    }
97
98
    /**
99
     * @return IParam[]
100
     */
101 8
    public function getParams(): array
102
    {
103 8
        return $this->params;
104
    }
105
}
106