DeviceFingerprintManager::generateHashes()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Alpha\VisitorTrackingBundle\Manager;
6
7
use Alpha\VisitorTrackingBundle\Entity\Device;
8
9
class DeviceFingerprintManager
10
{
11
    private const HASHES = [
12
        'canvas',
13
        'fonts',
14
        'navigator',
15
        'plugins',
16
        'screen',
17
        'systemColors',
18
        'storedIds',
19
    ];
20
21
    public function generateHashes(Device $device): Device
22
    {
23
        $data = \json_decode($device->getFingerprint(), true);
24
25
        if (\is_array($data) && \JSON_ERROR_NONE === \json_last_error()) {
26
            foreach (self::HASHES as $field) {
27
                $method = 'set'.\ucfirst($field);
28
                $value = \array_key_exists($field, $data) ? \md5(\serialize($data[$field])) : null;
29
                $device->$method($value);
30
            }
31
        }
32
33
        return $device;
34
    }
35
}
36