Completed
Push — master ( 7a069b...4da1f4 )
by Paweł
11s
created

ThemeWidgetsGenerator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B generate() 0 20 5
A createWidget() 0 11 2
A linkWidgets() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Theme\Generator;
18
19
use SWP\Bundle\TemplatesSystemBundle\Factory\ContainerWidgetFactoryInterface;
20
use SWP\Bundle\TemplatesSystemBundle\Form\Type\WidgetType;
21
use SWP\Bundle\TemplatesSystemBundle\Provider\ContainerProviderInterface;
22
use SWP\Component\Storage\Factory\FactoryInterface;
23
use SWP\Component\Storage\Repository\RepositoryInterface;
24
use Symfony\Component\Form\FormFactoryInterface;
25
26
class ThemeWidgetsGenerator implements GeneratorInterface
27
{
28
    /**
29
     * @var FormFactoryInterface
30
     */
31
    protected $formFactory;
32
33
    /**
34
     * @var FactoryInterface
35
     */
36
    protected $widgetModelFactory;
37
38
    /**
39
     * @var RepositoryInterface
40
     */
41
    protected $widgetModelRepository;
42
43
    /**
44
     * @var ContainerWidgetFactoryInterface
45
     */
46
    protected $containerWidgetFactory;
47
48
    /**
49
     * @var ContainerProviderInterface
50
     */
51
    protected $containerProvider;
52
53
    /**
54
     * ThemeWidgetsGenerator constructor.
55
     *
56
     * @param FormFactoryInterface            $formFactory
57
     * @param FactoryInterface                $widgetModelFactory
58
     * @param RepositoryInterface             $widgetModelRepository
59
     * @param ContainerWidgetFactoryInterface $containerWidgetFactory
60
     * @param ContainerProviderInterface      $containerProvider
61
     */
62
    public function __construct(FormFactoryInterface $formFactory, FactoryInterface $widgetModelFactory, RepositoryInterface $widgetModelRepository, ContainerWidgetFactoryInterface $containerWidgetFactory, ContainerProviderInterface $containerProvider)
63
    {
64
        $this->formFactory = $formFactory;
65
        $this->widgetModelFactory = $widgetModelFactory;
66
        $this->widgetModelRepository = $widgetModelRepository;
67
        $this->containerWidgetFactory = $containerWidgetFactory;
68
        $this->containerProvider = $containerProvider;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function generate(array $widgets): void
75
    {
76
        foreach ($widgets as $widgetData) {
77
            if (null !== $this->widgetModelRepository->findOneByName($widgetData['name'])) {
0 ignored issues
show
Bug introduced by
The method findOneByName() does not exist on SWP\Component\Storage\Re...ory\RepositoryInterface. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
78
                continue;
79
            }
80
81
            $widgetContainers = 0;
82
            if (count($widgetData['containers']) > 0) {
83
                $widgetContainers = $widgetData['containers'];
84
            }
85
            unset($widgetData['containers']);
86
87
            $widget = $this->createWidget($widgetData);
88
            if (null !== $widgetContainers) {
89
                $this->linkWidgets($widget, $widgetContainers);
90
            }
91
            $this->widgetModelRepository->add($widget);
92
        }
93
    }
94
95
    /**
96
     * @param array $widgetData
97
     *
98
     * @return mixed
99
     *
100
     * @throws \Exception
101
     */
102
    protected function createWidget(array $widgetData)
103
    {
104
        $widget = $this->widgetModelFactory->create();
105
        $form = $this->formFactory->create(WidgetType::class, $widget);
106
        $form->submit($widgetData, false);
107
        if (!$form->isValid()) {
108
            throw new \Exception('Invalid widget definition');
109
        }
110
111
        return $widget;
112
    }
113
114
    /**
115
     * @param $widget
116
     * @param $containers
117
     */
118
    protected function linkWidgets($widget, $containers)
119
    {
120
        foreach ($containers as $containerName) {
121
            $container = $this->containerProvider->getOneByName($containerName);
122
            if (null === $container) {
123
                continue;
124
            }
125
126
            $containerWidget = $this->containerWidgetFactory->create($container, $widget);
127
            $this->widgetModelRepository->persist($containerWidget);
128
129
            $container->addWidget($containerWidget);
130
        }
131
    }
132
}
133