Passed
Push — develop ( d2d0cb...6c46b1 )
by Pieter van der
02:42
created

ContainerController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 35
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setServiceContainer() 0 3 1
A get() 0 15 2
A setContainer() 0 6 1
1
<?php
2
3
/**
4
 * Copyright 2024 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\GatewayBundle\Container;
20
21
use Exception;
22
use Psr\Container\ContainerInterface;
23
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
24
25
/**
26
 * ! This should be a temporary construction !
27
 *
28
 * The container controller is a means to allow access to the service container
29
 * in the controllers. Allowing the controller actions to ->get services from
30
 * within the controller actions.
31
 *
32
 * This is highly discouraged, but we needed it to get a working Gateway with
33
 * SF6 support. See: https://www.pivotaltracker.com/story/show/187475121
34
 */
35
class ContainerController extends AbstractController
36
{
37
    /** @var ContainerInterface */
38
    protected $container;
39
40
    /** @var ContainerInterface */
41
    protected $serviceContainer;
42
43
    public function setContainer(ContainerInterface $container): ?ContainerInterface
44
    {
45
        $previous = $this->container ?? null;
46
        $this->container = $container;
47
48
        return $previous;
49
    }
50
    public function setServiceContainer(ContainerInterface $container): void
51
    {
52
        $this->serviceContainer = $container;
53
    }
54
55
    public function get(string $serviceName): mixed
56
    {
57
        $logger = $this->serviceContainer->get('logger');
58
        $logger->info(
59
            sprintf(
60
                'Reading the "%s" service from the container (temporary ContainerController solution)',
61
                $serviceName
62
            )
63
        );
64
        try {
65
            $service = $this->container->get($serviceName);
66
        } catch (Exception) {
67
            $service = $this->serviceContainer->get($serviceName);
68
        }
69
        return $service;
70
    }
71
}
72