Completed
Push — master ( 37aba0...533498 )
by Paweł
09:21
created

Sylius/Behat/Context/Setup/ShopSecurityContext.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
namespace Sylius\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\Service\SecurityServiceInterface;
16
use Sylius\Behat\Service\SharedStorageInterface;
17
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
18
use Sylius\Component\User\Repository\UserRepositoryInterface;
19
use Webmozart\Assert\Assert;
20
21
/**
22
 * @author Arkadiusz Krakowiak <[email protected]>
23
 */
24
final class ShopSecurityContext implements Context
25
{
26
    /**
27
     * @var SharedStorageInterface
28
     */
29
    private $sharedStorage;
30
31
    /**
32
     * @var SecurityServiceInterface
33
     */
34
    private $securityService;
35
36
    /**
37
     * @var ExampleFactoryInterface
38
     */
39
    private $userFactory;
40
41
    /**
42
     * @var UserRepositoryInterface
43
     */
44
    private $userRepository;
45
46
    /**
47
     * @param SharedStorageInterface $sharedStorage
48
     * @param SecurityServiceInterface $securityService
49
     * @param ExampleFactoryInterface $userFactory
50
     * @param UserRepositoryInterface $userRepository
51
     */
52
    public function __construct(
53
        SharedStorageInterface $sharedStorage,
54
        SecurityServiceInterface $securityService,
55
        ExampleFactoryInterface $userFactory,
56
        UserRepositoryInterface $userRepository
57
    ) {
58
        $this->sharedStorage = $sharedStorage;
59
        $this->securityService = $securityService;
60
        $this->userFactory = $userFactory;
61
        $this->userRepository = $userRepository;
62
    }
63
64
    /**
65
     * @Given I am logged in as :email
66
     */
67
    public function iAmLoggedInAs($email)
68
    {
69
        $user = $this->userRepository->findOneByEmail($email);
70
        Assert::notNull($user);
71
72
        $this->securityService->logIn($user);
0 ignored issues
show
It seems like $user defined by $this->userRepository->findOneByEmail($email) on line 69 can be null; however, Sylius\Behat\Service\Sec...rviceInterface::logIn() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
73
    }
74
75
    /**
76
     * @Given I am a logged in customer
77
     */
78
    public function iAmLoggedInCustomer()
79
    {
80
        $user = $this->userFactory->create(['email' => '[email protected]', 'password' => 'sylius', 'enabled' => true]);
81
        $this->userRepository->add($user);
82
83
        $this->securityService->logIn($user);
84
85
        $this->sharedStorage->set('user', $user);
86
    }
87
}
88