Passed
Push — develop ( 34195e...b04746 )
by Stone
08:36 queued 03:47
created

CommentFixtures   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 17
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 26 4
A getDependencies() 0 5 1
1
<?php
2
3
namespace App\DataFixtures;
4
5
use App\Entity\Comment;
6
use App\Entity\Trick;
7
use App\Entity\User;
8
use Doctrine\Bundle\FixturesBundle\Fixture;
9
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
10
use Doctrine\Common\Persistence\ObjectManager;
11
use Faker\Factory;
12
13
14
class CommentFixtures extends Fixture implements DependentFixtureInterface
15
{
16
17
    /**
18
     * This method must return an array of fixtures classes
19
     * on which the implementing class depends on
20
     *
21
     * @return array
22
     */
23
    public function getDependencies()
24
    {
25
        return array(
26
            UserFixtures::class,
27
            TrickFixtures::class,
28
        );
29
    }
30
31
    /**
32
     * Load data fixtures with the passed EntityManager
33
     *
34
     * @param ObjectManager $manager
35
     */
36
    public function load(ObjectManager $manager)
37
    {
38
        $faker = Factory::create();
39
40
        $users = $manager->getRepository(User::class)->findAll();
41
        $tricks = $manager->getRepository(Trick::class)->findAll();
42
43
        /** @var Trick $trick */
44
        foreach ($tricks as $trick) {
45
            $commentNumber = rand(0, 40);
46
            if ($commentNumber > 0) {
47
                for ($i = 0; $i <= $commentNumber; $i++) {
48
                    ///** @var User $user */
49
                    //$user = $users[rand(0, count($users))];
50
                    $comment = new Comment();
51
                    $comment->setTrick($trick);
52
                    $comment->setUser($faker->randomElement($users));
53
                    $comment->setComment($faker->realText(rand(10,75)));
54
55
                    $manager->persist($comment);
56
57
                }
58
            }
59
        }
60
61
        $manager->flush();
62
    }
63
}