Discovery   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 9
c 8
b 1
f 0
lcom 1
cbo 7
dl 0
loc 90
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A supports() 0 5 1
B manage() 0 29 4
A getPath() 0 16 3
1
<?php
2
3
namespace AerialShip\SamlSPBundle\Bridge;
4
5
use AerialShip\SamlSPBundle\Config\ServiceInfoCollection;
6
use AerialShip\SamlSPBundle\RelyingParty\RelyingPartyInterface;
7
use Symfony\Component\Templating\EngineInterface;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Security\Http\HttpUtils;
12
13
class Discovery implements RelyingPartyInterface
14
{
15
    /** @var  ServiceInfoCollection */
16
    protected $metaProviders;
17
18
    /** @var \Symfony\Component\Templating\EngineInterface  */
19
    protected $twig;
20
21
    /** @var \Symfony\Component\Security\Http\HttpUtils  */
22
    protected $httpUtils;
23
24
25
    /**
26
     * @param string $providerID
27
     * @param ServiceInfoCollection $metaProviders
28
     * @param EngineInterface $twig
29
     * @param HttpUtils $httpUtils
30
     */
31
    function __construct($providerID, ServiceInfoCollection $metaProviders, EngineInterface  $twig, HttpUtils $httpUtils)
0 ignored issues
show
Unused Code introduced by
The parameter $providerID is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
    {
33
        $this->metaProviders = $metaProviders;
34
        $this->twig = $twig;
35
        $this->httpUtils = $httpUtils;
36
    }
37
38
39
    /**
40
     * @param \Symfony\Component\HttpFoundation\Request $request
41
     * @return bool
42
     */
43
    public function supports(Request $request)
44
    {
45
        $result = $request->attributes->get('discovery_path') == $request->getPathInfo();
46
        return $result;
47
    }
48
49
    /**
50
     * @param \Symfony\Component\HttpFoundation\Request $request
51
     * @throws \RuntimeException
52
     * @throws \InvalidArgumentException if cannot manage the Request
53
     * @return \Symfony\Component\HttpFoundation\Response|SamlSpInfo
54
     */
55
    public function manage(Request $request)
56
    {
57
        if (!$this->supports($request)) {
58
            throw new \InvalidArgumentException('Unsupported request');
59
        }
60
61
        $path = $this->getPath($request);
62
63
        $allProviders = $this->metaProviders->all();
64
65
        if (count($allProviders) == 1) {
66
            // there's only one idp... go straight to it
67
            $names = array_keys($allProviders);
68
            return new RedirectResponse($path.'?as='.array_pop($names));
69
        } else if (count($allProviders) == 0) {
70
            // configuration validation should ensure this... but anyway just to be sure
71
            throw new \RuntimeException('At least one authentication service required in configuration');
72
        } else {
73
            //$this->metaProviders->get('')->getIdpProvider()->getEntityDescriptor()->getEntityID()
74
            // present user to choose which idp he wants to authenticate with
75
            return new Response($this->twig->render(
76
                '@AerialShipSamlSP/discovery.html.twig',
77
                array(
78
                    'providers' => $this->metaProviders->all(),
79
                    'path' => $path
80
                )
81
            ));
82
        }
83
    }
84
85
86
    protected function getPath(Request $request)
87
    {
88
        $type = $request->query->get('type');
89
        switch ($type) {
90
            case 'metadata':
91
                $path = $request->attributes->get('metadata_path');
92
                break;
93
            case 'logout':
94
                $path = $request->attributes->get('logout_path');
95
                break;
96
            default:
97
                $path = $request->attributes->get('login_path');
98
        }
99
        $path = $this->httpUtils->generateUri($request, $path);
100
        return $path;
101
    }
102
}
103