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 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 an administrator |
||
66 | */ |
||
67 | public function iAmLoggedInAsAnAdministrator() |
||
68 | { |
||
69 | $user = $this->userFactory->create(['email' => '[email protected]', 'password' => 'sylius']); |
||
70 | $this->userRepository->add($user); |
||
71 | |||
72 | $this->securityService->logIn($user); |
||
73 | |||
74 | $this->sharedStorage->set('administrator', $user); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @Given /^I am logged in as "([^"]+)" administrator$/ |
||
79 | */ |
||
80 | public function iAmLoggedInAsAdministrator($email) |
||
81 | { |
||
82 | $user = $this->userRepository->findOneByEmail($email); |
||
83 | Assert::notNull($user); |
||
84 | |||
85 | $this->securityService->logIn($user); |
||
0 ignored issues
–
show
|
|||
86 | |||
87 | $this->sharedStorage->set('administrator', $user); |
||
88 | } |
||
89 | } |
||
90 |
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: