Completed
Push — master ( c8e10c...47e8bc )
by
unknown
21s queued 16s
created

execute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 21
rs 9.8666
cc 4
nc 3
nop 0
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Spryker Marketplace License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerShop\Yves\MerchantSwitcherWidget\Plugin\CustomerPage;
9
10
use Generated\Shared\Transfer\MerchantSwitchRequestTransfer;
11
use Spryker\Yves\Kernel\AbstractPlugin;
12
use SprykerShop\Yves\CustomerPageExtension\Dependency\Plugin\AfterCustomerAuthenticationSuccessPluginInterface;
13
14
/**
15
 * @method \SprykerShop\Yves\MerchantSwitcherWidget\MerchantSwitcherWidgetFactory getFactory()
16
 * @method \SprykerShop\Yves\MerchantSwitcherWidget\MerchantSwitcherWidgetConfig getConfig()
17
 */
18
class MerchantSwitchCartAfterCustomerAuthenticationSuccessPlugin extends AbstractPlugin implements AfterCustomerAuthenticationSuccessPluginInterface
19
{
20
    /**
21
     * {@inheritDoc}
22
     * - Sets merchant reference value to cookies if a customer's quote contains it, and the quote is not empty.
23
     * - If the quote is empty or the quote doesn't contain merchant reference gets merchant reference from cookies and sets to quote.
24
     *
25
     * @api
26
     *
27
     * @return void
28
     */
29
    public function execute(): void
30
    {
31
        if (!$this->getFactory()->getConfig()->isMerchantSwitcherEnabled()) {
32
            return;
33
        }
34
35
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
36
        $merchantReference = $quoteTransfer->getMerchantReference();
37
38
        if ($merchantReference && $quoteTransfer->getItems()->count()) {
39
            $this->getFactory()->createSelectedMerchantCookie()->setMerchantReference($merchantReference);
40
41
            return;
42
        }
43
44
        $merchantReference = $this->getFactory()->createSelectedMerchantCookie()->getMerchantReference();
45
        $merchantSwitchRequestTransfer = (new MerchantSwitchRequestTransfer())
46
            ->setQuote($quoteTransfer)
47
            ->setMerchantReference($merchantReference);
48
49
        $this->getFactory()->getMerchantSwitcherClient()->switchMerchantInQuote($merchantSwitchRequestTransfer);
50
    }
51
}
52