CustomerNewsletterMapper::resolveOperationType()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
c 0
b 0
f 0
rs 10
cc 3
nc 3
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\Episerver\Business\Mapper\Customer;
9
10
use Generated\Shared\Transfer\EpiserverRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\EpiserverRequestTransfer 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\MailTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\MailTransfer 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\Newsletter\Communication\Plugin\Mail\NewsletterSubscribedMailTypePlugin;
0 ignored issues
show
Bug introduced by
The type Spryker\Zed\Newsletter\C...ubscribedMailTypePlugin 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 Spryker\Zed\Newsletter\Communication\Plugin\Mail\NewsletterUnsubscribedMailTypePlugin;
0 ignored issues
show
Bug introduced by
The type Spryker\Zed\Newsletter\C...ubscribedMailTypePlugin 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...
14
15
class CustomerNewsletterMapper extends AbstractCustomerMapper
16
{
17
    /**
18
     * @param \Generated\Shared\Transfer\MailTransfer $mailTransfer
19
     * @param \Generated\Shared\Transfer\EpiserverRequestTransfer $requestTransfer
20
     *
21
     * @return \Generated\Shared\Transfer\EpiserverRequestTransfer
22
     */
23
    public function mapMailTransferToEpiserverRequestTransfer(MailTransfer $mailTransfer, EpiserverRequestTransfer $requestTransfer): EpiserverRequestTransfer
24
    {
25
        if ($mailTransfer->getType() === null) {
26
            return $requestTransfer;
27
        }
28
29
        $requestTransfer->setAuthorizationCode($this->config->getCustomerNewsLetterListAuthorizationCode());
30
        $requestTransfer->setOperationType($this->resolveOperationType($mailTransfer->getType()));
31
        $requestTransfer->setPayload($this->buildPayload($mailTransfer));
32
33
        return $requestTransfer;
34
    }
35
36
    /**
37
     * @param \Generated\Shared\Transfer\MailTransfer $mailTransfer
38
     *
39
     * @return array
40
     */
41
    protected function buildPayload(MailTransfer $mailTransfer): array
42
    {
43
        $customerTransfer = $mailTransfer->getCustomer();
44
45
        $payload = [
46
            static::KEY_CUSTOMER_SHOP_URL => $this->config->getHostYves(),
47
            static::KEY_CUSTOMER_LOGIN_URL => $this->config->getHostYves() . static::URL_LOGIN,
48
            static::KEY_CUSTOMER_SHOP_LOCALE => $this->getLocale($customerTransfer),
49
        ];
50
51
        $optInId = null;
52
53
        if ($mailTransfer->getType() !== null) {
54
            $optInId = $this->getMailingIdByMailType($mailTransfer->getType());
55
        }
56
57
        if ($optInId !== null) {
58
            $payload[static::KEY_OPT_IN_ID] = $optInId;
59
        }
60
61
        if ($customerTransfer !== null) {
62
            $payload[static::KEY_SALUTATION] = $customerTransfer->getSalutation();
63
            $payload[static::KEY_FIRST_NAME] = $customerTransfer->getFirstName();
64
            $payload[static::KEY_LAST_NAME] = $customerTransfer->getLastName();
65
            $payload[static::KEY_SPRYKER_ID] = $customerTransfer->getIdCustomer();
66
            $payload[static::KEY_CUSTOMER_RESET_LINK] = $customerTransfer->getRestorePasswordLink();
67
            $payload[static::KEY_EMAIL] = $customerTransfer->getEmail();
68
        }
69
70
        $newsletterSubscriber = $mailTransfer->getNewsletterSubscriber();
71
72
        if ($newsletterSubscriber !== null) {
73
            $payload[static::KEY_EMAIL] = $newsletterSubscriber->getEmail();
74
            $payload[static::KEY_CUSTOMER_SUBSCRIBER_KEY] = $newsletterSubscriber->getSubscriberKey();
75
        }
76
77
        if ($mailTransfer->getType() === NewsletterUnsubscribedMailTypePlugin::MAIL_TYPE) {
78
            $payload[static::KEY_REMOVE_ID] = true;
79
        }
80
81
        return $payload;
82
    }
83
84
    /**
85
     * @param string|null $mailTypeName
86
     *
87
     * @return string
88
     */
89
    protected function resolveOperationType(?string $mailTypeName): string
90
    {
91
        if ($mailTypeName === NewsletterSubscribedMailTypePlugin::MAIL_TYPE) {
92
            return $this->config->getOperationSubscribeEventEmail();
93
        }
94
95
        if ($mailTypeName === NewsletterUnsubscribedMailTypePlugin::MAIL_TYPE) {
96
            return $this->config->getOperationUnsubscribeEventEmail();
97
        }
98
99
        return $this->config->getOperationUpdateFieldsEventEmail();
100
    }
101
}
102