LogoutResponse::create()   B
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 8.7636
c 0
b 0
f 0
cc 3
nc 2
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 SAML2\XML\saml\Issuer;
16
use yii\base\Event;
17
18
/**
19
 * Class AbstractLogoutResponse
20
 * @package flipbox\saml\core\services\messages
21
 */
22
class LogoutResponse extends Component
23
{
24
    const EVENT_AFTER_MESSAGE_CREATED = 'eventAfterMessageCreated';
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function create(
30
        LogoutRequest $request,
31
        AbstractProvider $theirProvider,
32
        AbstractProvider $ourProvider
33
    ) {
34
        $logout = new SamlLogoutResponse();
35
36
        /**
37
         * Set remote destination
38
         */
39
        $logout->setDestination(
40
            $theirProvider->getType() === SettingsInterface::SP ?
41
                $theirProvider->firstSpSloService(
42
                )->getLocation() :
43
                $theirProvider->firstIdpSloService(
44
                )->getLocation()
45
        );
46
47
        /**
48
         * Set session id
49
         */
50
        $logout->setInResponseTo(
51
            $request->getSessionIndex()
52
        );
53
54
        $logout->setRelayState(
55
            $request->getRelayState()
56
        );
57
58
        /**
59
         * Set issuer
60
         */
61
        $logout->setIssuer(
62
            $issuer = new Issuer()
63
        );
64
        $issuer->setValue(
65
            $ourProvider->getEntityId()
66
        );
67
68
        /**
69
         * Sign the message
70
         */
71
        if ($ourProvider->keychain) {
72
            $logout->setSignatureKey(
73
                $ourProvider->keychainPrivateXmlSecurityKey()
74
            );
75
        }
76
77
        /**
78
         * Kick off event here so people can manipulate this object if needed
79
         */
80
        $event = new Event();
81
82
        /**
83
         * request
84
         */
85
        $event->sender = $request;
86
        /**
87
         * response
88
         */
89
        $event->data = $logout;
90
        $this->trigger(static::EVENT_AFTER_MESSAGE_CREATED, $event);
91
92
        return $logout;
93
    }
94
}
95