Passed
Push — develop ( 527962...2758c7 )
by BENARD
09:34
created

ArticleFixtures   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 5 1
A loadArticles() 0 9 2
A updateGeneratorType() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\ArticleBundle\DataFixtures;
6
7
use Doctrine\Bundle\FixturesBundle\Fixture;
8
use Doctrine\ORM\Mapping\ClassMetadataInfo;
9
use Doctrine\Persistence\ObjectManager;
10
use Exception;
11
use ProjetNormandie\ArticleBundle\Entity\Article;
12
use ProjetNormandie\ArticleBundle\ValueObject\ArticleStatus;
13
14
class ArticleFixtures extends Fixture
15
{
16
    /**
17
     * @var array<string>
18
     */
19
    private array $entities = [
20
        'Article',
21
    ];
22
23
24
    /**
25
     * @var array<mixed>
26
     */
27
    private array $articles = [
28
        [
29
            'id'    => 1,
30
            'status'    => ArticleStatus::UNDER_CONSTRUCTION,
31
            'author_id' => 1,
32
            'transtations' => [
33
                'fr' => [
34
                    'title' => 'FR',
35
                    'text' => 'FR'
36
                ],
37
                'en' => [
38
                    'title' => 'EN',
39
                    'text' => 'EN'
40
                ]
41
            ]
42
        ],
43
    ];
44
45
46
    private function updateGeneratorType(ObjectManager $manager): void
47
    {
48
        foreach ($this->entities as $entity) {
49
            $metadata = $manager->getClassMetaData("VideoGamesRecords\\CoreBundle\\Entity\\" . $entity);
50
            $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
51
        }
52
    }
53
54
    /**
55
     * @throws Exception
56
     */
57
    public function load(ObjectManager $manager): void
58
    {
59
        $this->updateGeneratorType($manager);
60
        $this->loadArticles($manager);
61
        $manager->flush();
62
    }
63
64
    /**
65
     * @param ObjectManager $manager
66
     */
67
    private function loadArticles(ObjectManager $manager): void
68
    {
69
        foreach ($this->articles as $row) {
70
            $article = new Article();
71
            $article->setId($row['id']);
72
            $article->setStatus($row['status']);
73
74
            $manager->persist($article);
75
            $this->addReference('article' . $article->getId(), $article);
76
        }
77
    }
78
}
79