TextGenerator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 10
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A __invoke() 0 6 2
1
<?php
2
namespace DBFaker\Generators;
3
4
use Doctrine\DBAL\Schema\Column;
5
use Faker\Factory;
6
use Faker\Generator;
7
8
class TextGenerator implements FakeDataGeneratorInterface
9
{
10
11
    /**
12
     * @var Generator
13
     */
14
    private $faker;
15
16
    /**
17
     * @var Column
18
     */
19
    private $column;
20
21
    /**
22
     * TextGenerator constructor.
23
     * @param Column $column
24
     * @param bool $generateUniqueValues
25
     */
26
    public function __construct(Column $column, bool $generateUniqueValues = false)
27
    {
28
        $this->faker = Factory::create();
29
        if ($generateUniqueValues) {
30
            $this->faker->unique();
31
        }
32
        $this->column = $column;
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function __invoke() : string
39
    {
40
        $colLength = $this->column->getLength();
41
        $maxLength = min($colLength, 300);
42
43
        return $colLength > 5 ? $this->faker->text($maxLength) : substr($this->faker->text(5), 0, $colLength - 1);
44
    }
45
}
46