HttpPostBinding::receive()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
ccs 15
cts 15
cp 1
rs 9.536
cc 4
nc 5
nop 2
crap 4
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\Context\Profile\Helper\MessageContextHelper;
15
use LightSaml\Context\Profile\MessageContext;
16
use LightSaml\Error\LightSamlBindingException;
17
use LightSaml\Model\Protocol\AbstractRequest;
18
use LightSaml\Model\Protocol\SamlMessage;
19
use Symfony\Component\HttpFoundation\Request;
20
21
class HttpPostBinding extends AbstractBinding
22
{
23
    /**
24
     * @param MessageContext $context
25
     * @param null|string    $destination
26
     *
27
     * @return SamlPostResponse
28
     */
29 4
    public function send(MessageContext $context, $destination = null)
30
    {
31 4
        $message = MessageContextHelper::asSamlMessage($context);
32 4
        $destination = $message->getDestination() ? $message->getDestination() : $destination;
33
34 4
        $serializationContext = $context->getSerializationContext();
35 4
        $message->serialize($serializationContext->getDocument(), $serializationContext);
0 ignored issues
show
Bug introduced by
The method getDocument() does not seem to exist on object<LightSaml\Context\ContextInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Documentation introduced by
$serializationContext is of type object<LightSaml\Context\ContextInterface>|null, but the function expects a object<LightSaml\Model\C...t\SerializationContext>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36 4
        $msgStr = $serializationContext->getDocument()->saveXML();
0 ignored issues
show
Bug introduced by
The method getDocument() does not seem to exist on object<LightSaml\Context\ContextInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
38 4
        $this->dispatchSend($msgStr);
39
40 4
        $msgStr = base64_encode($msgStr);
41
42 4
        $type = $message instanceof AbstractRequest ? 'SAMLRequest' : 'SAMLResponse';
43
44 4
        $data = array($type => $msgStr);
45 4
        if ($message->getRelayState()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $message->getRelayState() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
46 2
            $data['RelayState'] = $message->getRelayState();
47
        }
48
49 4
        $result = new SamlPostResponse($destination, $data);
50 4
        $result->renderContent();
51
52 4
        return $result;
53
    }
54
55
    /**
56
     * @param Request        $request
57
     * @param MessageContext $context
58
     */
59 3
    public function receive(Request $request, MessageContext $context)
60
    {
61 3
        $post = $request->request->all();
62 3
        if (array_key_exists('SAMLRequest', $post)) {
63 1
            $msg = $post['SAMLRequest'];
64 2
        } elseif (array_key_exists('SAMLResponse', $post)) {
65 1
            $msg = $post['SAMLResponse'];
66
        } else {
67 1
            throw new LightSamlBindingException('Missing SAMLRequest or SAMLResponse parameter');
68
        }
69
70 2
        $msg = base64_decode($msg);
71
72 2
        $this->dispatchReceive($msg);
73
74 2
        $deserializationContext = $context->getDeserializationContext();
75 2
        $result = SamlMessage::fromXML($msg, $deserializationContext);
0 ignored issues
show
Documentation introduced by
$deserializationContext is of type object<LightSaml\Context\ContextInterface>|null, but the function expects a object<LightSaml\Model\C...DeserializationContext>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
77 2
        if (array_key_exists('RelayState', $post)) {
78 1
            $result->setRelayState($post['RelayState']);
79
        }
80
81 2
        $context->setMessage($result);
82 2
    }
83
}
84