FakerGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A generate() 0 10 2
1
<?php
2
3
namespace WebnetFr\DatabaseAnonymizer\Generator;
4
5
use Faker\DefaultGenerator;
6
use Faker\Generator;
7
8
/**
9
 * Fake random value using Faker.
10
 *
11
 * @author Vlad Riabchenko <[email protected]>
12
 */
13
class FakerGenerator implements GeneratorInterface
14
{
15
    /**
16
     * @var Generator|DefaultGenerator
17
     */
18
    private $generator;
19
20
    /**
21
     * @var string
22
     */
23
    private $formatter;
24
25
    /**
26
     * @var array
27
     */
28
    private $arguments;
29
30
    /**
31
     * @var array
32
     */
33
    private $config;
34
35
    /**
36
     * @param Generator|DefaultGenerator $generator
37
     * @param string                     $formatter
38
     * @param array                      $arguments
39
     * @param array                      $config
40
     */
41
    public function __construct($generator, string $formatter, array $arguments = [], array $config = [])
42
    {
43
        $this->generator = $generator;
44
        $this->formatter = $formatter;
45
        $this->arguments = $arguments;
46
        $this->config = $config;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function generate()
53
    {
54
        $value = $this->generator->__call($this->formatter, $this->arguments);
55
        if ($value instanceof \DateTime) {
56
            $format = $this->config['date_format'] ?? null;
57
58
            return $value->format($format);
59
        }
60
61
        return $value;
62
    }
63
}
64