Completed
Push — EZP-31644 ( 47e85b )
by
unknown
17:57
created

InlineFragmentRenderer::render()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 31

Duplication

Lines 18
Ratio 58.06 %

Importance

Changes 0
Metric Value
cc 7
nc 13
nop 3
dl 18
loc 31
rs 8.4906
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Bundle\EzPublishCoreBundle\Fragment;
8
9
use eZ\Publish\Core\MVC\Symfony\Component\Serializer\SerializerTrait;
10
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware;
11
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Controller\ControllerReference;
14
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
15
use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer as BaseRenderer;
16
use Symfony\Component\HttpKernel\Fragment\RoutableFragmentRenderer;
17
18
class InlineFragmentRenderer extends BaseRenderer implements SiteAccessAware
19
{
20
    use SerializerTrait;
21
22
    /** @var \Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface */
23
    private $innerRenderer;
24
25
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess */
26
    private $siteAccess;
27
28
    public function __construct(FragmentRendererInterface $innerRenderer)
29
    {
30
        $this->innerRenderer = $innerRenderer;
31
    }
32
33
    public function setFragmentPath($path)
34
    {
35
        if ($this->innerRenderer instanceof RoutableFragmentRenderer) {
36
            $this->innerRenderer->setFragmentPath($path);
37
        }
38
    }
39
40
    public function setSiteAccess(SiteAccess $siteAccess = null)
41
    {
42
        $this->siteAccess = $siteAccess;
43
    }
44
45
    public function render($uri, Request $request, array $options = [])
46
    {
47
        if ($uri instanceof ControllerReference) {
48 View Code Duplication
            if ($request->attributes->has('siteaccess')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49
                /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess $siteAccess */
50
                $siteAccess = $request->attributes->get('siteaccess');
51
                $uri->attributes['serialized_siteaccess'] = json_encode($siteAccess);
52
                $uri->attributes['serialized_siteaccess_matcher'] = $this->getSerializer()->serialize(
53
                    $siteAccess->matcher,
54
                    'json'
55
                );
56
                if ($siteAccess->matcher instanceof SiteAccess\Matcher\CompoundInterface) {
57
                    $subMatchers = $siteAccess->matcher->getSubMatchers() ?? null;
58
                    foreach ($subMatchers as $subMatcher) {
0 ignored issues
show
Bug introduced by
The expression $subMatchers of type array<integer,object<eZ\...teAccess\Matcher>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
59
                        $uri->attributes['serialized_siteaccess_sub_matchers'][get_class($subMatcher)] = $this->getSerializer()->serialize(
60
                            $subMatcher,
61
                            'json'
62
                        );
63
                    }
64
                }
65
            }
66
            if ($request->attributes->has('semanticPathinfo')) {
67
                $uri->attributes['semanticPathinfo'] = $request->attributes->get('semanticPathinfo');
68
            }
69
            if ($request->attributes->has('viewParametersString')) {
70
                $uri->attributes['viewParametersString'] = $request->attributes->get('viewParametersString');
71
            }
72
        }
73
74
        return $this->innerRenderer->render($uri, $request, $options);
75
    }
76
77
    public function getName()
78
    {
79
        return $this->innerRenderer->getName();
80
    }
81
}
82