Passed
Push — master ( 7a2782...032b3c )
by Christian
11:03
created

LogoutRoute   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDecorated() 0 3 1
B logout() 0 35 7
A deleteContextToken() 0 6 1
A __construct() 0 12 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Customer\SalesChannel;
4
5
use Doctrine\DBAL\Connection;
6
use OpenApi\Annotations as OA;
7
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
8
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
9
use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
10
use Shopware\Core\Framework\Feature;
11
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
12
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
13
use Shopware\Core\Framework\Util\Random;
14
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
15
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
16
use Shopware\Core\System\SalesChannel\ContextTokenResponse;
17
use Shopware\Core\System\SalesChannel\NoContentResponse;
18
use Shopware\Core\System\SalesChannel\SalesChannelContext;
19
use Shopware\Core\System\SystemConfig\SystemConfigService;
20
use Symfony\Component\Routing\Annotation\Route;
21
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22
23
/**
24
 * @RouteScope(scopes={"store-api"})
25
 */
26
class LogoutRoute extends AbstractLogoutRoute
27
{
28
    /**
29
     * @var SalesChannelContextPersister
30
     */
31
    private $contextPersister;
32
33
    /**
34
     * @var EventDispatcherInterface
35
     */
36
    private $eventDispatcher;
37
38
    /**
39
     * @var SystemConfigService
40
     */
41
    private $systemConfig;
42
43
    /**
44
     * @var CartService
45
     */
46
    private $cartService;
47
48
    /**
49
     * @var Connection
50
     */
51
    private $connection;
52
53
    public function __construct(
54
        SalesChannelContextPersister $contextPersister,
55
        EventDispatcherInterface $eventDispatcher,
56
        SystemConfigService $systemConfig,
57
        CartService $cartService,
58
        Connection $connection
59
    ) {
60
        $this->contextPersister = $contextPersister;
61
        $this->eventDispatcher = $eventDispatcher;
62
        $this->systemConfig = $systemConfig;
63
        $this->cartService = $cartService;
64
        $this->connection = $connection;
65
    }
66
67
    public function getDecorated(): AbstractLogoutRoute
68
    {
69
        throw new DecorationPatternException(self::class);
70
    }
71
72
    /**
73
     * @OA\Post(
74
     *      path="/account/logout",
75
     *      description="Logouts current loggedin customer",
76
     *      operationId="logoutCustomer",
77
     *      tags={"Store API", "Account"},
78
     *      @OA\Response(
79
     *          response="200",
80
     *          description=""
81
     *     )
82
     * )
83
     * @Route(path="/store-api/v{version}/account/logout", name="store-api.account.logout", methods={"POST"})
84
     */
85
    public function logout(SalesChannelContext $context, ?RequestDataBag $data = null)
86
    {
87
        if (!$context->getCustomer()) {
88
            throw new CustomerNotLoggedInException();
89
        }
90
91
        $salesChannelId = $context->getSalesChannel()->getId();
92
        if ($this->systemConfig->get('core.loginRegistration.invalidateSessionOnLogOut', $salesChannelId)) {
93
            $this->cartService->deleteCart($context);
94
            $this->deleteContextToken($context->getToken());
0 ignored issues
show
Deprecated Code introduced by
The function Shopware\Core\Checkout\C...e::deleteContextToken() has been deprecated: tag:v6.3.0 use \Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister::delete ( Ignorable by Annotation )

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

94
            /** @scrutinizer ignore-deprecated */ $this->deleteContextToken($context->getToken());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
95
        } elseif (Feature::isActive('FEATURE_NEXT_10058')) {
96
            $newToken = Random::getAlphanumericString(32);
97
98
            if ($data && (bool) $data->get('replace-token')) {
99
                $newToken = $this->contextPersister->replace($context->getToken(), $context);
100
            }
101
102
            $context->assign([
103
                'token' => $newToken,
104
            ]);
105
        } else {
106
            $this->contextPersister->save(
107
                $context->getToken(),
108
                [
109
                    'customerId' => null,
110
                    'billingAddressId' => null,
111
                    'shippingAddressId' => null,
112
                ]
113
            );
114
        }
115
116
        $event = new CustomerLogoutEvent($context, $context->getCustomer());
117
        $this->eventDispatcher->dispatch($event);
118
119
        return Feature::isActive('FEATURE_NEXT_10058') ? new ContextTokenResponse($context->getToken()) : new NoContentResponse();
120
    }
121
122
    /**
123
     * @deprecated tag:v6.3.0 use \Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister::delete
124
     */
125
    private function deleteContextToken(string $token): void
126
    {
127
        $this->connection->executeUpdate(
128
            'DELETE FROM sales_channel_api_context WHERE token = :token',
129
            [
130
                'token' => $token,
131
            ]
132
        );
133
    }
134
}
135