Operation::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 2
b 0
f 0
nc 8
nop 6
dl 0
loc 24
ccs 13
cts 13
cp 1
crap 4
rs 9.8666
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
        ?OperationEnum $operation = null
50
    ) {
51 8
        if ($operation === null) {
52 8
            $operation = OperationEnum::CREATE_ORDER();
53
        }
54 8
        $this->addParam(new OperationParam($operation));
55 8
        $this->addParam($amount);
56 8
        $this->addParam($orderNumber);
57 8
        $this->addParam($currency);
58
59 8
        if (null !== $gateway) {
60 4
            $gateway = strtolower($gateway);
61 4
            $this->gateway = $gateway;
62 4
            $this->addParam(new Md($gateway));
63
        }
64
65 8
        if (null !== $responseUrl) {
66 2
            $this->addParam($responseUrl);
67
        }
68
    }
69
70 9
    public function getGateway(): ?string
71
    {
72 9
        return $this->gateway;
73
    }
74
75
    /**
76
     * @param IParam $param
77
     *
78
     * @return $this
79
     * @throws InvalidArgumentException
80
     */
81 8
    public function addParam(IParam $param): OperationInterface
82
    {
83 8
        if (($param instanceof Md) && $this->gateway !== (string)$param) {
84 2
            $param = new Md($this->gateway . '|' . $param);
85
        }
86
87 8
        $this->params[$param->getParamName()] = $param;
88
89 8
        return $this;
90
    }
91
92
    /**
93
     * @param ParamEnum $param
94
     *
95
     * @return IParam|null
96
     */
97 9
    public function getParam(ParamEnum $param): ?IParam
98
    {
99 9
        return $this->params[(string)$param] ?? null;
100
    }
101
102
    /**
103
     * @return IParam[]
104
     */
105 8
    public function getParams(): array
106
    {
107 8
        return $this->params;
108
    }
109
}
110