Assertion   C
last analyzed

Complexity

Total Complexity 55

Size/Duplication

Total Lines 437
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 55
eloc 118
c 1
b 0
f 0
dl 0
loc 437
rs 6

29 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllAttributeStatements() 0 10 3
A setId() 0 5 1
A equals() 0 19 5
A hasSessionIndex() 0 13 4
A deserialize() 0 32 1
A getVersion() 0 3 1
A setIssueInstant() 0 5 1
A hasAnySessionIndex() 0 13 4
A getIssuer() 0 3 1
A getConditions() 0 3 1
A serialize() 0 20 2
A setVersion() 0 5 1
A hasBearerSubject() 0 9 4
A getSignature() 0 3 1
A getIssueInstantDateTime() 0 7 2
A getFirstAuthnStatement() 0 9 3
A setSubject() 0 5 1
A getSubject() 0 3 1
A getId() 0 3 1
A addItem() 0 5 1
A setIssuer() 0 5 1
A getAllAuthnStatements() 0 10 3
A getIssueInstantTimestamp() 0 3 1
A getIssueInstantString() 0 7 2
A setConditions() 0 5 1
A setSignature() 0 5 1
A prepareForXml() 0 7 3
A getAllItems() 0 3 1
A getFirstAttributeStatement() 0 9 3

How to fix   Complexity   

Complex Class

