CustomerContext::getCustomer()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
nc 3
cc 4
nop 0
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Context;
13
14
use Sylius\Component\Customer\Context\CustomerContextInterface;
15
use Sylius\Component\Customer\Model\CustomerInterface;
16
use Sylius\Component\User\Model\UserInterface;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
19
20
class CustomerContext implements CustomerContextInterface
21
{
22
    /**
23
     * @var TokenStorageInterface
24
     */
25
    private $tokenStorage;
26
27
    /**
28
     * @var AuthorizationCheckerInterface
29
     */
30
    private $authorizationChecker;
31
32
    /**
33
     * @param TokenStorageInterface         $tokenStorage
34
     * @param AuthorizationCheckerInterface $authorizationChecker
35
     */
36
    public function __construct(TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $authorizationChecker)
37
    {
38
        $this->tokenStorage = $tokenStorage;
39
        $this->authorizationChecker = $authorizationChecker;
40
    }
41
42
    /**
43
     * Gets customer based on currently logged user.
44
     *
45
     * @return CustomerInterface|null
46
     */
47
    public function getCustomer(): ?CustomerInterface
48
    {
49
        if (null === $token = $this->tokenStorage->getToken()) {
50
            return null;
51
        }
52
53
        if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') && $token->getUser() instanceof UserInterface) {
54
            return $token->getUser()->getCustomer();
55
        }
56
57
        return null;
58
    }
59
}
60