Passed
Push — master ( 4f556b...efe1f1 )
by Christian
10:35 queued 10s
created

LogoutRoute   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDecorated() 0 3 1
A logout() 0 26 5
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\Plugin\Exception\DecorationPatternException;
11
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
12
use Shopware\Core\Framework\Routing\Annotation\Since;
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\SalesChannelContext;
18
use Shopware\Core\System\SystemConfig\SystemConfigService;
19
use Symfony\Component\Routing\Annotation\Route;
20
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21
22
/**
23
 * @RouteScope(scopes={"store-api"})
24
 */
25
class LogoutRoute extends AbstractLogoutRoute
26
{
27
    /**
28
     * @var SalesChannelContextPersister
29
     */
30
    private $contextPersister;
31
32
    /**
33
     * @var EventDispatcherInterface
34
     */
35
    private $eventDispatcher;
36
37
    /**
38
     * @var SystemConfigService
39
     */
40
    private $systemConfig;
41
42
    /**
43
     * @var CartService
44
     */
45
    private $cartService;
46
47
    /**
48
     * @var Connection
49
     */
50
    private $connection;
51
52
    public function __construct(
53
        SalesChannelContextPersister $contextPersister,
54
        EventDispatcherInterface $eventDispatcher,
55
        SystemConfigService $systemConfig,
56
        CartService $cartService,
57
        Connection $connection
58
    ) {
59
        $this->contextPersister = $contextPersister;
60
        $this->eventDispatcher = $eventDispatcher;
61
        $this->systemConfig = $systemConfig;
62
        $this->cartService = $cartService;
63
        $this->connection = $connection;
64
    }
65
66
    public function getDecorated(): AbstractLogoutRoute
67
    {
68
        throw new DecorationPatternException(self::class);
69
    }
70
71
    /**
72
     * @Since("6.2.0.0")
73
     * @OA\Post(
74
     *      path="/account/logout",
75
     *      summary="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.4.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
        } else {
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
        }
106
107
        $event = new CustomerLogoutEvent($context, $context->getCustomer());
108
        $this->eventDispatcher->dispatch($event);
109
110
        return new ContextTokenResponse($context->getToken());
111
    }
112
113
    /**
114
     * @deprecated tag:v6.4.0 use \Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister::delete
115
     */
116
    private function deleteContextToken(string $token): void
117
    {
118
        $this->connection->executeUpdate(
119
            'DELETE FROM sales_channel_api_context WHERE token = :token',
120
            [
121
                'token' => $token,
122
            ]
123
        );
124
    }
125
}
126