Completed
Branch feature-unit-tests (9d0273)
by Tim
01:44
created

Metadata::__construct()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 3
nop 1
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite;
4
5
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration;
6
use \SimpleSAML\Module\monitor\TestCase as TestCase;
7
use \SimpleSAML\Module\monitor\TestData as TestData;
8
use \SimpleSAML\Module\monitor\TestResult as TestResult;
9
10
final class Metadata extends \SimpleSAML\Module\monitor\TestSuiteFactory
11
{
12
    /**
13
     * @param array
14
     */
15
    private $metadata = array();
16
17
    /**
18
     * @param integer|null;
19
     */
20
    private $certExpirationWarning = null;
21
22
    /**
23
     * @param TestConfiguration $configuration
24
     */
25
    public function __construct($configuration)
26
    {
27
        $moduleConfig = $configuration->getModuleConfig();
28
        $metadataConfig = $configuration->getMetadataConfig();
29
30
        $checkMetadata = $moduleConfig->getValue('checkMetadata', true);
31
        if ($checkMetadata === true) {
32
            $metadata = $metadataConfig;
33
        } else {
34
            $metadata = array();
35
            if (is_array($checkMetadata)) {
36
                foreach ($checkMetadata as $set => $entityId) {
37
                    if (array_key_exists($set, $metadataConfig)) {
38
                        if (array_key_exists($entityId, $metadataConfig[$set])) {
39
                            $metadata[$set][$entityId] = $metadataConfig[$set][$entityId];
40
                        }
41
                    }
42
                }
43
            }
44
        } 
45
46
        $this->certExpirationWarning = $moduleConfig->getValue('certExpirationWarning', 28);
47
48
        $this->fixEntityIds($metadata);
49
        $this->metadata = $metadata;
50
        $this->setCategory('Metadata');
51
52
        parent::__construct($configuration);
53
    }
54
55
    /**
56
     * @return void
57
     */
58
    public function invokeTest()
59
    {
60
        $configuration = $this->getConfiguration();
61
        $output = [];
62
63
        foreach ($this->metadata as $set => $metadataSet) {
64
            foreach ($metadataSet as $entityId => $entityMetadata) {
65
                $input = array(
66
                    'entityId' => $entityId,
67
                    'entityMetadata' => $entityMetadata
68
                );
69
                $testData = new TestData($input);
70
71
                $metadataTest = new Metadata\Entity($configuration, $testData);
72
                $output[$entityId] = $metadataTest->getArrayizeTestResults();
73
74
                $this->addTestResults($metadataTest->getTestResults());
75
76
            }
77
        }
78
79
        $state = $this->calculateState();
80
        $testResult = new TestResult('Metadata entities');
81
        $testResult->setState($state);
82
        $testResult->setOutput($output);
83
        $this->setTestResult($testResult);
84
    }
85
86
    /**
87
     * @param array $metadata
88
     *
89
     * @return void
90
     */
91
    private function fixEntityIds(&$metadata)
92
    {
93
        foreach ($metadata as $set => $metadataSet) {
94
            foreach ($metadataSet as $entityId => $entityMetadata) {
95
                if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) {
96
                    // Remove old entry and create a new one based on new entityId
97
                    unset($metadata[$set][$entityId]);
98
                    $newEntityId = $this->generateDynamicHostedEntityID($set);
99
                    $metadata[$set][$newEntityId] = $entityMetadata;
100
                }
101
            }
102
        }
103
    }
104
105
    // Borrowed this from lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php until we take care of different sources properly
106
    /**
107
     * @return string
108
     */
109
    private function generateDynamicHostedEntityID($set)
110
    {
111
        // get the configuration
112
        $baseurl = \SimpleSAML\Utils\HTTP::getBaseURL();
113
114
        if ($set === 'saml20-idp-hosted') {
115
            return $baseurl.'saml2/idp/metadata.php';
116
        } elseif ($set === 'shib13-idp-hosted') {
117
            return $baseurl.'shib13/idp/metadata.php';
118
        } elseif ($set === 'wsfed-sp-hosted') {
119
            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost();
120
        } elseif ($set === 'adfs-idp-hosted') {
121
            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp';
122
        } else {
123
            throw new \Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']');
124
        }
125
    }
126
}
127
128