Passed
Pull Request — v3 (#685)
by Timothy
16:12
created

Nonce::includeNonce()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
nc 5
nop 2
dl 0
loc 18
ccs 0
cts 18
cp 0
crap 30
rs 9.4555
c 1
b 0
f 0
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\helpers;
13
14
use nystudio107\seomatic\Seomatic;
15
16
use Craft;
17
use craft\helpers\App;
18
19
/**
20
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
21
 * @package   Seomatic
22
 * @since     3.3.11
23
 */
24
class Nonce
25
{
26
    // Constants
27
    // =========================================================================
28
29
    const CSP_HEADERS = [
30
        'Content-Security-Policy',
31
        'X-Content-Security-Policy',
32
        'X-WebKit-CSP',
33
    ];
34
35
    // Static Methods
36
    // =========================================================================
37
38
    /**
39
     * @param string $nonce
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
40
     * @param string $cspDirective
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
41
     */
42
    private static function includeNonce(string $nonce, string $cspDirective)
0 ignored issues
show
Unused Code introduced by
The method includeNonce() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
Coding Style introduced by
Private method name "Nonce::includeNonce" must be prefixed with an underscore
Loading history...
43
    {
44
        $cspNonceType = self::getCspNonceType();
45
        if ($cspNonceType) {
46
            $cspValue = "{$cspDirective} 'nonce-$nonce'";
47
            foreach(self::CSP_HEADERS as $cspHeader) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
48
                switch ($cspNonceType) {
49
                    case 'tag':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
50
                        Craft::$app->getView()->registerMetaTag([
51
                            'httpEquiv' => $cspHeader,
52
                            'value' => $cspValue,
53
                        ]);
54
                        break;
55
                    case 'header':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
56
                        Craft::$app->getResponse()->getHeaders()->add($cspHeader, $cspValue . ';');
57
                        break;
58
                    default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
59
                        break;
60
                }
61
            }
62
        }
63
    }
64
65
    /**
66
     * @return string|null
67
     */
68
    private static function getCspNonceType()
0 ignored issues
show
Coding Style introduced by
Private method name "Nonce::getCspNonceType" must be prefixed with an underscore
Loading history...
69
    {
70
        $cspNonceType = !empty(Seomatic::$settings->cspNonce) ? strtolower(Seomatic::$settings->cspNonce) : null;
71
72
        return $cspNonceType;
73
    }
74
75
    /**
76
     * @return string|null
77
     */
78
    private static function getNonce()
0 ignored issues
show
Unused Code introduced by
The method getNonce() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
Coding Style introduced by
Private method name "Nonce::getNonce" must be prefixed with an underscore
Loading history...
79
    {
80
        $result = null;
81
        if (self::getCspNonceType() !== null) {
82
            try {
83
                $result = bin2hex(random_bytes(22));
84
            } catch (\Exception $e) {
85
                // That's okay
86
            }
87
        }
88
89
        return $result;
90
    }
91
92
}
93