Completed
Push — master ( 4b394d...0ca4f0 )
by Damien
09:55
created

LogoutResponse::create()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 8.589
c 0
b 0
f 0
nc 2
cc 3
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 */
6
7
namespace flipbox\saml\core\services\messages;
8
9
use craft\base\Component;
10
use flipbox\saml\core\models\SettingsInterface;
11
use flipbox\saml\core\records\AbstractProvider;
12
use SAML2\Constants;
13
use SAML2\LogoutRequest;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, flipbox\saml\core\services\messages\LogoutRequest.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use SAML2\LogoutResponse as SamlLogoutResponse;
15
use yii\base\Event;
16
17
/**
18
 * Class AbstractLogoutResponse
19
 * @package flipbox\saml\core\services\messages
20
 */
21
class LogoutResponse extends Component
22
{
23
    const EVENT_AFTER_MESSAGE_CREATED = 'eventAfterMessageCreated';
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function create(
29
        LogoutRequest $request,
30
        AbstractProvider $theirProvider,
31
        AbstractProvider $ourProvider
32
    )
33
    {
34
        $logout = new SamlLogoutResponse();
35
36
        /**
37
         * Set remote destination
38
         */
39
        $logout->setDestination(
40
41
            $theirProvider->getType() === SettingsInterface::SP ?
42
                $theirProvider->firstSpSloService(
43
                /**
44
                 * We only support post right now
45
                 */
46
                    Constants::BINDING_HTTP_POST
47
                )->getResponseLocation() :
48
                $theirProvider->firstIdpSloService(
49
50
                /**
51
                 * We only support post right now
52
                 */
53
                    Constants::BINDING_HTTP_POST
54
                )->getResponseLocation()
55
        );
56
57
        /**
58
         * Set session id
59
         */
60
        $logout->setInResponseTo(
61
            $request->getSessionIndex()
62
        );
63
64
        $logout->setRelayState(
65
            $request->getRelayState()
66
        );
67
68
        /**
69
         * Set issuer
70
         */
71
        $logout->setIssuer(
72
            $ourProvider->getEntityId()
73
        );
74
75
        /**
76
         * Sign the message
77
         */
78
        if ($ourProvider->keychain) {
79
            $logout->setSignatureKey(
80
                $ourProvider->keychainPrivateXmlSecurityKey()
81
            );
82
        }
83
84
        /**
85
         * Kick off event here so people can manipulate this object if needed
86
         */
87
        $event = new Event();
88
89
        /**
90
         * request
91
         */
92
        $event->sender = $request;
93
        /**
94
         * response
95
         */
96
        $event->data = $logout;
97
        $this->trigger(static::EVENT_AFTER_MESSAGE_CREATED, $event);
98
99
        return $logout;
100
    }
101
}
102