TagFixtures::load()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 37
rs 8.9457
c 0
b 0
f 0
cc 6
nc 3
nop 1
1
<?php
2
3
namespace App\DataFixtures;
4
5
use App\Entity\Tag;
6
use App\Entity\Trick;
7
use Doctrine\Bundle\FixturesBundle\Fixture;
8
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
9
use Doctrine\Common\Persistence\ObjectManager;
10
use Faker\Factory;
11
12
class TagFixtures extends Fixture implements DependentFixtureInterface
13
{
14
15
    /**
16
     * This method must return an array of fixtures classes
17
     * on which the implementing class depends on
18
     *
19
     * @return array
20
     */
21
    public function getDependencies()
22
    {
23
        return array(
24
            TrickFixtures::class,
25
        );
26
    }
27
28
    /**
29
     * Load data fixtures with the passed EntityManager
30
     *
31
     * @param ObjectManager $manager
32
     */
33
    public function load(ObjectManager $manager)
34
    {
35
        $faker = Factory::create();
36
37
        $tricks = $manager->getRepository(Trick::class)->findAll();
38
        $tagRepository = $manager->getRepository(Tag::class);
39
40
        $fixedTag = new Tag();
41
        $fixedTag->setName("Starbug tag");
42
43
        foreach ($tricks as $trick){
44
            $maxTags = rand(0,5);
45
            if($maxTags >0){
46
                for($i=0; $i<=$maxTags; $i++){
47
48
                    $tagName = strtolower($faker->words(rand(1,3), true));
0 ignored issues
show
Bug introduced by
It seems like $faker->words(rand(1, 3), true) can also be of type array; however, parameter $str of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

48
                    $tagName = strtolower(/** @scrutinizer ignore-type */ $faker->words(rand(1,3), true));
Loading history...
49
                    //Avoid duplicate tags
50
                    $tag = $tagRepository->findOneBy(['name'=>$tagName]);
51
                    if($tag === null){
52
                        $tag=new Tag();
53
                        $tag->setName($tagName);
54
                        $manager->persist($tag);
55
                        $manager->flush(); //create the tag in DB
56
                    }
57
58
                    $trick->addTag($tag);
59
60
                    if(rand(0,6)===1){
61
                        $trick->addTag($fixedTag);
62
                    }
63
                }
64
            }
65
            $manager->persist($trick);
66
            $manager->flush(); //need to flush or next DB query for unicity will fail
67
        }
68
69
        $manager->flush();
70
71
    }
72
}