Completed
Push — 1.5 ( dc62f6...bc7363 )
by Rafał
10:57
created

UserContext::theFollowingUsers()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Behat\Contexts;
6
7
use Behat\Behat\Context\Context;
8
use Behat\Gherkin\Node\TableNode;
9
use FOS\UserBundle\Model\UserManagerInterface;
10
use SWP\Bundle\CoreBundle\Factory\ApiKeyFactory;
11
use SWP\Bundle\CoreBundle\Model\UserInterface;
12
use SWP\Bundle\CoreBundle\Repository\ApiKeyRepositoryInterface;
13
14
final class UserContext extends AbstractContext implements Context
15
{
16
    private $userManager;
17
18
    private $apiKeyFactory;
19
20
    private $apiKeyRepository;
21
22
    public function __construct(UserManagerInterface $userManager, ApiKeyFactory $apiKeyFactory, ApiKeyRepositoryInterface $apiKeyRepository)
23
    {
24
        $this->userManager = $userManager;
25
        $this->apiKeyFactory = $apiKeyFactory;
26
        $this->apiKeyRepository = $apiKeyRepository;
27
    }
28
29
    /**
30
     * @Given the following Users:
31
     */
32
    public function theFollowingUsers(TableNode $table): void
33
    {
34
        foreach ($table as $row => $columns) {
35
            /** @var UserInterface $user */
36
            $user = $this->userManager->createUser();
37
38
            if (isset($columns['role'])) {
39
                $user->addRole($columns['role']);
40
                unset($columns['role']);
41
            }
42
43
            $token = null;
44
            if (isset($columns['token'])) {
45
                $token = $columns['token'];
46
                unset($columns['token']);
47
            }
48
49
            $this->fillObject($user, $columns);
50
            $this->userManager->updateUser($user);
51
52
            $apiKey = $this->apiKeyFactory->create($user, base64_encode($token));
53
54
            $this->apiKeyRepository->add($apiKey);
55
            $this->apiKeyRepository->add($user);
56
        }
57
    }
58
}
59