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

LogoutRequest::create()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 8.48
c 0
b 0
f 0
nc 2
cc 3
nop 4

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 flipbox\saml\core\records\AbstractProviderIdentity;
13
use SAML2\Constants;
14
use SAML2\XML\saml\NameID;
15
use yii\base\Event;
16
use SAML2\LogoutRequest as SamlLogoutRequest;
17
18
/**
19
 * Class LogoutRequest
20
 * @package flipbox\saml\core\services\messages
21
 */
22
class LogoutRequest extends Component
23
{
24
25
26
    const EVENT_AFTER_MESSAGE_CREATED = 'eventAfterMessageCreated';
27
28
    /**
29
     * @param AbstractProvider $theirProvider
30
     * @param AbstractProvider $ourProvider
31
     * @param AbstractProviderIdentity $identity
32
     * @return SamlLogoutRequest
33
     * @throws \Exception
34
     */
35
    public function create(
36
        AbstractProvider $theirProvider,
37
        AbstractProvider $ourProvider,
38
        AbstractProviderIdentity $identity,
39
        string $relayState = ''
40
    )
41
    {
42
43
        $logout = new SamlLogoutRequest();
44
45
        /**
46
         * Set remote destination
47
         */
48
        $logout->setDestination(
49
            $theirProvider->getType() === SettingsInterface::SP ?
50
                $theirProvider->firstSpSloService(Constants::BINDING_HTTP_POST)->getResponseLocation() :
51
                $theirProvider->firstIdpSloService(Constants::BINDING_HTTP_POST)->getResponseLocation()
52
        );
53
54
        /**
55
         * Set session id
56
         */
57
        $logout->setSessionIndex($identity->sessionId);
58
59
        $logout->setNotOnOrAfter(
60
            (new \DateTime('+5 minutes'))->getTimestamp()
61
        );
62
        $logout->setIssueInstant(
63
            (new \DateTime())->getTimestamp()
64
        );
65
        $logout->setConsent(
66
            Constants::CONSENT_UNSPECIFIED
67
        );
68
69
        $logout->setRelayState(
70
            $relayState
71
        );
72
73
        /**
74
         * Set NameId
75
         */
76
        $logout->setNameID(
77
            $nameId = new NameID()
78
        );
79
80
        $nameId->setValue($identity->nameId);
0 ignored issues
show
Documentation introduced by
The property nameId does not exist on object<flipbox\saml\core...stractProviderIdentity>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
        $nameId->setFormat(Constants::NAMEID_EMAIL_ADDRESS);
82
83
        /**
84
         * Set issuer
85
         */
86
        $logout->setIssuer(
87
            $ourProvider->getEntityId()
88
        );
89
90
        /**
91
         * Sign the message
92
         */
93
        if ($ourProvider->keychain) {
94
            $logout->setSignatureKey(
95
                $ourProvider->keychainPrivateXmlSecurityKey()
96
            );
97
        }
98
99
100
        /**
101
         * Kick off event here so people can manipulate this object if needed
102
         */
103
        $event = new Event();
104
105
        /**
106
         * response
107
         */
108
        $event->data = $logout;
109
        $this->trigger(static::EVENT_AFTER_MESSAGE_CREATED, $event);
110
111
        return $logout;
112
    }
113
}
114