Passed
Branch monitor-2.5.x (8b654c)
by Tim
01:31
created

Entity   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getType() 0 10 5
B invokeTest() 0 59 6
1
<?php
2
3
namespace SimpleSAML\Modules\Monitor\TestSuite\Metadata;
4
5
use \SimpleSAML\Modules\Monitor\State as State;
6
use \SimpleSAML\Modules\Monitor\TestConfiguration as TestConfiguration;
7
use \SimpleSAML\Modules\Monitor\TestCase as TestCase;
8
use \SimpleSAML\Modules\Monitor\TestData as TestData;
9
use \SimpleSAML\Modules\Monitor\TestResult as TestResult;
10
11
final class Entity extends \SimpleSAML\Modules\Monitor\TestSuiteFactory
12
{
13
    /**
14
     * @var array
15
     */
16
    private $entityMetadata;
17
18
    /**
19
     * @var string
20
     */
21
    private $entityId;
22
23
    /**
24
     * @param integer|null;
25
     */
26
    private $certExpirationWarning = null;
27
28
    /**
29
     * @param TestConfiguration $configuration
30
     * @param TestData $testData
31
     */
32
    public function __construct($configuration, $testData)
33
    {
34
        $moduleConfig = $configuration->getModuleConfig();
35
        $entityMetadata = $testData->getInputItem('entityMetadata');
36
        $entityId = $testData->getInputItem('entityId');
37
38
        assert(is_array($entityMetadata));
39
        assert(is_string($entityId));
40
41
        $this->certExpirationWarning = $moduleConfig->getValue('certExpirationWarning', 28);
42
        $this->entityMetadata = $entityMetadata;
43
        $this->entityId = $entityId;
44
45
        $this->setCategory('Metadata entity');
46
        parent::__construct($configuration);
47
    }
48
49
    /**
50
     * @return void
51
     */
52
    public function invokeTest()
53
    {
54
        $input = [
55
            'entityId' => $this->entityId,
56
            'entityMetadata' => $this->entityMetadata,
57
        ];
58
        $testData = new TestData($input);
59
60
        $expTest = new TestCase\Metadata\Expiration($testData);
61
        $expTestResult = $expTest->getTestResult();
62
        $expTestResult->setSubject($this->entityId);
63
        $this->addTestResult($expTestResult);
64
65
        if (array_key_exists('keys', $this->entityMetadata)) {
66
            $keys = $this->entityMetadata['keys'];
67
            foreach ($keys as $key) {
68
                $input = array(
69
                    'category' => $this->getType($key),
70
                    'certData' => "-----BEGIN CERTIFICATE-----\n" .chunk_split($key['X509Certificate'], 64)."-----END CERTIFICATE-----\n",
71
                    'certExpirationWarning' => $this->certExpirationWarning,
72
                );
73
                $testData = new TestData($input);
74
75
                $certTest = new TestCase\Cert\Data($testData);
76
                $certTestResult = $certTest->getTestResult();
77
78
                $this->addTestResult($certTestResult);
79
            }
80
        } else {
81
            // saml20-idp-hosted
82
            $files = [];
83
            if (array_key_exists('certificate', $this->entityMetadata)) {
84
                $files[] = $this->entityMetadata['certificate'];
85
            }
86
            if (array_key_exists('new_certificate', $this->entityMetadata)) {
87
                $files[] = $this->entityMetadata['new_certificate'];
88
            }
89
90
            foreach ($files as $file) {
91
                $input = [
92
                    'category' => $this->getType(['signing' => true]),
93
                    'certFile' => \SimpleSAML\Utils\Config::getCertPath($file),
94
                    'certExpirationWarning' => $this->certExpirationWarning,
95
                ];
96
97
                $testData = new TestData($input);
98
99
                $certTest = new TestCase\Cert\File($testData);
100
                $certTestResult = $certTest->getTestResult();
101
102
                $this->addTestResult($certTestResult);
103
            }
104
        }
105
106
        $state = $this->calculateState();
107
108
        $testResult = new TestResult('Metadata endpoint');
109
        $testResult->setState($state);
110
        $this->setTestResult($testResult);
111
    }
112
113
    /**
114
     * @param array $key
115
     *
116
     * @return string
117
     */
118
    public function getType($key)
119
    {
120
        if ($key['encryption'] === true && $key['signing'] === false) {
121
            $category = 'Encryption certificate';
122
        } elseif ($key['encryption'] === false && $key['signing'] === true) {
123
            $category = 'Signing certificate';
124
        } else {
125
            $category = 'Unknown type';
126
        }
127
        return $category;
128
    }
129
}
130