SecurityContext::iAmLoggedInAs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 13
c 2
b 1
f 0
nc 1
nop 1
dl 0
loc 21
rs 9.8333
1
<?php
2
3
/**
4
 * Donut Social Network - Yet another experimental social network.
5
 * Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Donut Social Network.
8
 *
9
 * Donut Social Network is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Donut Social Network is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Donut Social Network.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Donut Social Network
23
 * @copyright Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/donut/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace AppBundle\FeatureContexts\Setup;
29
30
use Angelov\Donut\Behat\Service\Storage\StorageInterface;
31
use Angelov\Donut\Core\CommandBus\CommandBusInterface;
32
use Angelov\Donut\Core\UuidGenerator\UuidGeneratorInterface;
33
use Angelov\Donut\Places\City;
34
use Angelov\Donut\Users\Commands\StoreUserCommand;
35
use Angelov\Donut\Users\Repositories\UsersRepositoryInterface;
36
use Behat\Behat\Context\Context;
37
use Behat\Mink\Session;
38
use Doctrine\ORM\EntityManager;
39
use Symfony\Component\HttpFoundation\Session\SessionInterface;
40
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
41
42
// @todo take a better look on Sylius\Behat\Service\SecurityService
43
44
class SecurityContext implements Context
45
{
46
    private $entityManager;
47
    private $session;
48
    private $minkSession;
49
    private $storage;
50
    private $uuidGenerator;
51
    private $commandBus;
52
    private $users;
53
54
    public function __construct(
55
        EntityManager $entityManager,
56
        SessionInterface $session,
57
        Session $minkSession,
58
        StorageInterface $storage,
59
        UuidGeneratorInterface $uuidGenerator,
60
        CommandBusInterface $commandBus,
61
        UsersRepositoryInterface $users
62
    ) {
63
        $this->entityManager = $entityManager;
64
        $this->session = $session;
65
        $this->minkSession = $minkSession;
66
        $this->storage = $storage;
67
        $this->uuidGenerator = $uuidGenerator;
68
        $this->commandBus = $commandBus;
69
        $this->users = $users;
70
    }
71
72
    /**
73
     * @Given I am logged in as :email
74
     */
75
    public function iAmLoggedInAs(string $email) : void
76
    {
77
        $id = $this->uuidGenerator->generate();
78
        $city = new City($id, 'Valandovo');
79
        $this->entityManager->persist($city);
80
81
        $id = $this->uuidGenerator->generate();
82
        $name = $this->storage->get('user_name', 'John Smith');
83
84
        $this->commandBus->handle(new StoreUserCommand($id, $name, $email, '123456', $city->getId()));
85
        $user = $this->users->find($id);
86
87
        $token = new UsernamePasswordToken($user, $user->getPassword(), 'randomstringbutnotnull', $user->getRoles());
88
89
        $serializedToken = serialize($token);
90
        $this->session->set('_security_main', $serializedToken);
91
        $this->session->save();
92
93
        $this->minkSession->setCookie($this->session->getName(), $this->session->getId());
94
95
        $this->storage->set('logged_user', $user);
96
    }
97
}
98