Completed
Push — master ( 56a686...736967 )
by Oleksandr
15s queued 11s
created

getDirectDebitRegistration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 2
nc 2
nop 1
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\Zed\Heidelpay\Business\Payment\DirectDebit\Registration;
9
10
use Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...bitRegistrationTransfer 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;
1 ignored issue
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 Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
13
use SprykerEco\Zed\Heidelpay\Persistence\HeidelpayEntityManagerInterface;
14
use SprykerEco\Zed\Heidelpay\Persistence\HeidelpayRepositoryInterface;
15
16
class DirectDebitRegistrationWriter implements DirectDebitRegistrationWriterInterface
17
{
18
    use TransactionTrait;
19
20
    /**
21
     * @var \SprykerEco\Zed\Heidelpay\Persistence\HeidelpayEntityManagerInterface
22
     */
23
    protected $entityManager;
24
25
    /**
26
     * @var \SprykerEco\Zed\Heidelpay\Persistence\HeidelpayRepositoryInterface
27
     */
28
    protected $repository;
29
30
    /**
31
     * @param \SprykerEco\Zed\Heidelpay\Persistence\HeidelpayEntityManagerInterface $entityManager
32
     * @param \SprykerEco\Zed\Heidelpay\Persistence\HeidelpayRepositoryInterface $repository
33
     */
34
    public function __construct(
35
        HeidelpayEntityManagerInterface $entityManager,
36
        HeidelpayRepositoryInterface $repository
37
    ) {
38
        $this->entityManager = $entityManager;
39
        $this->repository = $repository;
40
    }
41
42
    /**
43
     * @param \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer $directDebitRegistrationTransfer
44
     *
45
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
46
     */
47
    public function createDirectDebitRegistration(
48
        HeidelpayDirectDebitRegistrationTransfer $directDebitRegistrationTransfer
49
    ): HeidelpayDirectDebitRegistrationTransfer {
50
        return $this->getTransactionHandler()->handleTransaction(
51
            function () use ($directDebitRegistrationTransfer) {
52
                return $this->savePaymentHeidelpayDirectDebitRegistrationEntity($directDebitRegistrationTransfer);
53
            }
54
        );
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
59
     *
60
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
61
     */
62
    public function updateDirectDebitRegistration(QuoteTransfer $quoteTransfer): HeidelpayDirectDebitRegistrationTransfer
63
    {
64
        return $this->getTransactionHandler()->handleTransaction(
65
            function () use ($quoteTransfer) {
66
                $directDebitRegistrationTransfer = $this->getDirectDebitRegistration($quoteTransfer);
67
68
                if ($directDebitRegistrationTransfer->getIdDirectDebitRegistration() === null) {
69
                    return $directDebitRegistrationTransfer;
70
                }
71
72
                $directDebitRegistrationTransfer->setIdCustomerAddress(
73
                    $quoteTransfer->getShippingAddress()->getIdCustomerAddress()
74
                );
75
76
                return $this->savePaymentHeidelpayDirectDebitRegistrationEntity($directDebitRegistrationTransfer);
77
            }
78
        );
79
    }
80
81
    /**
82
     * @param \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer $directDebitRegistrationTransfer
83
     *
84
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
85
     */
86
    protected function savePaymentHeidelpayDirectDebitRegistrationEntity(
87
        HeidelpayDirectDebitRegistrationTransfer $directDebitRegistrationTransfer
88
    ): HeidelpayDirectDebitRegistrationTransfer {
89
        return $this->entityManager->savePaymentHeidelpayDirectDebitRegistrationEntity($directDebitRegistrationTransfer);
90
    }
91
92
    /**
93
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
94
     *
95
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
96
     */
97
    protected function getDirectDebitRegistration(QuoteTransfer $quoteTransfer): HeidelpayDirectDebitRegistrationTransfer
98
    {
99
        $transactionUniqueId = $quoteTransfer
100
            ->getPayment()
101
            ->getHeidelpayDirectDebit()
102
            ->getSelectedRegistration()
103
            ->getRegistrationUniqueId();
104
105
        $directDebitRegistrationTransfer = $this->repository
106
            ->findHeidelpayDirectDebitRegistrationByRegistrationUniqueId($transactionUniqueId);
107
108
        if ($directDebitRegistrationTransfer === null) {
109
            return new HeidelpayDirectDebitRegistrationTransfer();
110
        }
111
112
        return $directDebitRegistrationTransfer;
113
    }
114
}
115