Passed
Push — master ( 879ca4...d0a2dc )
by Robbie
03:13
created

SAMLHelper::binToStrGuid()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 13

Duplication

Lines 9
Ratio 50 %

Importance

Changes 0
Metric Value
dl 9
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 8
nop 1
1
<?php
2
3
namespace SilverStripe\SAML\Helpers;
4
5
use SilverStripe\Core\Injector\Injectable;
6
use SilverStripe\SAML\Services\SAMLConfiguration;
7
use OneLogin_Saml2_Auth;
8
9
/**
10
 * Class SAMLHelper
11
 *
12
 * SAMLHelper acts as a simple wrapper for the OneLogin implementation, so that we can configure
13
 * and inject it via the config system.
14
 */
15
class SAMLHelper
16
{
17
    use Injectable;
18
19
    /**
20
     * @var array
21
     */
22
    public static $dependencies = [
23
        'SAMLConfService' => '%$' . SAMLConfiguration::class,
24
    ];
25
26
    /**
27
     * @var SAMLConfiguration
28
     */
29
    public $SAMLConfService;
30
31
    /**
32
     * @return OneLogin_Saml2_Auth
33
     */
34
    public function getSAMLauth()
35
    {
36
        $samlConfig = $this->SAMLConfService->asArray();
37
        return new OneLogin_Saml2_Auth($samlConfig);
38
    }
39
40
    /**
41
     * Checks if the string is a valid guid in the format of A98C5A1E-A742-4808-96FA-6F409E799937
42
     *
43
     * @param  string $guid
44
     * @return bool
45
     */
46
    public function validGuid($guid)
47
    {
48
        if (preg_match('/^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}?$/', $guid)) {
49
            return true;
50
        }
51
        return false;
52
    }
53
54
    /**
55
     * @param  string $object_guid
56
     * @return string
57
     */
58
    public function binToStrGuid($object_guid)
59
    {
60
        $hex_guid = bin2hex($object_guid);
61
        $hex_guid_to_guid_str = '';
62 View Code Duplication
        for ($k = 1; $k <= 4; ++$k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
64
        }
65
        $hex_guid_to_guid_str .= '-';
66 View Code Duplication
        for ($k = 1; $k <= 2; ++$k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
68
        }
69
        $hex_guid_to_guid_str .= '-';
70 View Code Duplication
        for ($k = 1; $k <= 2; ++$k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);
72
        }
73
        $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);
74
        $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);
75
        return strtoupper($hex_guid_to_guid_str);
76
    }
77
}
78