BindingFactory   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
eloc 47
c 1
b 0
f 0
dl 0
loc 122
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B processPOST() 0 21 7
A __construct() 0 3 1
A getBindingByRequest() 0 5 1
A processGET() 0 10 4
A setEventDispatcher() 0 5 1
A create() 0 25 6
A detectBindingType() 0 10 3
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Binding;
13
14
use LightSaml\Error\LightSamlBindingException;
15
use LightSaml\SamlConstants;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class BindingFactory implements BindingFactoryInterface
20
{
21
    /** @var EventDispatcherInterface|null */
22
    protected $eventDispatcher;
23
24
    /**
25
     * @param EventDispatcherInterface $eventDispatcher
26
     */
27
    public function __construct(EventDispatcherInterface $eventDispatcher = null)
28
    {
29
        $this->eventDispatcher = $eventDispatcher;
30
    }
31
32
    /**
33
     * @return BindingFactoryInterface
34
     */
35
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher = null)
36
    {
37
        $this->eventDispatcher = $eventDispatcher;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @return AbstractBinding
44
     */
45
    public function getBindingByRequest(Request $request)
46
    {
47
        $bindingType = $this->detectBindingType($request);
48
49
        return $this->create($bindingType);
50
    }
51
52
    /**
53
     * @param string $bindingType
54
     *
55
     * @throws \LogicException
56
     * @throws \LightSaml\Error\LightSamlBindingException
57
     *
58
     * @return AbstractBinding
59
     */
60
    public function create($bindingType)
61
    {
62
        $result = null;
63
        switch ($bindingType) {
64
            case SamlConstants::BINDING_SAML2_HTTP_REDIRECT:
65
                $result = new HttpRedirectBinding();
66
                break;
67
68
            case SamlConstants::BINDING_SAML2_HTTP_POST:
69
                $result = new HttpPostBinding();
70
                break;
71
72
            case SamlConstants::BINDING_SAML2_HTTP_ARTIFACT:
73
                throw new \LogicException('Artifact binding not implemented');
74
            case SamlConstants::BINDING_SAML2_SOAP:
75
                throw new \LogicException('SOAP binding not implemented');
76
        }
77
78
        if ($result) {
79
            $result->setEventDispatcher($this->eventDispatcher);
80
81
            return $result;
82
        }
83
84
        throw new LightSamlBindingException(sprintf("Unknown binding type '%s'", $bindingType));
85
    }
86
87
    /**
88
     * @return string|null
89
     */
90
    public function detectBindingType(Request $request)
91
    {
92
        $requestMethod = trim(strtoupper($request->getMethod()));
93
        if ('GET' == $requestMethod) {
94
            return $this->processGET($request);
95
        } elseif ('POST' == $requestMethod) {
96
            return $this->processPOST($request);
97
        }
98
99
        return null;
100
    }
101
102
    /**
103
     * @return string|null
104
     */
105
    protected function processGET(Request $request)
106
    {
107
        $get = $request->query->all();
108
        if (array_key_exists('SAMLRequest', $get) || array_key_exists('SAMLResponse', $get)) {
109
            return SamlConstants::BINDING_SAML2_HTTP_REDIRECT;
110
        } elseif (array_key_exists('SAMLart', $get)) {
111
            return SamlConstants::BINDING_SAML2_HTTP_ARTIFACT;
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * @return string|null
119
     */
120
    protected function processPOST(Request $request)
121
    {
122
        $post = $request->request->all();
123
        if (array_key_exists('SAMLRequest', $post) || array_key_exists('SAMLResponse', $post)) {
124
            return SamlConstants::BINDING_SAML2_HTTP_POST;
125
        } elseif (array_key_exists('SAMLart', $post)) {
126
            return SamlConstants::BINDING_SAML2_HTTP_ARTIFACT;
127
        } else {
128
            if ($contentType = $request->headers->get('CONTENT_TYPE')) {
129
                // Remove charset
130
                if (false !== $pos = strpos($contentType, ';')) {
131
                    $contentType = substr($contentType, 0, $pos);
132
                }
133
134
                if ('text/xml' === $contentType) {
135
                    return SamlConstants::BINDING_SAML2_SOAP;
136
                }
137
            }
138
        }
139
140
        return null;
141
    }
142
}
143