Completed
Push — master ( af42de...cc6223 )
by Florian
17s queued 11s
created

BaseFixture::getTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the feedback project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\DataFixtures\Base;
13
14
use App\Entity\Traits\AddressTrait;
0 ignored issues
show
Bug introduced by
The type App\Entity\Traits\AddressTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use App\Entity\Traits\PersonTrait;
0 ignored issues
show
Bug introduced by
The type App\Entity\Traits\PersonTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use App\Entity\Traits\ThingTrait;
0 ignored issues
show
Bug introduced by
The type App\Entity\Traits\ThingTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use App\Entity\Traits\UserTrait;
0 ignored issues
show
Bug introduced by
The type App\Entity\Traits\UserTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Doctrine\Bundle\FixturesBundle\Fixture;
19
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
20
use Doctrine\Common\Persistence\ObjectManager;
21
use Faker\Factory;
22
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
25
abstract class BaseFixture extends Fixture implements OrderedFixtureInterface, ContainerAwareInterface
26
{
27
    /* @var ContainerInterface $container */
28
    private $container;
29
30
    public function setContainer(ContainerInterface $container = null)
31
    {
32
        $this->container = $container;
33
    }
34
35
    protected function getTranslator()
36
    {
37
        return $this->container->get('translator');
38
    }
39
40
    /**
41
     * @return \Faker\Generator
42
     */
43
    protected function getFaker()
44
    {
45
        return Factory::create('de_CH');
46
    }
47
48
    /**
49
     * @param UserTrait $obj
50
     */
51
    protected function fillUser($obj)
52
    {
53
        $faker = $this->getFaker();
54
        $obj->setEmail($faker->email);
55
        $obj->setPlainPassword($faker->password);
56
        $obj->setPassword();
57
        $obj->setRegistrationDate(new \DateTime());
58
        $obj->setLastLogin(new \DateTime());
59
    }
60
61
    /**
62
     * @param AddressTrait $obj
63
     */
64
    protected function fillAddress($obj)
65
    {
66
        $faker = $this->getFaker();
67
        $obj->setStreet($faker->streetAddress);
68
        $obj->setStreetNr($faker->numberBetween(0, 300));
69
        if ($faker->numberBetween(0, 10) > 8) {
70
            $obj->setAddressLine($faker->streetAddress);
71
        }
72
        $obj->setPostalCode($faker->numberBetween(0, 9999));
73
        $obj->setCity($faker->city);
74
        $obj->setCountry($faker->countryCode);
75
    }
76
77
    /**
78
     * @param ThingTrait $obj
79
     */
80
    protected function fillThing($obj)
81
    {
82
        $faker = $this->getFaker();
83
        $obj->setName($faker->text(50));
84
        if ($faker->numberBetween(0, 10) > 5) {
85
            $obj->setDescription($faker->text(200));
86
        }
87
    }
88
89
    /**
90
     * @param PersonTrait $obj
91
     */
92
    protected function fillPerson($obj)
93
    {
94
        $faker = $this->getFaker();
95
        $obj->setGivenName($faker->firstName);
96
        $obj->setFamilyName($faker->lastName);
97
        if ($faker->numberBetween(0, 10) > 5) {
98
            $obj->setJobTitle($faker->jobTitle);
99
        }
100
    }
101
102
    /**
103
     * create random instances.
104
     *
105
     * @param $count
106
     *
107
     * @throws \Exception
108
     */
109
    protected function loadSomeRandoms(ObjectManager $manager, $count = 5)
110
    {
111
        for ($i = 0; $i < $count; ++$i) {
112
            $instance = $this->getRandomInstance();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $instance is correct as $this->getRandomInstance() targeting App\DataFixtures\Base\Ba...re::getRandomInstance() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
113
            if ($instance === null) {
114
                throw new \Exception('you need to override getRandomInstance to return an instance before you can use this function');
115
            }
116
            $manager->persist($instance);
117
        }
118
    }
119
120
    /**
121
     * create an instance with all random values.
122
     *
123
     * @return mixed
124
     */
125
    protected function getRandomInstance()
126
    {
127
        return null;
128
    }
129
}
130