Completed
Push — master ( bfa6c3...34b432 )
by Kamil
20:43 queued 09:51
created

UserImpersonatedListener::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ShopBundle\EventListener;
15
16
use Sylius\Bundle\UserBundle\Event\UserEvent;
17
use Sylius\Component\Channel\Context\ChannelContextInterface;
18
use Sylius\Component\Core\Model\ChannelInterface;
19
use Sylius\Component\Core\Model\OrderInterface;
20
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
21
use Sylius\Component\Core\Storage\CartStorageInterface;
22
23
final class UserImpersonatedListener
24
{
25
    /**
26
     * @var CartStorageInterface
27
     */
28
    private $cartStorage;
29
30
    /**
31
     * @var ChannelContextInterface
32
     */
33
    private $channelContext;
34
35
    /**
36
     * @var OrderRepositoryInterface
37
     */
38
    private $orderRepository;
39
40
    /**
41
     * @param CartStorageInterface $cartStorage
42
     * @param ChannelContextInterface $channelContext
43
     * @param OrderRepositoryInterface $orderRepository
44
     */
45
    public function __construct(
46
        CartStorageInterface $cartStorage,
47
        ChannelContextInterface $channelContext,
48
        OrderRepositoryInterface $orderRepository
49
    ) {
50
        $this->cartStorage = $cartStorage;
51
        $this->channelContext = $channelContext;
52
        $this->orderRepository = $orderRepository;
53
    }
54
55
    /**
56
     * @param UserEvent $event
57
     */
58
    public function onUserImpersonated(UserEvent $event): void
59
    {
60
        $customer = $event->getUser()->getCustomer();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\User\Model\UserInterface as the method getCustomer() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ShopUser.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
61
62
        /** @var ChannelInterface $channel */
63
        $channel = $this->channelContext->getChannel();
64
65
        /** @var OrderInterface $cart */
66
        $cart = $this->orderRepository->findLatestCartByChannelAndCustomer($channel, $customer);
67
68
        if ($cart === null) {
69
            $this->cartStorage->removeForChannel($channel);
70
71
            return;
72
        }
73
74
        $this->cartStorage->setForChannel($channel, $cart);
75
    }
76
}
77