Completed
Push — master ( ec5411...629401 )
by Kamil
31:49 queued 16:57
created

AdminUserContext::iHaveTheImageAsMyAvatar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\AdminUserInterface;
21
use Sylius\Component\Core\Model\AvatarImage;
22
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
23
use Sylius\Component\User\Repository\UserRepositoryInterface;
24
use Symfony\Component\HttpFoundation\File\UploadedFile;
25
26
final class AdminUserContext implements Context
27
{
28
    /** @var SharedStorageInterface */
29
    private $sharedStorage;
30
31
    /** @var ExampleFactoryInterface */
32
    private $userFactory;
33
34
    /** @var UserRepositoryInterface */
35
    private $userRepository;
36
37
    /** @var ImageUploaderInterface */
38
    private $imageUploader;
39
40
    /** @var ObjectManager */
41
    private $objectManager;
42
43
    /** @var \ArrayAccess */
44
    private $minkParameters;
45
46
    public function __construct(
47
        SharedStorageInterface $sharedStorage,
48
        ExampleFactoryInterface $userFactory,
49
        UserRepositoryInterface $userRepository,
50
        ImageUploaderInterface $imageUploader,
51
        ObjectManager $objectManager,
52
        \ArrayAccess $minkParameters
53
    ) {
54
        $this->sharedStorage = $sharedStorage;
55
        $this->userFactory = $userFactory;
56
        $this->userRepository = $userRepository;
57
        $this->imageUploader = $imageUploader;
58
        $this->objectManager = $objectManager;
59
        $this->minkParameters = $minkParameters;
60
    }
61
62
    /**
63
     * @Given there is an administrator :email identified by :password
64
     * @Given /^there is(?:| also) an administrator "([^"]+)"$/
65
     */
66
    public function thereIsAnAdministratorIdentifiedBy($email, $password = 'sylius')
67
    {
68
        /** @var AdminUserInterface $adminUser */
69
        $adminUser = $this->userFactory->create(['email' => $email, 'password' => $password, 'enabled' => true]);
70
71
        $this->userRepository->add($adminUser);
72
        $this->sharedStorage->set('administrator', $adminUser);
73
    }
74
75
    /**
76
     * @Given there is an administrator with name :username
77
     */
78
    public function thereIsAnAdministratorWithName($username)
79
    {
80
        /** @var AdminUserInterface $adminUser */
81
        $adminUser = $this->userFactory->create(['username' => $username]);
82
        $adminUser->setUsername($username);
83
84
        $this->userRepository->add($adminUser);
85
        $this->sharedStorage->set('administrator', $adminUser);
86
    }
87
88
    /**
89
     * @Given /^(this administrator) is using ("[^"]+" locale)$/
90
     * @Given /^(I) am using ("[^"]+" locale) for my panel$/
91
     */
92
    public function thisAdministratorIsUsingLocale(AdminUserInterface $adminUser, $localeCode)
93
    {
94
        $adminUser->setLocaleCode($localeCode);
95
96
        $this->userRepository->add($adminUser);
97
        $this->sharedStorage->set('administrator', $adminUser);
98
    }
99
100
    /**
101
     * @Given /^I have the "([^"]*)" image as (my) avatar$/
102
     */
103
    public function iHaveTheImageAsMyAvatar(string $avatarPath, AdminUserInterface $administrator): void
104
    {
105
        $filesPath = $this->minkParameters['files_path'];
106
107
        $avatar = new AvatarImage();
108
        $avatar->setFile(new UploadedFile($filesPath . $avatarPath, basename($avatarPath)));
109
110
        $this->imageUploader->upload($avatar);
111
112
        $administrator->setAvatar($avatar);
113
        $this->objectManager->flush();
114
115
        $this->sharedStorage->set($avatarPath, $avatar->getPath());
116
    }
117
}
118