TextGenerator::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 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