Provider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 75
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 8 2
A resetUniqueGenerator() 0 4 1
A getFakerData() 0 14 3
A seedRng() 0 4 1
A resetRng() 0 6 1
1
<?php
2
/**
3
 * integer_net Magento Module
4
 *
5
 * @category   IntegerNet
6
 * @package    IntegerNet_Anonymizer
7
 * @copyright  Copyright (c) 2015 integer_net GmbH (http://www.integer-net.de/)
8
 * @author     Fabian Schmengler <[email protected]>
9
 */
10
11
namespace IntegerNet\Anonymizer;
12
13
14
use Faker\Factory;
15
16
class Provider
17
{
18
    const __CLASS = __CLASS__;
19
20
    /**
21
     * @var \Faker\Generator
22
     */
23
    private $faker;
24
25
    private $salt;
26
27
    /**
28
     * Initializes Faker, generates new salt for RNG seeds
29
     *
30
     * @param string|null $locale
31
     * @return void
32
     */
33
    public function initialize($locale = null)
34
    {
35
        if ($locale === null) {
36
            $locale = Factory::DEFAULT_LOCALE;
37
        }
38
        $this->faker = Factory::create($locale);
39
        $this->salt = sha1(uniqid('', true));
40
    }
41
42
    /**
43
     * Resets the UniqueGenerator of Faker, this should be used after anonymizing a database table with
44
     * unique values to free memory and allow the same values in other tables
45
     *
46
     * @return void
47
     */
48
    public function resetUniqueGenerator()
49
    {
50
        $this->faker->unique(true);
51
    }
52
53
    /**
54
     * Return fake data from given Faker provider, always return the same data for each ($formatter, $identifier)
55
     * combination after initialized.
56
     *
57
     * @param $formatter
58
     * @param $identifier
59
     * @return mixed
60
     */
61
    public function getFakerData($formatter, $identifier, $unique = false)
62
    {
63
        $faker = $this->faker;
64
        if ($formatter === 'null') {
65
            return $faker->optional(0)->randomDigit;
66
        }
67
        if ($unique) {
68
            $faker = $faker->unique();
69
        }
70
        $this->seedRng($formatter.$identifier);
71
        $result = $faker->format($formatter);
72
        $this->resetRng();
73
        return $result;
74
    }
75
76
    /**
77
     * @param $identifier
78
     */
79
    private function seedRng($identifier)
80
    {
81
        $this->faker->seed(hexdec(hash("crc32b", $identifier . $this->salt)));
82
    }
83
84
    private function resetRng()
85
    {
86
        //$this->faker->seed();
87
        //TODO use above as soon as pr 543 has been merged
88
        mt_srand();
89
    }
90
}