Completed
Pull Request — development (#546)
by Nick
06:40
created

AbstractController::getGlobalContext()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Legacy\Traits\LegacyTemplateTrait;
6
use Oc\GlobalContext\GlobalContext;
7
use RuntimeException;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * Class AbstractController
14
 *
15
 * @package AppBundle\Controller
0 ignored issues
show
introduced by
Unexpected tag type @package in doc block
Loading history...
16
 */
17
abstract class AbstractController extends Controller
18
{
19
    use LegacyTemplateTrait;
20
21
    /**
22
     * Fetches the global context from the master request.
23
     *
24
     * @return GlobalContext
0 ignored issues
show
introduced by
GlobalContext => \Oc\GlobalContext\GlobalContext
Loading history...
25
     */
26
    public function getGlobalContext()
27
    {
28
        $requestStack = $this->get('request_stack');
29
30
        $masterRequest = $requestStack->getMasterRequest();
31
32
        if ($masterRequest === null) {
33
            throw new RuntimeException('No master request found.');
34
        }
35
36
        /**
37
         * @var GlobalContext $globalContext
0 ignored issues
show
introduced by
GlobalContext => \Oc\GlobalContext\GlobalContext
Loading history...
38
         */
39
        $globalContext = $masterRequest->get('global_context');
40
41
        if ($globalContext === null) {
42
            throw new RuntimeException('Global context not found on master request');
43
        }
44
45
        return $globalContext;
46
    }
47
48
    /**
49
     * Sets the container.
50
     *
51
     * There is no container available in the constructor of a controller, so we override setContainer() and use this
52
     *
53
     * @param ContainerInterface|null $container A ContainerInterface instance or null.
0 ignored issues
show
introduced by
ContainerInterface => \Symfony\Component\DependencyInjection\ContainerInterface
Loading history...
54
     *
55
     * @return void
56
     */
57
    public function setContainer(ContainerInterface $container = null)
58
    {
59
        parent::setContainer($container);
60
61
        if ($container === null) {
62
            return;
63
        }
64
65
        $requestStack = $container->get('request_stack');
66
        /**
67
         * @var Request $masterRequest
0 ignored issues
show
introduced by
Request => \Symfony\Component\HttpFoundation\Request
Loading history...
68
         */
69
        $masterRequest = $requestStack->getMasterRequest();
70
        if ($masterRequest) {
71
            $this->setTarget($masterRequest->getUri());
72
        }
73
    }
74
75
    /**
76
     * @param string $message
77
     *
78
     * @return void
79
     */
80
    protected function addErrorMessage($message)
81
    {
82
        $this->addFlash('error', $message);
83
    }
84
85
    /**
86
     * @param string $message
87
     *
88
     * @return void
89
     */
90
    protected function addSuccessMessage($message)
91
    {
92
        $this->addFlash('success', $message);
93
    }
94
95
    /**
96
     * @param string $message
97
     *
98
     * @return void
99
     */
100
    protected function addInfoMessage($message)
101
    {
102
        $this->addFlash('info', $message);
103
    }
104
}
105