Complex classes like Assertion often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Assertion, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Model\Assertion;
13
14
use LightSaml\Helper;
15
use LightSaml\Model\AbstractSamlModel;
16
use LightSaml\Model\Context\DeserializationContext;
17
use LightSaml\Model\Context\SerializationContext;
18
use LightSaml\Model\XmlDSig\Signature;
19
use LightSaml\SamlConstants;
20
21
class Assertion extends AbstractSamlModel
22
{
23
    //region Attributes
24
25
    /**
26
     * @var string
27
     */
28
    protected $id;
29
30
    /**
31
     * @var string
32
     */
33
    protected $version = SamlConstants::VERSION_20;
34
35
    /**
36
     * @var int
37
     */
38
    protected $issueInstant;
39
40
    //endregion
41
42
    //region Elements
43
44
    /**
45
     * @var Issuer
46
     */
47
    protected $issuer;
48
49
    /**
50
     * @var Signature|null
51
     */
52
    protected $signature;
53
54
    /**
55
     * @var Subject|null
56
     */
57
    protected $subject;
58
59
    /**
60
     * @var Conditions|null
61
     */
62
    protected $conditions;
63
64
    /**
65
     * @var array|AbstractStatement[]|AuthnStatement[]|AttributeStatement[]
66
     */
67
    protected $items = [];
68
69
    //endregion
70
71
    /**
72
     * Core 3.3.4 Processing rules.
73
     *
74
     * @param string      $nameId
75
     * @param string|null $format
76
     *
77
     * @return bool
78
     */
79
    public function equals($nameId, $format)
80
    {
81
        if (false == $this->getSubject()) {
0 ignored issues
show
introduced by
The condition false == $this->getSubject() is always false.
Loading history...
82
            return false;
83
        }
84
85
        if (false == $this->getSubject()->getNameID()) {
0 ignored issues
show
introduced by
The condition false == $this->getSubject()->getNameID() is always false.
Loading history...
86
            return false;
87
        }
88
89
        if ($this->getSubject()->getNameID()->getValue() != $nameId) {
90
            return false;
91
        }
92
93
        if ($this->getSubject()->getNameID()->getFormat() != $format) {
94
            return false;
95
        }
96
97
        return true;
98
    }
99
100
    /**
101
     * @param string $sessionIndex
102
     *
103
     * @return bool
104
     */
105
    public function hasSessionIndex($sessionIndex)
106
    {
107
        if (null == $this->getAllAuthnStatements()) {
108
            return false;
109
        }
110
111
        foreach ($this->getAllAuthnStatements() as $authnStatement) {
112
            if ($authnStatement->getSessionIndex() == $sessionIndex) {
113
                return true;
114
            }
115
        }
116
117
        return false;
118
    }
119
120
    public function hasAnySessionIndex()
121
    {
122
        if (false == $this->getAllAuthnStatements()) {
123
            return false;
124
        }
125
126
        foreach ($this->getAllAuthnStatements() as $authnStatement) {
127
            if ($authnStatement->getSessionIndex()) {
128
                return true;
129
            }
130
        }
131
132
        return false;
133
    }
134
135
    //region Getters & Setters
136
137
    /**
138
     * @return Assertion
139
     */
140
    public function setConditions(Conditions $conditions = null)
141
    {
142
        $this->conditions = $conditions;
143
144
        return $this;
145
    }
146
147
    /**
148
     * @return \LightSaml\Model\Assertion\Conditions|null
149
     */
150
    public function getConditions()
151
    {
152
        return $this->conditions;
153
    }
154
155
    /**
156
     * @param string $id
157
     *
158
     * @return Assertion
159
     */
160
    public function setId($id)
161
    {
162
        $this->id = (string) $id;
163
164
        return $this;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getId()
171
    {
172
        return $this->id;
173
    }
174
175
    /**
176
     * @param string|int|\DateTime $issueInstant
177
     *
178
     * @throws \InvalidArgumentException
179
     *
180
     * @return Assertion
181
     */
182
    public function setIssueInstant($issueInstant)
183
    {
184
        $this->issueInstant = Helper::getTimestampFromValue($issueInstant);
185
186
        return $this;
187
    }
188
189
    /**
190
     * @return int
191
     */
192
    public function getIssueInstantTimestamp()
193
    {
194
        return $this->issueInstant;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getIssueInstantString()
201
    {
202
        if ($this->issueInstant) {
203
            return Helper::time2string($this->issueInstant);
204
        }
205
206
        return null;
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getIssueInstantDateTime()
213
    {
214
        if ($this->issueInstant) {
215
            return new \DateTime('@'.$this->issueInstant);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new DateTime('@' . $this->issueInstant) returns the type DateTime which is incompatible with the documented return type string.
Loading history...
216
        }
217
218
        return null;
219
    }
220
221
    /**
222
     * @param Issuer $issuer
223
     *
224
     * @return Assertion
225
     */
226
    public function setIssuer(Issuer $issuer = null)
227
    {
228
        $this->issuer = $issuer;
229
230
        return $this;
231
    }
232
233
    /**
234
     * @return \LightSaml\Model\Assertion\Issuer
235
     */
236
    public function getIssuer()
237
    {
238
        return $this->issuer;
239
    }
240
241
    /**
242
     * @param Signature $signature
243
     *
244
     * @return Assertion
245
     */
246
    public function setSignature(Signature $signature = null)
247
    {
248
        $this->signature = $signature;
249
250
        return $this;
251
    }
252
253
    /**
254
     * @return \LightSaml\Model\XmlDSig\Signature|null
255
     */
256
    public function getSignature()
257
    {
258
        return $this->signature;
259
    }
260
261
    /**
262
     * @return Assertion
263
     */
264
    public function setSubject(Subject $subject)
265
    {
266
        $this->subject = $subject;
267
268
        return $this;
269
    }
270
271
    /**
272
     * @return \LightSaml\Model\Assertion\Subject
273
     */
274
    public function getSubject()
275
    {
276
        return $this->subject;
277
    }
278
279
    /**
280
     * @param string $version
281
     *
282
     * @return Assertion
283
     */
284
    public function setVersion($version)
285
    {
286
        $this->version = (string) $version;
287
288
        return $this;
289
    }
290
291
    /**
292
     * @return string
293
     */
294
    public function getVersion()
295
    {
296
        return $this->version;
297
    }
298
299
    /**
300
     * @return Assertion
301
     */
302
    public function addItem(AbstractStatement $statement)
303
    {
304
        $this->items[] = $statement;
305
306
        return $this;
307
    }
308
309
    /**
310
     * @return AbstractStatement[]|AttributeStatement[]|AuthnStatement[]|array
311
     */
312
    public function getAllItems()
313
    {
314
        return $this->items;
315
    }
316
317
    /**
318
     * @return \LightSaml\Model\Assertion\AuthnStatement[]
319
     */
320
    public function getAllAuthnStatements()
321
    {
322
        $result = [];
323
        foreach ($this->items as $item) {
324
            if ($item instanceof AuthnStatement) {
325
                $result[] = $item;
326
            }
327
        }
328
329
        return $result;
330
    }
331
332
    /**
333
     * @return \LightSaml\Model\Assertion\AttributeStatement[]
334
     */
335
    public function getAllAttributeStatements()
336
    {
337
        $result = [];
338
        foreach ($this->items as $item) {
339
            if ($item instanceof AttributeStatement) {
340
                $result[] = $item;
341
            }
342
        }
343
344
        return $result;
345
    }
346
347
    /**
348
     * @return \LightSaml\Model\Assertion\AttributeStatement|null
349
     */
350
    public function getFirstAttributeStatement()
351
    {
352
        foreach ($this->items as $item) {
353
            if ($item instanceof AttributeStatement) {
354
                return $item;
355
            }
356
        }
357
358
        return null;
359
    }
360
361
    /**
362
     * @return \LightSaml\Model\Assertion\AuthnStatement|null
363
     */
364
    public function getFirstAuthnStatement()
365
    {
366
        foreach ($this->items as $item) {
367
            if ($item instanceof AuthnStatement) {
368
                return $item;
369
            }
370
        }
371
372
        return null;
373
    }
374
375
    //endregion
376
377
    /**
378
     * @return bool
379
     */
380
    public function hasBearerSubject()
381
    {
382
        if ($this->getAllAuthnStatements() && $this->getSubject()) {
383
            if ($this->getSubject()->getBearerConfirmations()) {
384
                return true;
385
            }
386
        }
387
388
        return false;
389
    }
390
391
    protected function prepareForXml()
392
    {
393
        if (false == $this->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->getId() of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
394
            $this->setId(Helper::generateID());
395
        }
396
        if (false == $this->getIssueInstantTimestamp()) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->getIssueInstantTimestamp() of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
397
            $this->setIssueInstant(time());
398
        }
399
    }
400
401
    /**
402
     * @return void
403
     */
404
    public function serialize(\DOMNode $parent, SerializationContext $context)
405
    {
406
        $this->prepareForXml();
407
408
        $result = $this->createElement('Assertion', SamlConstants::NS_ASSERTION, $parent, $context);
409
410
        $this->attributesToXml(['ID', 'Version', 'IssueInstant'], $result);
411
412
        $this->singleElementsToXml(
413
            ['Issuer', 'Subject', 'Conditions'],
414
            $result,
415
            $context
416
        );
417
418
        foreach ($this->items as $item) {
419
            $item->serialize($result, $context);
420
        }
421
422
        // must be added at the end
423
        $this->singleElementsToXml(['Signature'], $result, $context);
424
    }
425
426
    public function deserialize(\DOMNode $node, DeserializationContext $context)
427
    {
428
        $this->checkXmlNodeName($node, 'Assertion', SamlConstants::NS_ASSERTION);
429
430
        $this->attributesFromXml($node, ['ID', 'Version', 'IssueInstant']);
431
432
        $this->singleElementsFromXml($node, $context, [
433
            'Issuer' => ['saml', 'LightSaml\Model\Assertion\Issuer'],
434
            'Subject' => ['saml', 'LightSaml\Model\Assertion\Subject'],
435
            'Conditions' => ['saml', 'LightSaml\Model\Assertion\Conditions'],
436
        ]);
437
438
        $this->manyElementsFromXml(
439
            $node,
440
            $context,
441
            'AuthnStatement',
442
            'saml',
443
            'LightSaml\Model\Assertion\AuthnStatement',
444
            'addItem'
445
        );
446
447
        $this->manyElementsFromXml(
448
            $node,
449
            $context,
450
            'AttributeStatement',
451
            'saml',
452
            'LightSaml\Model\Assertion\AttributeStatement',
453
            'addItem'
454
        );
455
456
        $this->singleElementsFromXml($node, $context, [
457
            'Signature' => ['ds', 'LightSaml\Model\XmlDSig\SignatureXmlReader'],
458
        ]);
459
    }
460
}
461