Passed
Branch master (4b23d6)
by Tim
04:40
created

Metadata::fixEntityIds()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace SimpleSAML\Module\Monitor\TestSuite;
4
5
use SimpleSAML\Module\Monitor\TestConfiguration;
6
use SimpleSAML\Module\Monitor\TestCase;
7
use SimpleSAML\Module\Monitor\TestData;
8
use SimpleSAML\Module\Monitor\TestResult;
9
10
final class Metadata extends \SimpleSAML\Module\Monitor\TestSuiteFactory
11
{
12
    /** @var array */
13
    private $metadata = [];
14
15
    /** @var integer|null */
16
    private $certExpirationWarning = null;
17
18
19
    /**
20
     * @param \SimpleSAML\Module\Monitor\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(): void
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
        $state = $this->calculateState();
77
        $testResult = new TestResult('Metadata entities');
78
        $testResult->setState($state);
79
        $testResult->setOutput($output);
80
        $this->setTestResult($testResult);
81
    }
82
83
84
    /**
85
     * @param array $metadata
86
     *
87
     * @return void
88
     */
89
    private function fixEntityIds(array &$metadata): void
90
    {
91
        foreach ($metadata as $set => $metadataSet) {
92
            foreach ($metadataSet as $entityId => $entityMetadata) {
93
                if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) {
94
                    // Remove old entry and create a new one based on new entityId
95
                    unset($metadata[$set][$entityId]);
96
                    $newEntityId = $entityMetadata['entityid'];
97
                    $metadata[$set][$newEntityId] = $entityMetadata;
98
                }
99
            }
100
        }
101
    }
102
}
103