FirstDataPaymentValidator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 26
c 1
b 0
f 0
dl 0
loc 72
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateOid() 0 15 3
A validateTransactionId() 0 15 3
A validate() 0 19 3
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Glue\FirstData\Validator;
9
10
use Generated\Shared\Transfer\RestCheckoutRequestAttributesTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...questAttributesTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\RestErrorCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ErrorCollectionTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Generated\Shared\Transfer\RestErrorMessageTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\RestErrorMessageTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SprykerEco\Glue\FirstData\FirstDataConfig;
14
use SprykerEco\Shared\FirstData\FirstDataConfig as SharedFirstDataConfig;
15
use Symfony\Component\HttpFoundation\Response;
16
17
class FirstDataPaymentValidator implements FirstDataPaymentValidatorInterface
18
{
19
    /**
20
     * @param \Generated\Shared\Transfer\RestCheckoutRequestAttributesTransfer $restCheckoutRequestAttributesTransfer
21
     *
22
     * @return \Generated\Shared\Transfer\RestErrorCollectionTransfer
23
     */
24
    public function validate(RestCheckoutRequestAttributesTransfer $restCheckoutRequestAttributesTransfer): RestErrorCollectionTransfer
25
    {
26
        $restErrorCollectionTransfer = new RestErrorCollectionTransfer();
27
28
        if (!$restCheckoutRequestAttributesTransfer->getPayments()->count()) {
29
            return $restErrorCollectionTransfer;
30
        }
31
32
        /** @var \Generated\Shared\Transfer\RestPaymentTransfer $restPaymentTransfer */
33
        $restPaymentTransfer = $restCheckoutRequestAttributesTransfer->getPayments()->offsetGet(0);
34
35
        if ($restPaymentTransfer->getPaymentProviderName() !== SharedFirstDataConfig::PAYMENT_PROVIDER_NAME_KEY) {
36
            return $restErrorCollectionTransfer;
37
        }
38
39
        $this->validateOid($restCheckoutRequestAttributesTransfer, $restErrorCollectionTransfer);
40
        $this->validateTransactionId($restCheckoutRequestAttributesTransfer, $restErrorCollectionTransfer);
41
42
        return $restErrorCollectionTransfer;
43
    }
44
45
    /**
46
     * @param \Generated\Shared\Transfer\RestCheckoutRequestAttributesTransfer $restCheckoutRequestAttributesTransfer
47
     * @param \Generated\Shared\Transfer\RestErrorCollectionTransfer $restErrorCollectionTransfer
48
     *
49
     * @return \Generated\Shared\Transfer\RestErrorCollectionTransfer
50
     */
51
    protected function validateOid(
52
        RestCheckoutRequestAttributesTransfer $restCheckoutRequestAttributesTransfer,
53
        RestErrorCollectionTransfer $restErrorCollectionTransfer
54
    ): RestErrorCollectionTransfer {
55
        $firstDataTransactionDataTransfer = $restCheckoutRequestAttributesTransfer->getFirstDataTransactionData();
56
57
        if (!$firstDataTransactionDataTransfer || !$firstDataTransactionDataTransfer->getOid()) {
58
            $restErrorMessageTransfer = (new RestErrorMessageTransfer())
59
                ->setStatus(Response::HTTP_BAD_REQUEST)
60
                ->setCode(FirstDataConfig::RESPONSE_CODE_INVALID_FIRST_DATA_PAYMENT_OID)
61
                ->setDetail(FirstDataConfig::RESPONSE_DETAILS_FIRST_DATA_PAYMENT_OID_MISSING);
62
            $restErrorCollectionTransfer->addRestError($restErrorMessageTransfer);
63
        }
64
65
        return $restErrorCollectionTransfer;
66
    }
67
68
    /**
69
     * @param \Generated\Shared\Transfer\RestCheckoutRequestAttributesTransfer $restCheckoutRequestAttributesTransfer
70
     * @param \Generated\Shared\Transfer\RestErrorCollectionTransfer $restErrorCollectionTransfer
71
     *
72
     * @return \Generated\Shared\Transfer\RestErrorCollectionTransfer
73
     */
74
    protected function validateTransactionId(
75
        RestCheckoutRequestAttributesTransfer $restCheckoutRequestAttributesTransfer,
76
        RestErrorCollectionTransfer $restErrorCollectionTransfer
77
    ): RestErrorCollectionTransfer {
78
        $firstDataTransactionDataTransfer = $restCheckoutRequestAttributesTransfer->getFirstDataTransactionData();
79
80
        if (!$firstDataTransactionDataTransfer || !$firstDataTransactionDataTransfer->getTransactionId()) {
81
            $restErrorMessageTransfer = (new RestErrorMessageTransfer())
82
                ->setStatus(Response::HTTP_BAD_REQUEST)
83
                ->setCode(FirstDataConfig::RESPONSE_CODE_INVALID_FIRST_DATA_PAYMENT_TRANSACTION_ID)
84
                ->setDetail(FirstDataConfig::RESPONSE_DETAILS_FIRST_DATA_PAYMENT_TRANSACTION_ID_MISSING);
85
            $restErrorCollectionTransfer->addRestError($restErrorMessageTransfer);
86
        }
87
88
        return $restErrorCollectionTransfer;
89
    }
90
}
91