Factory::send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace flipbox\saml\core\services\bindings;
4
5
use craft\base\Component;
6
use flipbox\saml\core\exceptions\InvalidMetadata;
7
use flipbox\saml\core\records\AbstractProvider;
8
use flipbox\saml\core\records\ProviderInterface;
9
use SAML2\Constants;
10
use SAML2\AuthnRequest;
11
use SAML2\HTTPPost;
12
use SAML2\HTTPRedirect;
13
use SAML2\LogoutRequest;
14
use SAML2\LogoutResponse;
15
use SAML2\Message as SamlMessage;
16
use SAML2\Response;
17
18
/**
19
 * Class Factory
20
 * @package flipbox\saml\core\services\bindings
21
 */
22
class Factory extends Component
23
{
24
    /**
25
     * @return SamlMessage
26
     * @throws \Exception
27
     */
28
    public static function receive()
29
    {
30
        $request = \Craft::$app->request;
31
        switch ($request->getMethod()) {
32
            case 'POST':
33
                $binding = new HTTPPost;
34
                break;
35
            case 'GET':
36
            default:
37
                $binding = new HTTPRedirect;
38
        }
39
40
        return $binding->receive();
41
    }
42
43
    /**
44
     * @param SamlMessage $message
45
     * @param ProviderInterface $provider
46
     * @return mixed
47
     * @throws InvalidMetadata
48
     */
49
    public static function send(SamlMessage $message, AbstractProvider $provider)
50
    {
51
        $binding = static::determineSendBinding($message, $provider);
52
53
        $binding->send($message);
54
    }
55
56
    /**
57
     * @param SamlMessage $message
58
     * @param AbstractProvider $provider
59
     * @return HTTPPost|HTTPRedirect
60
     */
61
    protected static function determineSendBinding(SamlMessage $message, AbstractProvider $provider)
62
    {
63
        $binding = null;
64
        switch (true) {
65
            case ($message instanceof AuthnRequest):
66
                $binding = $provider->firstIdpSsoService(Constants::BINDING_HTTP_POST)
67
                    ??
68
                    $provider->firstIdpSsoService();
69
                break;
70
            case ($message instanceof Response):
71
                $binding = $provider->firstSpAcsService(Constants::BINDING_HTTP_POST)
72
                    ??
73
                    $provider->firstSpAcsService();
74
                break;
75
            case ($message instanceof LogoutRequest):
76
            case ($message instanceof LogoutResponse):
77
                $binding = static::getSLOEndpoint($provider);
78
                break;
79
        }
80
        return $binding->getBinding() === Constants::BINDING_HTTP_POST ? new HTTPPost : new HTTPRedirect;
81
    }
82
83
    /**
84
     * @param SamlMessage $message
0 ignored issues
show
Bug introduced by
There is no parameter named $message. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
85
     * @param AbstractProvider $provider
86
     * @return \SAML2\XML\md\IndexedEndpointType|null
87
     */
88
    protected static function getSLOEndpoint(AbstractProvider $provider)
89
    {
90
        return $provider->getType() === $provider::TYPE_IDP ? (
91
            $provider->firstIdpSloService(Constants::BINDING_HTTP_POST)
92
            ??
93
            $provider->firstIdpSloService()
94
        ) : (
95
            $provider->firstSpSloService(Constants::BINDING_HTTP_POST)
96
            ??
97
            $provider->firstSpSloService()
98
        );
99
    }
100
}
101