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

ThemeContainersGenerator::generate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
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\Form\Type\ContainerType;
20
use SWP\Bundle\TemplatesSystemBundle\Service\ContainerServiceInterface;
21
use SWP\Component\Storage\Factory\FactoryInterface;
22
use SWP\Component\Storage\Repository\RepositoryInterface;
23
use Symfony\Component\Form\FormFactoryInterface;
24
25
class ThemeContainersGenerator implements GeneratorInterface
26
{
27
    /**
28
     * @var FormFactoryInterface
29
     */
30
    protected $formFactory;
31
32
    /**
33
     * @var FactoryInterface
34
     */
35
    protected $containerFactory;
36
37
    /**
38
     * @var RepositoryInterface
39
     */
40
    protected $containerRepository;
41
42
    /**
43
     * @var ContainerServiceInterface
44
     */
45
    protected $containerService;
46
47
    /**
48
     * ThemeContainersGenerator constructor.
49
     *
50
     * @param FormFactoryInterface      $formFactory
51
     * @param FactoryInterface          $containerFactory
52
     * @param RepositoryInterface       $containerRepository
53
     * @param ContainerServiceInterface $containerService
54
     */
55
    public function __construct(FormFactoryInterface $formFactory, FactoryInterface $containerFactory, RepositoryInterface $containerRepository, ContainerServiceInterface $containerService)
56
    {
57
        $this->formFactory = $formFactory;
58
        $this->containerFactory = $containerFactory;
59
        $this->containerRepository = $containerRepository;
60
        $this->containerService = $containerService;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function generate(array $containers): void
67
    {
68
        foreach ($containers as $containerData) {
69
            if (null !== $this->containerRepository->findOneByName($containerData['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...
70
                continue;
71
            }
72
73
            $this->containerService->createContainer($containerData['name'], [], $this->createContainer($containerData));
74
        }
75
    }
76
77
    /**
78
     * @param array $containerData
79
     *
80
     * @return mixed
81
     *
82
     * @throws \Exception
83
     */
84
    protected function createContainer(array $containerData)
85
    {
86
        $container = $this->containerFactory->create();
87
        $form = $this->formFactory->create(ContainerType::class, $container);
88
        $form->submit($containerData, false);
89
        if (!$form->isValid()) {
90
            throw new \Exception('Invalid container definition');
91
        }
92
93
        return $container;
94
    }
95
}
96