Completed
Push — master ( 3d485f...407dff )
by Paweł
66:49
created

RevisionAwareContainerService::updateContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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 2015 Sourcefabric z.u. 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 2015 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Service;
18
19
use Doctrine\Common\Collections\ArrayCollection;
20
use Doctrine\ORM\EntityManagerInterface;
21
use SWP\Bundle\TemplatesSystemBundle\Service\ContainerService;
22
use SWP\Bundle\TemplatesSystemBundle\Service\ContainerServiceInterface;
23
use SWP\Component\Revision\Model\RevisionInterface;
24
use SWP\Component\Revision\RevisionAwareInterface;
25
use SWP\Component\TemplatesSystem\Gimme\Model\ContainerInterface;
26
use SWP\Component\TemplatesSystem\Gimme\Model\ContainerWidgetInterface;
27
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
28
use Symfony\Component\DependencyInjection\ContainerInterface as ServiceContainerInterface;
29
use Symfony\Component\HttpFoundation\Request;
30
31
/**
32
 * Class RendererService.
33
 */
34
class RevisionAwareContainerService extends ContainerService implements ContainerServiceInterface
35
{
36
    /**
37
     * RevisionAwareContainerService constructor.
38
     *
39
     * @param EntityManagerInterface    $entityManager
40
     * @param EventDispatcherInterface  $eventDispatcher
41
     * @param ServiceContainerInterface $serviceContainer
42
     */
43
    public function __construct(
44
        EntityManagerInterface $entityManager,
45
        EventDispatcherInterface $eventDispatcher,
46
        ServiceContainerInterface $serviceContainer
47
    ) {
48
        parent::__construct($entityManager, $eventDispatcher, $serviceContainer);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function createContainer($name, array $parameters = [], ContainerInterface $container = null): ContainerInterface
55
    {
56
        // assign current revision to container
57
        $container = $this->serviceContainer->get('swp.factory.container')->create();
58
        if ($container instanceof RevisionAwareInterface) {
59
            $revisionContext = $this->serviceContainer->get('swp_revision.context.revision');
60
            $container->setRevision($revisionContext->getWorkingRevision());
61
        }
62
63
        return parent::createContainer($name, $parameters, $container);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function updateContainer(ContainerInterface $container, array $extraData): ContainerInterface
70
    {
71
        return parent::updateContainer($this->handleForking($container), $extraData);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function linkUnlinkWidget($object, ContainerInterface $container, Request $request)
78
    {
79
        return parent::linkUnlinkWidget($object, $this->handleForking($container), $request);
80
    }
81
82
    private function forkContainerRelations(ContainerInterface $container, ContainerInterface $workingContainer)
83
    {
84
        $containerWidgetFactory = $this->serviceContainer->get('swp.factory.container_widget');
85
        /** @var ContainerWidgetInterface $containerWidget */
86
        foreach ($container->getWidgets() as $containerWidget) {
87
            $containerWidget = $containerWidgetFactory->create($workingContainer, $containerWidget->getWidget());
88
            $this->entityManager->persist($containerWidget);
89
            $workingContainer->addWidget($containerWidget);
90
        }
91
    }
92
93
    private function handleForking($container)
94
    {
95
        if ($container instanceof RevisionAwareInterface &&
96
            $container->getRevision()->getStatus() === RevisionInterface::STATE_PUBLISHED
97
        ) {
98
            if ($this->entityManager->contains($container)) {
99
                $this->entityManager->detach($container);
100
            }
101
102
            $workingContainer = $container->fork();
103
            $workingContainer->setWidgets(new ArrayCollection());
104
            $workingContainer->setData(new ArrayCollection());
105
            /** @var RevisionAwareInterface | ContainerInterface $workingContainer */
106
            $revisionContext = $this->serviceContainer->get('swp_revision.context.revision');
107
            $workingContainer->setRevision($revisionContext->getWorkingRevision());
108
            $this->entityManager->persist($workingContainer);
109
            $this->forkContainerRelations($container, $workingContainer);
0 ignored issues
show
Documentation introduced by
$container is of type object<SWP\Component\Rev...RevisionAwareInterface>, but the function expects a object<SWP\Component\Tem...del\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
110
111
            $this->entityManager->flush();
112
113
            return $workingContainer;
114
        }
115
116
        return $container;
117
    }
118
}
119