Completed
Push — master ( bb050e...36e41a )
by Paweł
11:10
created

ExternalFragmentRenderer::render()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.1635
c 0
b 0
f 0
cc 8
nc 7
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Fragment;
16
17
use SWP\Bundle\BridgeBundle\Client\GuzzleClient;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
22
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
23
use Symfony\Component\HttpKernel\HttpKernelInterface;
24
use Symfony\Component\HttpKernel\KernelEvents;
25
26
/**
27
 * Class ExternalFragmentRenderer.
28
 *
29
 * Render content fetched from external uri with guzzle.
30
 */
31
class ExternalFragmentRenderer implements FragmentRendererInterface
32
{
33
    /**
34
     * @var HttpKernelInterface
35
     */
36
    private $kernel;
37
38
    /**
39
     * @var EventDispatcherInterface
40
     */
41
    private $dispatcher;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param HttpKernelInterface      $kernel     A HttpKernelInterface instance
47
     * @param EventDispatcherInterface $dispatcher A EventDispatcherInterface instance
48
     */
49
    public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
50
    {
51
        $this->kernel = $kernel;
52
        $this->dispatcher = $dispatcher;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function render($uri, Request $request, array $options = [])
59
    {
60
        $level = ob_get_level();
61
62
        try {
63
            return new Response($this->createExternalRequest($uri));
0 ignored issues
show
Bug introduced by
It seems like $uri defined by parameter $uri on line 58 can also be of type object<Symfony\Component...er\ControllerReference>; however, SWP\Bundle\CoreBundle\Fr...createExternalRequest() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
64
        } catch (\Throwable $e) {
65
            // we dispatch the exception event to trigger the logging
66
            // the response that comes back is simply ignored
67
            if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
68
                $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
0 ignored issues
show
Compatibility introduced by
$e of type object<Throwable> is not a sub-type of object<Exception>. It seems like you assume a concrete implementation of the interface Throwable to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
69
70
                $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
71
            }
72
73
            // let's clean up the output buffers that were created by the sub-request
74
            Response::closeOutputBuffers($level, false);
75
76
            if (isset($options['alt'])) {
77
                $alt = $options['alt'];
78
                unset($options['alt']);
79
80
                return $this->render($alt, $request, $options);
81
            }
82
83
            if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
84
                throw $e;
85
            }
86
87
            return new Response();
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getName()
95
    {
96
        return 'external';
97
    }
98
99
    /**
100
     * @param string $uri
101
     *
102
     * @return mixed
103
     */
104
    private function createExternalRequest(string $uri)
105
    {
106
        $client = new GuzzleClient();
107
108
        return $client->makeCall($uri)['body'];
109
    }
110
}
111