Completed
Push — master ( 61044a...94728c )
by Rafał
24:26 queued 10:49
created

RuleContext::submitForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\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 View Code Duplication
    private function submitForm(PyStringNode $string): FormInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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