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

PackageContext::iPublishTheSubmittedPackage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 2
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 OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
10
use SWP\Bundle\CoreBundle\Form\Type\CompositePublishActionType;
11
use SWP\Bundle\CoreBundle\Model\CompositePublishAction;
12
use SWP\Bundle\CoreBundle\Repository\PackageRepositoryInterface;
13
use SWP\Bundle\CoreBundle\Service\ArticlePublisherInterface;
14
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents;
15
use SWP\Component\Bridge\Transformer\JsonToPackageTransformer;
16
use SWP\Component\Bridge\Events;
17
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\EventDispatcher\GenericEvent;
20
use Symfony\Component\Form\FormFactoryInterface;
21
use Symfony\Component\Form\FormInterface;
22
23
final class PackageContext extends AbstractContext implements Context
24
{
25
    private $tenantContext;
26
27
    private $jsonToPackageTransformer;
28
29
    private $eventDispatcher;
30
31
    private $contentPushProducer;
32
33
    private $articlePublisher;
34
35
    private $formFactory;
36
37
    private $packageRepository;
38
39 View Code Duplication
    public function __construct(
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...
40
        TenantContextInterface $tenantContext,
41
        JsonToPackageTransformer $jsonToPackageTransformer,
42
        EventDispatcherInterface $eventDispatcher,
43
        ProducerInterface $contentPushProducer,
44
        ArticlePublisherInterface $articlePublisher,
45
        FormFactoryInterface $formFactory,
46
        PackageRepositoryInterface $packageRepository
47
    ) {
48
        $this->tenantContext = $tenantContext;
49
        $this->jsonToPackageTransformer = $jsonToPackageTransformer;
50
        $this->eventDispatcher = $eventDispatcher;
51
        $this->contentPushProducer = $contentPushProducer;
52
        $this->articlePublisher = $articlePublisher;
53
        $this->formFactory = $formFactory;
54
        $this->packageRepository = $packageRepository;
55
    }
56
57
    /**
58
     * @Given the following Package ninjs:
59
     */
60
    public function theFollowingPackageNinjs(PyStringNode $node)
61
    {
62
        $package = $this->jsonToPackageTransformer->transform($node->getRaw());
63
        $this->eventDispatcher->dispatch(Events::SWP_VALIDATION, new GenericEvent($package));
64
65
        $payload = \serialize([
66
            'package' => $package,
67
            'tenant' => $this->tenantContext->getTenant(),
68
        ]);
69
70
        $this->contentPushProducer->publish($payload);
71
    }
72
73
    /**
74
     * @Given I publish the submitted package :guid:
75
     */
76
    public function iPublishTheSubmittedPackage(string $guid, PyStringNode $string): void
77
    {
78
        $this->eventDispatcher->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE);
79
80
        $package = $this->packageRepository->findOneBy(['guid' => $guid]);
81
82
        if (null === $package) {
83
            throw new \Exception('Package not found');
84
        }
85
86
        $form = $this->submitForm($string);
87
88
        if ($form->isValid()) {
89
            $this->articlePublisher->publish($package, $form->getData());
90
        } else {
91
            throw new \Exception('Invalid form data');
92
        }
93
    }
94
95 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...
96
    {
97
        $form = $this->formFactory->create(CompositePublishActionType::class, new CompositePublishAction(), []);
98
        $form->submit(\json_decode($string->getRaw(), true), true);
99
100
        return $form;
101
    }
102
}
103