Completed
Push — master ( 3c7667...799620 )
by Kamil
94:52 queued 57:43
created

src/Sylius/Behat/Context/Setup/UserContext.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 Doctrine\Common\Persistence\ObjectManager;
18
use Sylius\Behat\Service\SharedStorageInterface;
19
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
20
use Sylius\Component\Core\Model\ShopUserInterface;
21
use Sylius\Component\User\Model\UserInterface;
22
use Sylius\Component\User\Repository\UserRepositoryInterface;
23
24
final class UserContext implements Context
25
{
26
    /**
27
     * @var SharedStorageInterface
28
     */
29
    private $sharedStorage;
30
31
    /**
32
     * @var UserRepositoryInterface
33
     */
34
    private $userRepository;
35
36
    /**
37
     * @var ExampleFactoryInterface
38
     */
39
    private $userFactory;
40
41
    /**
42
     * @var ObjectManager
43
     */
44
    private $userManager;
45
46
    /**
47
     * @param SharedStorageInterface $sharedStorage
48
     * @param UserRepositoryInterface $userRepository
49
     * @param ExampleFactoryInterface $userFactory
50
     * @param ObjectManager $userManager
51
     */
52
    public function __construct(
53
        SharedStorageInterface $sharedStorage,
54
        UserRepositoryInterface $userRepository,
55
        ExampleFactoryInterface $userFactory,
56
        ObjectManager $userManager
57
    ) {
58
        $this->sharedStorage = $sharedStorage;
59
        $this->userRepository = $userRepository;
60
        $this->userFactory = $userFactory;
61
        $this->userManager = $userManager;
62
    }
63
64
    /**
65
     * @Given there is a user :email identified by :password
66
     * @Given there was account of :email with password :password
67
     * @Given there is a user :email
68
     */
69
    public function thereIsUserIdentifiedBy($email, $password = 'sylius')
70
    {
71
        $user = $this->userFactory->create(['email' => $email, 'password' => $password, 'enabled' => true]);
72
73
        $this->sharedStorage->set('user', $user);
74
75
        $this->userRepository->add($user);
76
    }
77
78
    /**
79
     * @Given the account of :email was deleted
80
     * @Given my account :email was deleted
81
     */
82
    public function accountWasDeleted($email)
83
    {
84
        /** @var ShopUserInterface $user */
85
        $user = $this->userRepository->findOneByEmail($email);
86
87
        $this->sharedStorage->set('customer', $user->getCustomer());
88
89
        $this->userRepository->remove($user);
90
    }
91
92
    /**
93
     * @Given its account was deleted
94
     */
95
    public function hisAccountWasDeleted()
96
    {
97
        $user = $this->sharedStorage->get('user');
98
99
        $this->userRepository->remove($user);
100
    }
101
102
    /**
103
     * @Given /^(this user) is not verified$/
104
     * @Given /^(I) have not verified my account (?:yet)$/
105
     */
106
    public function accountIsNotVerified(UserInterface $user)
107
    {
108
        $user->setVerifiedAt(null);
109
110
        $this->userManager->flush();
111
    }
112
113
    /**
114
     * @Given /^(?:(I) have|(this user) has) already received a verification email$/
115
     */
116
    public function iHaveReceivedVerificationEmail(UserInterface $user)
117
    {
118
        $this->prepareUserVerification($user);
119
    }
120
121
    /**
122
     * @Given a verification email has already been sent to :email
123
     */
124
    public function aVerificationEmailHasBeenSentTo($email)
125
    {
126
        $user = $this->userRepository->findOneByEmail($email);
127
128
        $this->prepareUserVerification($user);
0 ignored issues
show
It seems like $user defined by $this->userRepository->findOneByEmail($email) on line 126 can be null; however, Sylius\Behat\Context\Set...epareUserVerification() 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...
129
    }
130
131
    /**
132
     * @Given /^(I) have already verified my account$/
133
     */
134
    public function iHaveAlreadyVerifiedMyAccount(UserInterface $user)
135
    {
136
        $user->setVerifiedAt(new \DateTime());
137
138
        $this->userManager->flush();
139
    }
140
141
    /**
142
     * @param UserInterface $user
143
     */
144
    private function prepareUserVerification(UserInterface $user)
145
    {
146
        $token = 'marryhadalittlelamb';
147
        $this->sharedStorage->set('verification_token', $token);
148
149
        $user->setEmailVerificationToken($token);
150
151
        $this->userManager->flush();
152
    }
153
}
154