Completed
Push — example/managing-articles ( a73081...648917 )
by Loïc
05:32 queued 03:08
created

AppSecurityContext   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 6
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A iAmLoggedInAs() 0 7 1
A iAmLoggedInAsACustomer() 0 10 1
1
<?php
2
3
/*
4
 * This file is part of Jedisjeux.
5
 *
6
 * (c) Loïc Frémont
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 App\Behat\Context\Setup;
13
14
use App\Behat\Service\SecurityServiceInterface;
15
use App\Behat\Service\SharedStorageInterface;
16
use App\Entity\AppUser;
17
use App\Fixture\Factory\ExampleFactoryInterface;
18
use Behat\Behat\Context\Context;
19
use Sylius\Component\User\Repository\UserRepositoryInterface;
20
use Webmozart\Assert\Assert;
21
22
final class AppSecurityContext implements Context
23
{
24
    /**
25
     * @var SharedStorageInterface
26
     */
27
    private $sharedStorage;
28
29
    /**
30
     * @var SecurityServiceInterface
31
     */
32
    private $securityService;
33
34
    /**
35
     * @var ExampleFactoryInterface
36
     */
37
    private $userFactory;
38
39
    /**
40
     * @var UserRepositoryInterface
41
     */
42
    private $userRepository;
43
44
    /**
45
     * @param SharedStorageInterface   $sharedStorage
46
     * @param SecurityServiceInterface $securityService
47
     * @param ExampleFactoryInterface  $userFactory
48
     * @param UserRepositoryInterface  $userRepository
49
     */
50
    public function __construct(
51
        SharedStorageInterface $sharedStorage,
52
        SecurityServiceInterface $securityService,
53
        ExampleFactoryInterface $userFactory,
54
        UserRepositoryInterface $userRepository
55
    ) {
56
        $this->sharedStorage = $sharedStorage;
57
        $this->securityService = $securityService;
58
        $this->userFactory = $userFactory;
59
        $this->userRepository = $userRepository;
60
    }
61
62
    /**
63
     * @Given I am logged in as :email
64
     */
65
    public function iAmLoggedInAs($email)
66
    {
67
        $user = $this->userRepository->findOneByEmail($email);
68
        Assert::notNull($user);
69
70
        $this->securityService->logIn($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userRepository->findOneByEmail($email) on line 67 can be null; however, App\Behat\Service\Securi...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...
71
    }
72
73
    /**
74
     * @Given I am a logged in customer
75
     */
76
    public function iAmLoggedInAsACustomer()
77
    {
78
        /** @var AppUser $user */
79
        $user = $this->userFactory->create(['email' => '[email protected]', 'password' => 'password', 'roles' => ['ROLE_USER']]);
80
        $this->userRepository->add($user);
81
82
        $this->securityService->logIn($user);
83
84
        $this->sharedStorage->set('customer', $user->getCustomer());
85
    }
86
}
87