GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

EmbeddedShortcodeHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 30
c 2
b 0
f 0
dl 0
loc 73
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getControllerName() 0 3 1
A __invoke() 0 29 4
1
<?php
2
3
namespace Webfactory\ShortcodeBundle\Handler;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\NullLogger;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
use Symfony\Component\HttpKernel\Controller\ControllerReference;
9
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
10
use Throwable;
11
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
12
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
13
14
/**
15
 * Handler for thunderer\Shortcode that embeds the configured renderer for a shortcode.
16
 *
17
 * The attributes of the shortcode will be passed on as parameters to the controller.
18
 */
19
class EmbeddedShortcodeHandler
20
{
21
    /** @var FragmentHandler */
22
    private $fragmentHandler;
23
24
    /** @var string */
25
    private $controllerName;
26
27
    /** @var string */
28
    private $renderer;
29
30
    /** @var LoggerInterface */
31
    private $logger;
32
33
    /** @var RequestStack */
34
    private $requestStack;
35
36
    /**
37
     * @param string $controllerName
38
     * @param string $renderer
39
     */
40
    public function __construct(
41
        FragmentHandler $fragmentHandler,
42
        $controllerName,
43
        $renderer,
44
        RequestStack $requestStack,
45
        ?LoggerInterface $logger = null
46
    ) {
47
        $this->fragmentHandler = $fragmentHandler;
48
        $this->controllerName = $controllerName;
49
        $this->renderer = $renderer;
50
        $this->requestStack = $requestStack;
51
        $this->logger = $logger ?: new NullLogger();
52
    }
53
54
    /**
55
     * @return string|null
56
     */
57
    public function __invoke(ShortcodeInterface $shortcode)
58
    {
59
        $this->logger->info(
60
            'Request {controllerName} with parameters {parameters} and renderer {renderer} to resolve shortcode {shortcode}, triggered by a request to {url}.',
61
            [
62
                'controllerName' => $this->controllerName,
63
                'parameters' => json_encode($shortcode->getParameters()),
64
                'renderer' => $this->renderer,
65
                'shortcode' => $shortcode->getName(),
66
                'url' => $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getRequestUri() : '-',
67
            ]
68
        );
69
70
        try {
71
            return $this->fragmentHandler->render(
72
                new ControllerReference($this->controllerName, $shortcode->getParameters()),
73
                $this->renderer
74
            );
75
        } catch (Throwable $exception) {
76
            $this->logger->error('An exception was thrown when rendering the shortcode', ['exception' => $exception]);
77
78
            if ($shortcode instanceof ProcessedShortcode) {
79
                $text = trim($shortcode->getShortcodeText(), '[]');
80
            } else {
81
                $text = $shortcode->getName().' ...';
82
            }
83
84
            // Avoid an infinite loop that occurs if the original shortcode is returned
85
            return "<code>&#91;$text&#93;</code>";
86
        }
87
    }
88
89
    public function getControllerName(): string
90
    {
91
        return $this->controllerName;
92
    }
93
}
94