Passed
Push — master ( 9acb33...13732c )
by Tim
03:41
created

Metadata::generateDynamicHostedEntityID()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 15
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Modules\Monitor\TestSuite;
4
5
use \SimpleSAML\Modules\Monitor\TestConfiguration as TestConfiguration;
6
use \SimpleSAML\Modules\Monitor\TestCase as TestCase;
7
use \SimpleSAML\Modules\Monitor\TestData as TestData;
8
use \SimpleSAML\Modules\Monitor\TestResult as TestResult;
9
10
final class Metadata extends \SimpleSAML\Modules\Monitor\TestSuiteFactory
11
{
12
    /** @var array */
13
    private $metadata = [];
14
15
    /** @var integer|null */
16
    private $certExpirationWarning = null;
17
18
19
    /**
20
     * @param TestConfiguration $configuration
21
     */
22
    public function __construct(TestConfiguration $configuration)
23
    {
24
        $moduleConfig = $configuration->getModuleConfig();
25
        $metadataConfig = $configuration->getMetadataConfig();
26
27
        $checkMetadata = $moduleConfig->getValue('checkMetadata', true);
28
        if ($checkMetadata === true) {
29
            $metadata = $metadataConfig;
30
        } else {
31
            $metadata = [];
32
            if (is_array($checkMetadata)) {
33
                foreach ($checkMetadata as $set => $entityId) {
34
                    if (array_key_exists($set, $metadataConfig)) {
35
                        if (array_key_exists($entityId, $metadataConfig[$set])) {
36
                            $metadata[$set][$entityId] = $metadataConfig[$set][$entityId];
37
                        }
38
                    }
39
                }
40
            }
41
        } 
42
43
        $this->certExpirationWarning = $moduleConfig->getValue('certExpirationWarning', 28);
44
45
        $this->fixEntityIds($metadata);
46
        $this->metadata = $metadata;
47
        $this->setCategory('Metadata');
48
49
        parent::__construct($configuration);
50
    }
51
52
53
    /**
54
     * @return void
55
     */
56
    public function invokeTest()
57
    {
58
        $configuration = $this->getConfiguration();
59
        $output = [];
60
61
        foreach ($this->metadata as $set => $metadataSet) {
62
            foreach ($metadataSet as $entityId => $entityMetadata) {
63
                $input = [
64
                    'entityId' => $entityId,
65
                    'entityMetadata' => $entityMetadata
66
                ];
67
                $testData = new TestData($input);
68
69
                $metadataTest = new Metadata\Entity($configuration, $testData);
70
                $output[$entityId] = $metadataTest->getArrayizeTestResults();
71
72
                $this->addTestResults($metadataTest->getTestResults());
73
74
            }
75
        }
76
77
        $state = $this->calculateState();
78
        $testResult = new TestResult('Metadata entities');
79
        $testResult->setState($state);
80
        $testResult->setOutput($output);
81
        $this->setTestResult($testResult);
82
    }
83
84
85
    /**
86
     * @param array $metadata
87
     *
88
     * @return void
89
     */
90
    private function fixEntityIds(array &$metadata)
91
    {
92
        foreach ($metadata as $set => $metadataSet) {
93
            foreach ($metadataSet as $entityId => $entityMetadata) {
94
                if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) {
95
                    // Remove old entry and create a new one based on new entityId
96
                    unset($metadata[$set][$entityId]);
97
                    $newEntityId = $entityMetadata['entityid'];
98
                    $metadata[$set][$newEntityId] = $entityMetadata;
99
                }
100
            }
101
        }
102
    }
103
}
104