BaseFixture   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 11
dl 0
loc 49
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRandomInstance() 0 3 1
A getTranslator() 0 3 1
A loadSomeRandoms() 0 8 3
A setContainer() 0 3 1
A getFaker() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the thealternativezurich/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 Doctrine\Bundle\FixturesBundle\Fixture;
15
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
16
use Doctrine\Persistence\ObjectManager;
17
use Faker\Factory;
18
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
21
abstract class BaseFixture extends Fixture implements OrderedFixtureInterface, ContainerAwareInterface
22
{
23
    /* @var ContainerInterface $container */
24
    private $container;
25
26
    public function setContainer(ContainerInterface $container = null)
27
    {
28
        $this->container = $container;
29
    }
30
31
    protected function getTranslator()
32
    {
33
        return $this->container->get('translator');
34
    }
35
36
    /**
37
     * @return \Faker\Generator
38
     */
39
    protected function getFaker()
40
    {
41
        return Factory::create('de_CH');
42
    }
43
44
    /**
45
     * create random instances.
46
     *
47
     * @param $count
48
     *
49
     * @throws \Exception
50
     */
51
    protected function loadSomeRandoms(ObjectManager $manager, $count = 5)
52
    {
53
        for ($i = 0; $i < $count; ++$i) {
54
            $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...
55
            if (null === $instance) {
56
                throw new \Exception('you need to override getRandomInstance to return an instance before you can use this function');
57
            }
58
            $manager->persist($instance);
0 ignored issues
show
Bug introduced by
$instance of type void is incompatible with the type object expected by parameter $object of Doctrine\Persistence\ObjectManager::persist(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            $manager->persist(/** @scrutinizer ignore-type */ $instance);
Loading history...
59
        }
60
    }
61
62
    /**
63
     * create an instance with all random values.
64
     *
65
     * @return mixed
66
     */
67
    protected function getRandomInstance()
68
    {
69
        return null;
70
    }
71
}
72