|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\DataFixtures; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Article; |
|
6
|
|
|
use App\Entity\Comment; |
|
7
|
|
|
use App\Entity\Tag; |
|
8
|
|
|
use App\Entity\User; |
|
9
|
|
|
use Doctrine\Bundle\FixturesBundle\Fixture; |
|
10
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
11
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* AppFixtures. |
|
15
|
|
|
*/ |
|
16
|
|
|
class AppFixtures extends Fixture |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var UserPasswordEncoderInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $passwordEncoder; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param UserPasswordEncoderInterface $passwordEncoder |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(UserPasswordEncoderInterface $passwordEncoder) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->passwordEncoder = $passwordEncoder; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function load(ObjectManager $manager) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->loadUsers($manager); |
|
34
|
|
|
$this->loadTags($manager); |
|
35
|
|
|
$this->loadArticles($manager); |
|
36
|
|
|
$this->loadComments($manager); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function loadUsers(ObjectManager $manager) |
|
40
|
|
|
{ |
|
41
|
|
|
foreach ($this->getUserData() as [$reference, $email, $username, $password, $image, $bio, $follow]) { |
|
42
|
|
|
$user = new User(); |
|
43
|
|
|
$user->setEmail($email); |
|
44
|
|
|
$user->setUsername($username); |
|
45
|
|
|
$user->setPassword($this->passwordEncoder->encodePassword($user, $password)); |
|
46
|
|
|
$user->setImage($image); |
|
47
|
|
|
$user->setBio($bio); |
|
48
|
|
|
if ($follow) { |
|
49
|
|
|
$user->follow($this->getReference($follow)); |
|
50
|
|
|
} |
|
51
|
|
|
$manager->persist($user); |
|
52
|
|
|
$this->addReference($reference, $user); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$manager->flush(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function loadTags(ObjectManager $manager): void |
|
59
|
|
|
{ |
|
60
|
|
|
foreach ($this->getTagData() as [$reference, $name]) { |
|
61
|
|
|
$tag = new Tag(); |
|
62
|
|
|
$tag->setName($name); |
|
63
|
|
|
$manager->persist($tag); |
|
64
|
|
|
$this->addReference($reference, $tag); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$manager->flush(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function loadArticles(ObjectManager $manager): void |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($this->getArticleData() as [$reference, $title, $description, $body, $author, $tag, $favor]) { |
|
73
|
|
|
$article = new Article(); |
|
74
|
|
|
$article->setAuthor($this->getReference($author)); |
|
75
|
|
|
$article->setTitle($title); |
|
76
|
|
|
$article->setDescription($description); |
|
77
|
|
|
$article->setBody($body); |
|
78
|
|
|
$article->setTags([$this->getReference($tag)]); |
|
79
|
|
|
if ($favor && ($favor = $this->getReference($favor))) { |
|
80
|
|
|
$favor->addToFavorites($article); |
|
81
|
|
|
} |
|
82
|
|
|
$manager->persist($article); |
|
83
|
|
|
$this->addReference($reference, $article); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$manager->flush(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function loadComments(ObjectManager $manager): void |
|
90
|
|
|
{ |
|
91
|
|
|
foreach ($this->getCommentData() as [$reference, $article, $author, $body]) { |
|
92
|
|
|
$comment = new Comment(); |
|
93
|
|
|
$comment->setAuthor($this->getReference($author)); |
|
94
|
|
|
$comment->setArticle($this->getReference($article)); |
|
95
|
|
|
$comment->setBody($body); |
|
96
|
|
|
$manager->persist($comment); |
|
97
|
|
|
$this->addReference($reference, $comment); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$manager->flush(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function getUserData(): array |
|
104
|
|
|
{ |
|
105
|
|
|
return [ |
|
106
|
|
|
['user-1', '[email protected]', 'user1', 'password', 'https://www.shareicon.net/male-headset-young-man-person-support-avatar-106428', 'Bio', null], |
|
107
|
|
|
['user-2', '[email protected]', 'user2', 'password', null, null, 'user-1'], |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function getTagData(): array |
|
112
|
|
|
{ |
|
113
|
|
|
return [ |
|
114
|
|
|
['tag-1', 'lorem'], |
|
115
|
|
|
['tag-2', 'ipsum'], |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function getArticleData(): array |
|
120
|
|
|
{ |
|
121
|
|
|
return [ |
|
122
|
|
|
['article-1', 'Article #1', 'Description #1', 'Body #1', 'user-1', 'tag-1', null], |
|
123
|
|
|
['article-2', 'Article #2', 'Description #2', 'Body #2', 'user-2', 'tag-2', 'user-1'], |
|
124
|
|
|
]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private function getCommentData(): array |
|
128
|
|
|
{ |
|
129
|
|
|
return [ |
|
130
|
|
|
['comment-1', 'article-1', 'user-2', 'Comment #1'], |
|
131
|
|
|
['comment-2', 'article-2', 'user-1', 'Comment #2'], |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|