Passed
Push — master ( ef0fdb...1d76f0 )
by
unknown
03:02
created

shouldTransactionBeExecuted()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 15
rs 9.6111
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\Yves\CrefoPay\Quote;
9
10
use Generated\Shared\Transfer\CrefoPayTransactionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...oPayTransactionTransfer 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\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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 SprykerEco\Client\CrefoPay\CrefoPayClientInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
15
class CrefoPayQuoteExpander implements CrefoPayQuoteExpanderInterface
16
{
17
    /**
18
     * @var \SprykerEco\Client\CrefoPay\CrefoPayClientInterface
19
     */
20
    protected CrefoPayClientInterface $client;
21
22
    /**
23
     * @param \SprykerEco\Client\CrefoPay\CrefoPayClientInterface $client
24
     */
25
    public function __construct(CrefoPayClientInterface $client)
26
    {
27
        $this->client = $client;
28
    }
29
30
    /**
31
     * @param \Symfony\Component\HttpFoundation\Request $request
32
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
33
     *
34
     * @return \Generated\Shared\Transfer\QuoteTransfer
35
     */
36
    public function expand(Request $request, QuoteTransfer $quoteTransfer): QuoteTransfer
37
    {
38
        if (!$this->shouldTransactionBeExecuted($quoteTransfer)) {
39
            return $quoteTransfer;
40
        }
41
42
        $quoteTransfer->setCrefoPayTransaction(
43
            $this->createCrefoPayTransactionTransfer($request),
44
        );
45
46
        return $this->client->startCrefoPayTransaction($quoteTransfer);
47
    }
48
49
    /**
50
     * @param \Symfony\Component\HttpFoundation\Request $request
51
     *
52
     * @return \Generated\Shared\Transfer\CrefoPayTransactionTransfer
53
     */
54
    protected function createCrefoPayTransactionTransfer(Request $request): CrefoPayTransactionTransfer
55
    {
56
        return (new CrefoPayTransactionTransfer())
57
            ->setClientIp($request->getClientIp());
58
    }
59
60
    /**
61
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
62
     *
63
     * @return bool
64
     */
65
    protected function shouldTransactionBeExecuted(QuoteTransfer $quoteTransfer): bool
66
    {
67
        $crefoPayTransaction = $quoteTransfer->getCrefoPayTransaction();
68
        if (
69
            $crefoPayTransaction === null
70
            || !$crefoPayTransaction->getIsSuccess()
71
        ) {
72
            return true;
73
        }
74
75
        if (!$quoteTransfer->getBillingAddress() || !$crefoPayTransaction->getBillingAddress()) {
76
            return true;
77
        }
78
79
        return $quoteTransfer->getBillingAddressOrFail()->getIso2Code() !== $crefoPayTransaction->getBillingAddressOrFail()->getCountry();
80
    }
81
}
82