Issues (5)

src/DataFixtures/TrickImageFixtures.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\DataFixtures;
4
5
use App\Entity\Image;
6
use App\Entity\Tag;
7
use App\Entity\Trick;
8
use Doctrine\Bundle\FixturesBundle\Fixture;
9
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
10
use Doctrine\Common\Persistence\ObjectManager;
11
use Faker\Factory;
12
13
class TrickImageFixtures extends Fixture implements DependentFixtureInterface
14
{
15
16
    /**
17
     * This method must return an array of fixtures classes
18
     * on which the implementing class depends on
19
     *
20
     * @return array
21
     */
22
    public function getDependencies()
23
    {
24
        return array(
25
            TrickFixtures::class,
26
        );
27
    }
28
29
    /**
30
     * Load data fixtures with the passed EntityManager
31
     *
32
     * @param ObjectManager $manager
33
     */
34
    public function load(ObjectManager $manager)
35
    {
36
        $faker = Factory::create();
37
38
        $tricks = $manager->getRepository(Trick::class)->findAll();
39
40
41
        foreach ($tricks as $trick) {
42
            $maxTags = rand(0, 8);
43
            $primaryImageSet = false;
44
            if ($maxTags > 0) {
45
                for ($i = 0; $i <= $maxTags; $i++) {
46
                    $image = new Image();
47
48
                    $image->setTitle($faker->words(rand(1, 3), true));
0 ignored issues
show
It seems like $faker->words(rand(1, 3), true) can also be of type array; however, parameter $title of App\Entity\Image::setTitle() does only seem to accept null|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
                    $image->setTitle(/** @scrutinizer ignore-type */ $faker->words(rand(1, 3), true));
Loading history...
49
                    $image->setImage($faker->image('public/uploads/trick_images', 400, 300, null, false));
50
                    $image->setTrick($trick);
51
                    if(rand(0,2)>0 && !$primaryImageSet){
52
                        $image->setPrimaryImage(true);
53
                        $primaryImageSet = true;
54
                    }
55
56
                    $manager->persist($image);
57
                    $manager->persist($trick);
58
                }
59
            }
60
        }
61
        $manager->flush();
62
    }
63
}