Passed
Push — master ( a4e022...b6d9f7 )
by Tim
04:12
created

Entity   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
c 1
b 0
f 0
dl 0
loc 137
rs 10
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getSigning() 0 2 2
A getEncryption() 0 2 2
A getType() 0 10 5
B invokeTest() 0 64 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
    /** @var array */
14
    private $entityMetadata;
15
16
    /** @var string */
17
    private $entityId;
18
19
    /** @var integer|null */
20
    private $certExpirationWarning = null;
21
22
23
    /**
24
     * @param TestConfiguration $configuration
25
     * @param TestData $testData
26
     */
27
    public function __construct(TestConfiguration $configuration, TestData $testData)
28
    {
29
        $moduleConfig = $configuration->getModuleConfig();
30
        $entityMetadata = $testData->getInputItem('entityMetadata');
31
        $entityId = $testData->getInputItem('entityId');
32
33
        assert(is_array($entityMetadata));
34
        assert(is_string($entityId));
35
36
        $this->certExpirationWarning = $moduleConfig->getValue('certExpirationWarning', 28);
37
        $this->entityMetadata = $entityMetadata;
38
        $this->entityId = $entityId;
39
40
        $this->setCategory('Metadata entity');
41
        parent::__construct($configuration);
42
    }
43
44
45
    /**
46
     * @return void
47
     */
48
    public function invokeTest()
49
    {
50
        $input = [
51
            'entityId' => $this->entityId,
52
            'entityMetadata' => $this->entityMetadata,
53
        ];
54
        $testData = new TestData($input);
55
56
        $expTest = new TestCase\Metadata\Expiration($testData);
57
        $expTestResult = $expTest->getTestResult();
58
        $expTestResult->setSubject($this->entityId);
59
        $this->addTestResult($expTestResult);
60
61
        if (array_key_exists('keys', $this->entityMetadata)) {
62
            $keys = $this->entityMetadata['keys'];
63
64
65
            $signing = array_filter($keys, [self::class, 'getSigning']);
0 ignored issues
show
Unused Code introduced by
The assignment to $signing is dead and can be removed.
Loading history...
66
            $encryption = array_filter($keys, [self::class, 'getEncryption']);
0 ignored issues
show
Unused Code introduced by
The assignment to $encryption is dead and can be removed.
Loading history...
67
68
            foreach ($keys as $key) {
69
                $input = [
70
                    'category' => $this->getType($key),
71
                    'certData' => "-----BEGIN CERTIFICATE-----\n" .chunk_split($key['X509Certificate'], 64)."-----END CERTIFICATE-----\n",
72
                    'certExpirationWarning' => $this->certExpirationWarning,
73
                ];
74
                $testData = new TestData($input);
75
76
                $certTest = new TestCase\Cert\Data($testData);
77
                $certTestResult = $certTest->getTestResult();
78
79
                $this->addTestResult($certTestResult);
80
            }
81
        } else {
82
            // saml20-idp-hosted
83
            $files = [];
84
            if (array_key_exists('certificate', $this->entityMetadata)) {
85
                $files[] = $this->entityMetadata['certificate'];
86
            }
87
            if (array_key_exists('new_certificate', $this->entityMetadata)) {
88
                $files[] = $this->entityMetadata['new_certificate'];
89
            }
90
91
            foreach ($files as $file) {
92
                $input = [
93
                    'category' => $this->getType(['signing' => true, 'encryption' => false]),
94
                    'certFile' => \SimpleSAML\Utils\Config::getCertPath($file),
95
                    'certExpirationWarning' => $this->certExpirationWarning,
96
                ];
97
98
                $testData = new TestData($input);
99
100
                $certTest = new TestCase\Cert\File($testData);
101
                $certTestResult = $certTest->getTestResult();
102
103
                $this->addTestResult($certTestResult);
104
            }
105
        }
106
107
        $state = $this->calculateState();
108
109
        $testResult = new TestResult('Metadata endpoint');
110
        $testResult->setState($state);
111
        $this->setTestResult($testResult);
112
    }
113
114
115
    /**
116
     * @param array $key
117
     * @return bool
118
     */
119
    private function getSigning(array $key) {
120
        return ($key['signing'] === true) && ($key['encryption'] === false);
121
    }
122
123
124
    /**
125
     * @param array $key
126
     * @return bool
127
     */
128
    private function getEncryption(array $key) {
129
        return ($key['signing'] === false) && ($key['encryption'] === true);
130
    }
131
132
133
    /**
134
     * @param array $key
135
     *
136
     * @return string
137
     */
138
    public function getType(array $key)
139
    {
140
        if ($key['encryption'] === true && $key['signing'] === false) {
141
            $category = 'Encryption certificate';
142
        } elseif ($key['encryption'] === false && $key['signing'] === true) {
143
            $category = 'Signing certificate';
144
        } else {
145
            $category = 'Unknown type';
146
        }
147
        return $category;
148
    }
149
}
150