Metadata::invokeTest()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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