Completed
Push — feature/pdp-cbac ( e99222 )
by
unknown
13:10
created

Request::from()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 55
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 55
rs 9.7692
cc 3
eloc 38
nc 3
nop 6

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 2017 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\GatewayBundle\Pdp\Dto;
20
21
use JsonSerializable;
22
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Request\AccessSubject;
23
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Request\Resource;
24
use Webmozart\Assert\Assert;
25
26
final class Request implements JsonSerializable
27
{
28
    const NAMEIDFORMAT_UNSPECIFIED = 'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified';
29
    const XACML_ATTRIBUTE_IP_ADDRESS = 'urn:mace:surfnet.nl:collab:xacml-attribute:ip-address';
30
31
    /**
32
     * @var AccessSubject
33
     */
34
    public $accessSubject;
35
36
    /**
37
     * @var \Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Request\Resource
38
     */
39
    public $resource;
40
41
    /**
42
     * @param string $clientId
43
     * @param string $subjectId
44
     * @param string $idpEntityId
45
     * @param string $spEntityId
46
     * @param array $responseAttributes
47
     * @param string $requestIpAddress
48
     * @return Request $request
49
     */
50
    public static function from($clientId, $subjectId, $idpEntityId, $spEntityId, array $responseAttributes, $requestIpAddress)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
51
    {
52
        Assert::string($subjectId, 'The SubjectId must be a string, received "%s"');
53
        Assert::string($idpEntityId, 'The IDPentityID must be a string, received "%s"');
54
        Assert::string($spEntityId, 'The SPentityID must be a string, received "%s"');
55
        Assert::allString(
56
            array_keys($responseAttributes),
57
            'The keys of the Response attributes must be strings'
58
        );
59
        Assert::allIsArray($responseAttributes, 'The values of the Response attributes must be arrays');
60
        Assert::string($clientId, 'The client ID must be a string, received "%s"');
61
        Assert::string($requestIpAddress, 'The request IP address must be a string, received "%s"');
62
63
        $request = new self;
64
65
        $subjectIdAttribute = new Attribute;
66
        $subjectIdAttribute->attributeId = self::NAMEIDFORMAT_UNSPECIFIED;
67
        $subjectIdAttribute->value = $subjectId;
68
69
        $request->accessSubject = new AccessSubject;
70
        $request->accessSubject->attributes = [$subjectIdAttribute];
71
72
        $spEntityIdAttribute  = new Attribute;
73
        $spEntityIdAttribute->attributeId = 'SPentityID';
74
        $spEntityIdAttribute->value = $spEntityId;
75
76
        $idpEntityIdAttribute = new Attribute;
77
        $idpEntityIdAttribute->attributeId = 'IDPentityID';
78
        $idpEntityIdAttribute->value = $idpEntityId;
79
80
        $clientIdAttribute = new Attribute;
81
        $clientIdAttribute->attributeId = 'ClientID';
82
        $clientIdAttribute->value = $clientId;
83
84
        $request->resource = new Resource;
85
        $request->resource->attributes = [$clientIdAttribute, $spEntityIdAttribute, $idpEntityIdAttribute];
86
87
        foreach ($responseAttributes as $id => $values) {
88
            foreach ($values as $value) {
89
                $attribute = new Attribute;
90
                $attribute->attributeId = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id can also be of type integer. However, the property $attributeId is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
91
                $attribute->value = $value;
92
93
                $request->accessSubject->attributes[] = $attribute;
94
            }
95
        }
96
97
        $ipAddressAttribute = new Attribute;
98
        $ipAddressAttribute->attributeId = self::XACML_ATTRIBUTE_IP_ADDRESS;
99
        $ipAddressAttribute->value = $requestIpAddress;
100
101
        $request->accessSubject->attributes[] = $ipAddressAttribute;
102
103
        return $request;
104
    }
105
106
    public function jsonSerialize()
107
    {
108
        return [
109
            'Request' => [
110
                'AccessSubject'      => $this->accessSubject,
111
                'Resource'           => $this->resource,
112
            ]
113
        ];
114
    }
115
}
116