Faker::__call()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
namespace PragmaRX\Random;
4
5
trait Faker
6
{
7
    protected $fakerClass = 'Faker\Factory';
8
9
    protected $faker;
10
11
    protected $fakerString;
12
13
    /**
14
     * Call faker.
15
     *
16
     * @param $name
17
     * @param $arguments
18
     * @return mixed
19
     * @throws \Exception
20
     */
21 2
    public function __call($name, $arguments)
22
    {
23
        try {
24 2
            $this->fakerString = $this->getFaker()->{$name}(...$arguments);
25 1
        } catch (\Error $e) {
26 1
            throw new \Exception('Faker is not installed. Call to undefined method PragmaRX\Random\Random::'.$name);
27
        }
28
29 1
        return $this;
30
    }
31
32
    /**
33
     * Instantiate and get Faker.
34
     *
35
     * @return \Faker\Generator|null
36
     */
37 2
    public function getFaker()
38
    {
39 2
        if (is_null($this->faker) && class_exists($this->fakerClass)) {
40 1
            $this->faker = call_user_func("$this->fakerClass::create");
41
        }
42
43 2
        return $this->faker;
44
    }
45
46
    /**
47
     * Set the faker class.
48
     *
49
     * @param $class
50
     * @return $this
51
     */
52 1
    public function fakerClass($class)
53
    {
54 1
        $this->fakerClass = $class;
55
56 1
        return $this;
57
    }
58
}
59