Passed
Push — master ( 24f31d...92d63d )
by Christian
13:07 queued 26s
created

LogoutRoute::deleteContextToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Customer\SalesChannel;
4
5
use OpenApi\Annotations as OA;
6
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
7
use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
8
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
9
use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
10
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
11
use Shopware\Core\Framework\Routing\Annotation\Since;
12
use Shopware\Core\Framework\Util\Random;
13
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
14
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
15
use Shopware\Core\System\SalesChannel\ContextTokenResponse;
16
use Shopware\Core\System\SalesChannel\SalesChannelContext;
17
use Shopware\Core\System\SystemConfig\SystemConfigService;
18
use Symfony\Component\Routing\Annotation\Route;
19
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20
21
/**
22
 * @RouteScope(scopes={"store-api"})
23
 */
24
class LogoutRoute extends AbstractLogoutRoute
25
{
26
    /**
27
     * @var SalesChannelContextPersister
28
     */
29
    private $contextPersister;
30
31
    /**
32
     * @var EventDispatcherInterface
33
     */
34
    private $eventDispatcher;
35
36
    /**
37
     * @var SystemConfigService
38
     */
39
    private $systemConfig;
40
41
    /**
42
     * @var CartService
43
     */
44
    private $cartService;
45
46
    public function __construct(
47
        SalesChannelContextPersister $contextPersister,
48
        EventDispatcherInterface $eventDispatcher,
49
        SystemConfigService $systemConfig,
50
        CartService $cartService
51
    ) {
52
        $this->contextPersister = $contextPersister;
53
        $this->eventDispatcher = $eventDispatcher;
54
        $this->systemConfig = $systemConfig;
55
        $this->cartService = $cartService;
56
    }
57
58
    public function getDecorated(): AbstractLogoutRoute
59
    {
60
        throw new DecorationPatternException(self::class);
61
    }
62
63
    /**
64
     * @Since("6.2.0.0")
65
     * @OA\Post(
66
     *      path="/account/logout",
67
     *      summary="Logouts current loggedin customer",
68
     *      operationId="logoutCustomer",
69
     *      tags={"Store API", "Account"},
70
     *      @OA\Response(
71
     *          response="200",
72
     *          description=""
73
     *     )
74
     * )
75
     * @LoginRequired()
76
     * @Route(path="/store-api/v{version}/account/logout", name="store-api.account.logout", methods={"POST"})
77
     */
78
    public function logout(SalesChannelContext $context, ?RequestDataBag $data = null): ContextTokenResponse
79
    {
80
        if ($this->shouldDelete($context)) {
81
            $this->cartService->deleteCart($context);
82
            $this->contextPersister->delete($context->getToken());
83
84
            $event = new CustomerLogoutEvent($context, $context->getCustomer());
0 ignored issues
show
Bug introduced by
It seems like $context->getCustomer() can also be of type null; however, parameter $customer of Shopware\Core\Checkout\C...outEvent::__construct() does only seem to accept Shopware\Core\Checkout\Customer\CustomerEntity, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            $event = new CustomerLogoutEvent($context, /** @scrutinizer ignore-type */ $context->getCustomer());
Loading history...
85
            $this->eventDispatcher->dispatch($event);
86
87
            return new ContextTokenResponse($context->getToken());
88
        }
89
90
        $newToken = Random::getAlphanumericString(32);
91
        if ($data && (bool) $data->get('replace-token')) {
92
            $newToken = $this->contextPersister->replace($context->getToken(), $context);
93
        }
94
95
        $context->assign([
96
            'token' => $newToken,
97
        ]);
98
99
        $event = new CustomerLogoutEvent($context, $context->getCustomer());
100
        $this->eventDispatcher->dispatch($event);
101
102
        return new ContextTokenResponse($context->getToken());
103
    }
104
105
    private function shouldDelete(SalesChannelContext $context): bool
106
    {
107
        $config = $this->systemConfig->get('core.loginRegistration.invalidateSessionOnLogOut', $context->getSalesChannelId());
108
109
        if ($config) {
110
            return true;
111
        }
112
113
        if ($context->getCustomer() === null) {
114
            return true;
115
        }
116
117
        return $context->getCustomer()->getGuest();
118
    }
119
}
120