Assert::keysAre()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
nc 7
nop 3
dl 0
loc 45
rs 8.8497
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2018 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\GatewayBundle;
20
21
use Assert\Assertion;
22
use Surfnet\StepupGateway\GatewayBundle\Exception\AssertionFailedException;
23
24
/**
25
 * Note: this file is excluded from translation scanning because the partent
26
 * Assertion classes uses an aliasOf annotation not supported by
27
 * JMSTranslationBundle.
28
 */
29
final class Assert extends Assertion
30
{
31
    protected static $exceptionClass = \Surfnet\StepupGateway\GatewayBundle\Exception\AssertionFailedException::class;
32
33
    public static function keysAre(array $array, array $expectedKeys, $propertyPath = null): void
34
    {
35
        $givenKeys = array_keys($array);
36
37
        sort($givenKeys);
38
        sort($expectedKeys);
39
40
        if ($givenKeys === $expectedKeys) {
41
            return;
42
        }
43
44
        $givenCount = count($givenKeys);
45
        $expectedCount = count($expectedKeys);
46
47
        if ($givenCount < $expectedCount) {
48
            $message = sprintf(
49
                'Required keys "%s" are missing',
50
                implode('", "', array_diff($expectedKeys, $givenKeys))
51
            );
52
        } elseif ($givenCount > $expectedCount) {
53
            $message = sprintf(
54
                'Additional keys "%s" found',
55
                implode('", "', array_diff($givenKeys, $expectedKeys))
56
            );
57
        } else {
58
            $additional = array_diff($givenKeys, $expectedKeys);
59
            $required = array_diff($expectedKeys, $givenKeys);
60
61
            $message = 'Keys do not match requirements';
62
            if (!empty($additional)) {
63
                $message .= sprintf(
64
                    ', additional keys "%s" found',
65
                    implode('", "', array_diff($givenKeys, $expectedKeys))
66
                );
67
            }
68
69
            if (!empty($required)) {
70
                $message .= sprintf(
71
                    ', required keys "%s" are missing',
72
                    implode('", "', array_diff($expectedKeys, $givenKeys))
73
                );
74
            }
75
        }
76
77
        throw new AssertionFailedException($message, 0, $propertyPath, $array);
78
    }
79
}
80