Completed
Pull Request — master (#8491)
by Stefan
18:30 queued 07:04
created

CustomerGroupContext::theStoreHasCustomerGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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 Sylius\Behat\Service\SharedStorageInterface;
18
use Sylius\Component\Core\Formatter\StringInflector;
19
use Sylius\Component\Customer\Model\CustomerGroupInterface;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
23
final class CustomerGroupContext implements Context
24
{
25
    /**
26
     * @var SharedStorageInterface
27
     */
28
    private $sharedStorage;
29
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    private $customerGroupRepository;
34
35
    /**
36
     * @var FactoryInterface
37
     */
38
    private $customerGroupFactory;
39
40
    /**
41
     * @param SharedStorageInterface $sharedStorage
42
     * @param RepositoryInterface $customerGroupRepository
43
     * @param FactoryInterface $customerGroupFactory
44
     */
45
    public function __construct(
46
        SharedStorageInterface $sharedStorage,
47
        RepositoryInterface $customerGroupRepository,
48
        FactoryInterface $customerGroupFactory
49
    ) {
50
        $this->sharedStorage = $sharedStorage;
51
        $this->customerGroupRepository = $customerGroupRepository;
52
        $this->customerGroupFactory = $customerGroupFactory;
53
    }
54
55
    /**
56
     * @Given the store has a customer group :name
57
     * @Given the store has a customer group :name with :code code
58
     */
59
    public function theStoreHasACustomerGroup($name, $code = null)
60
    {
61
        $this->createCustomerGroup($name, $code);
62
    }
63
64
    /**
65
     * @Given the store has customer groups :firstName and :secondName
66
     * @Given the store has customer groups :firstName, :secondName and :thirdName
67
     */
68
    public function theStoreHasCustomerGroups(string ...$names): void
69
    {
70
        foreach ($names as $name) {
71
            $this->theStoreHasACustomerGroup($name);
72
        }
73
    }
74
75
    /**
76
     * @param string $name
77
     * @param string $code
78
     */
79
    private function createCustomerGroup($name, $code)
80
    {
81
        /** @var CustomerGroupInterface $customerGroup */
82
        $customerGroup = $this->customerGroupFactory->createNew();
83
84
        $customerGroup->setCode($code ?: $this->generateCodeFromName($name));
85
        $customerGroup->setName(ucfirst($name));
86
87
        $this->sharedStorage->set('customer_group', $customerGroup);
88
        $this->customerGroupRepository->add($customerGroup);
89
    }
90
91
    /**
92
     * @param string $name
93
     *
94
     * @return string
95
     */
96
    private function generateCodeFromName($name)
97
    {
98
        return StringInflector::nameToCode($name);
99
    }
100
}
101