Completed
Push — master ( 61b824...9bd84d )
by Aleksandr
04:57
created

Checksum::formKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
4
namespace carono\checksum;
5
6
/**
7
 * Class Checksum
8
 *
9
 * @package app\helpers
10
 */
11
class Checksum
12
{
13
    /**
14
     * @param $array
15
     * @param null $salt
16
     * @return string
17
     */
18
    public static function calculate($array, $salt = null)
19
    {
20
        if ($key = static::formKey($array)) {
21
            return hash("sha256", $key . $salt);
22
        } else {
23
            return null;
24
        }
25
    }
26
27
    /**
28
     * @param $array
29
     * @param $hash
30
     * @param null $salt
31
     * @return bool
32
     */
33
    public static function validate($array, $hash, $salt = null)
34
    {
35
        $realHash = static::calculate($array, $salt);
36
        return $realHash == $hash;
37
    }
38
39
    /**
40
     * @param $array
41
     * @return array
42
     */
43
    public static function formKeyPartials($array)
44
    {
45
        $result = [];
46
        foreach ((array)$array as $model => $values) {
47
            if (is_array($values)) {
48
                foreach ($values as $key => $value) {
49
                    if (is_array($value)) {
50
                        foreach ($value as $subKey => $subValue) {
51
//                            $result[] = join('=', [$model,is_numeric($key) ? "{$value}[{$subKey}]" : "{$key}[{$subKey}]"]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
                            $result[] = join('=', [$model, is_numeric($key) ? $value : $key]);
53
                            break;
54
                        }
55
                    } else {
56
                        $result[] = join('=', [$model, is_numeric($key) ? $value : $key]);
57
                    }
58
                }
59
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
60
                // regular variables
61
            }
62
        }
63
        $result = array_unique($result);
64
        sort($result);
65
        return $result;
66
    }
67
68
    /**
69
     * @param $array
70
     * @return string
71
     */
72
    public static function formKey($array)
73
    {
74
        $result = join('|', static::formKeyPartials($array));
75
        return $result;
76
    }
77
}