Completed
Push — master ( b9c485...94fed1 )
by Michał
13:51
created

Behat/Context/Setup/AdminSecurityContext.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 AdminSecurityContext 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 UserRepositoryInterface $userRepository
50
     */
51
    public function __construct(
52
        SharedStorageInterface $sharedStorage,
53
        SecurityServiceInterface $securityService,
54
        ExampleFactoryInterface $userFactory,
55
        UserRepositoryInterface $userRepository
56
    ) {
57
        $this->sharedStorage = $sharedStorage;
58
        $this->securityService = $securityService;
59
        $this->userFactory = $userFactory;
60
        $this->userRepository = $userRepository;
61
    }
62
63
    /**
64
     * @Given I am logged in as an administrator
65
     */
66
    public function iAmLoggedInAsAnAdministrator()
67
    {
68
        $user = $this->userFactory->create();
69
        $this->userRepository->add($user);
70
71
        $this->securityService->logIn($user);
72
73
        $this->sharedStorage->set('admin', $user);
74
    }
75
76
    /**
77
     * @Given /^I am logged in as "([^"]+)" administrator$/
78
     */
79
    public function iAmLoggedInAsAdministrator($email)
80
    {
81
        $user = $this->userRepository->findOneByEmail($email);
82
        Assert::notNull($user);
83
84
        $this->securityService->logIn($user);
0 ignored issues
show
It seems like $user defined by $this->userRepository->findOneByEmail($email) on line 81 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...
85
86
        $this->sharedStorage->set('admin', $user);
87
    }
88
}
89