Completed
Push — master ( d81c19...f57266 )
by Kamil
20s
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Setup;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\Service\SecurityServiceInterface;
18
use Sylius\Behat\Service\SharedStorageInterface;
19
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
20
use Sylius\Component\User\Repository\UserRepositoryInterface;
21
use Webmozart\Assert\Assert;
22
23
final class AdminSecurityContext implements Context
24
{
25
    /**
26
     * @var SharedStorageInterface
27
     */
28
    private $sharedStorage;
29
30
    /**
31
     * @var SecurityServiceInterface
32
     */
33
    private $securityService;
34
35
    /**
36
     * @var ExampleFactoryInterface
37
     */
38
    private $userFactory;
39
40
    /**
41
     * @var UserRepositoryInterface
42
     */
43
    private $userRepository;
44
45
    /**
46
     * @param SharedStorageInterface $sharedStorage
47
     * @param SecurityServiceInterface $securityService
48
     * @param ExampleFactoryInterface $userFactory
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(['email' => '[email protected]', 'password' => 'sylius']);
69
        $this->userRepository->add($user);
70
71
        $this->securityService->logIn($user);
72
73
        $this->sharedStorage->set('administrator', $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('administrator', $user);
87
    }
88
}
89