Completed
Pull Request — 1.5 (#761)
by Paweł
17:05
created

RuleContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A theFollowingTenantPublishingRule() 0 10 2
A theFollowingOrganizationPublishingRule() 0 13 2
A submitForm() 0 7 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\PyStringNode;
9
use SWP\Bundle\CoreBundle\Model\RuleInterface;
10
use SWP\Bundle\RuleBundle\Form\Type\RuleType;
11
use SWP\Component\Rule\Repository\RuleRepositoryInterface;
12
use SWP\Component\Storage\Factory\FactoryInterface;
13
use Symfony\Component\Form\FormFactoryInterface;
14
use Symfony\Component\Form\FormInterface;
15
16
final class RuleContext extends AbstractContext implements Context
17
{
18
    private $ruleRepository;
19
20
    private $ruleFactory;
21
22
    private $formFactory;
23
24
    public function __construct(
25
        RuleRepositoryInterface $ruleRepository,
26
        FactoryInterface $ruleFactory,
27
        FormFactoryInterface $formFactory
28
    ) {
29
        $this->ruleRepository = $ruleRepository;
30
        $this->ruleFactory = $ruleFactory;
31
        $this->formFactory = $formFactory;
32
    }
33
34
    /**
35
     * @Given the following tenant publishing rule:
36
     */
37
    public function theFollowingTenantPublishingRule(PyStringNode $string)
38
    {
39
        $form = $this->submitForm($string);
40
        if ($form->isValid()) {
41
            $this->ruleRepository->add($form->getData());
42
            $this->ruleRepository->flush();
43
        } else {
44
            throw new \Exception('Rule configuration is invalid');
45
        }
46
    }
47
48
    /**
49
     * @Given the following organization publishing rule:
50
     */
51
    public function theFollowingOrganizationPublishingRule(PyStringNode $string)
52
    {
53
        $form = $this->submitForm($string);
54
        if ($form->isValid()) {
55
            /** @var RuleInterface $rule */
56
            $rule = $form->getData();
57
            $this->ruleRepository->add($rule);
58
            $rule->setTenantCode(null);
59
            $this->ruleRepository->flush();
60
        } else {
61
            throw new \Exception('Rule configuration is invalid');
62
        }
63
    }
64
65
    private function submitForm(PyStringNode $string): FormInterface
66
    {
67
        $form = $this->formFactory->create(RuleType::class, $this->ruleFactory->create(), []);
68
        $form->submit(\json_decode($string->getRaw(), true), true);
69
70
        return $form;
71
    }
72
}
73