Passed
Push — master ( 9792fc...04e5e0 )
by Tim
06:59
created

Metadata::__construct()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 19
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 31
rs 8.8333
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
        $this->fixEntityIds($metadataConfig);
27
28
        $checkMetadata = $moduleConfig->getValue('checkMetadata', true);
29
        if ($checkMetadata === true) {
30
            $metadata = $metadataConfig;
31
        } else {
32
            $metadata = [];
33
            if (is_array($checkMetadata)) {
34
                foreach ($checkMetadata as $set => $entityIds) {
35
                    if (array_key_exists($set, $metadataConfig)) {
36
                        foreach ($entityIds as $entityId) {
37
                            if (array_key_exists($entityId, $metadataConfig[$set])) {
38
                                $metadata[$set][$entityId] = $metadataConfig[$set][$entityId];
39
                            }
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
    /**
57
     * @return void
58
     */
59
    public function invokeTest(): void
60
    {
61
        $configuration = $this->getConfiguration();
62
        $output = [];
63
64
        foreach ($this->metadata as $set => $metadataSet) {
65
            foreach ($metadataSet as $entityId => $entityMetadata) {
66
                $input = [
67
                    'entityId' => $entityId,
68
                    'entityMetadata' => $entityMetadata
69
                ];
70
                $testData = new TestData($input);
71
72
                $metadataTest = new Metadata\Entity($configuration, $testData);
73
                $output[$entityId] = $metadataTest->getArrayizeTestResults();
74
75
                $this->addTestResults($metadataTest->getTestResults());
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
    /**
88
     * @param array $metadata
89
     *
90
     * @return void
91
     */
92
    private function fixEntityIds(array &$metadata): void
93
    {
94
        foreach ($metadata as $set => $metadataSet) {
95
            foreach ($metadataSet as $entityId => $entityMetadata) {
96
                if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) {
97
                    // Remove old entry and create a new one based on new entityId
98
                    unset($metadata[$set][$entityId]);
99
                    $newEntityId = $entityMetadata['entityid'];
100
                    $metadata[$set][$newEntityId] = $entityMetadata;
101
                }
102
            }
103
        }
104
    }
105
}
106