13
13
14
14
use AppBundle \Entity \Comment ;
15
15
use AppBundle \Entity \Post ;
16
- use AppBundle \Entity \User ;
16
+ use AppBundle \Entity \Tag ;
17
17
use Doctrine \Common \DataFixtures \AbstractFixture ;
18
+ use Doctrine \Common \DataFixtures \DependentFixtureInterface ;
18
19
use Doctrine \Common \Persistence \ObjectManager ;
19
20
use Symfony \Component \DependencyInjection \ContainerAwareInterface ;
20
21
use Symfony \Component \DependencyInjection \ContainerAwareTrait ;
31
32
*
32
33
* @author Ryan Weaver <[email protected] >
33
34
* @author Javier Eguiluz <[email protected] >
35
+ * @author Yonel Ceruto <[email protected] >
34
36
*/
35
- class LoadFixtures extends AbstractFixture implements ContainerAwareInterface
37
+ class PostFixtures extends AbstractFixture implements DependentFixtureInterface, ContainerAwareInterface
36
38
{
37
39
use ContainerAwareTrait;
38
40
@@ -41,46 +43,23 @@ class LoadFixtures extends AbstractFixture implements ContainerAwareInterface
41
43
*/
42
44
public function load (ObjectManager $ manager )
43
45
{
44
- $ this ->loadUsers ($ manager );
45
- $ this ->loadPosts ($ manager );
46
- }
47
-
48
- private function loadUsers (ObjectManager $ manager )
49
- {
50
- $ passwordEncoder = $ this ->container ->get ('security.password_encoder ' );
51
-
52
- $ johnUser = new User ();
53
- $ johnUser ->setUsername ('john_user ' );
54
- $ johnUser->
setEmail (
'[email protected] ' );
55
- $ encodedPassword = $ passwordEncoder ->encodePassword ($ johnUser , 'kitten ' );
56
- $ johnUser ->setPassword ($ encodedPassword );
57
- $ manager ->persist ($ johnUser );
58
- $ this ->addReference ('john-user ' , $ johnUser );
59
-
60
- $ annaAdmin = new User ();
61
- $ annaAdmin ->setUsername ('anna_admin ' );
62
- $ annaAdmin->
setEmail (
'[email protected] ' );
63
- $ annaAdmin ->setRoles (['ROLE_ADMIN ' ]);
64
- $ encodedPassword = $ passwordEncoder ->encodePassword ($ annaAdmin , 'kitten ' );
65
- $ annaAdmin ->setPassword ($ encodedPassword );
66
- $ manager ->persist ($ annaAdmin );
67
- $ this ->addReference ('anna-admin ' , $ annaAdmin );
68
-
69
- $ manager ->flush ();
70
- }
46
+ $ phrases = $ this ->getPhrases ();
47
+ shuffle ($ phrases );
71
48
72
- private function loadPosts (ObjectManager $ manager )
73
- {
74
- foreach (range (1 , 30 ) as $ i ) {
49
+ foreach ($ phrases as $ i => $ title ) {
75
50
$ post = new Post ();
76
51
77
- $ post ->setTitle ($ this -> getRandomPostTitle () );
52
+ $ post ->setTitle ($ title );
78
53
$ post ->setSummary ($ this ->getRandomPostSummary ());
79
54
$ post ->setSlug ($ this ->container ->get ('slugger ' )->slugify ($ post ->getTitle ()));
80
55
$ post ->setContent ($ this ->getPostContent ());
56
+ // This reference has been added in UserFixtures class and contains
57
+ // an instance of User entity.
81
58
$ post ->setAuthor ($ this ->getReference ('anna-admin ' ));
82
59
$ post ->setPublishedAt (new \DateTime ('now - ' .$ i .'days ' ));
83
60
61
+ $ this ->addRandomTags ($ post );
62
+
84
63
foreach (range (1 , 5 ) as $ j ) {
85
64
$ comment = new Comment ();
86
65
@@ -99,6 +78,34 @@ private function loadPosts(ObjectManager $manager)
99
78
$ manager ->flush ();
100
79
}
101
80
81
+ /**
82
+ * This method must return an array of fixtures classes
83
+ * on which the implementing class depends on.
84
+ *
85
+ * @return array
86
+ */
87
+ public function getDependencies ()
88
+ {
89
+ return [
90
+ TagFixtures::class,
91
+ UserFixtures::class,
92
+ ];
93
+ }
94
+
95
+ private function addRandomTags (Post $ post )
96
+ {
97
+ if (0 === $ count = mt_rand (0 , 3 )) {
98
+ return ;
99
+ }
100
+
101
+ $ indexes = (array ) array_rand (TagFixtures::$ names , $ count );
102
+ foreach ($ indexes as $ index ) {
103
+ /** @var Tag $tag */
104
+ $ tag = $ this ->getReference ('tag- ' .$ index );
105
+ $ post ->addTag ($ tag );
106
+ }
107
+ }
108
+
102
109
private function getPostContent ()
103
110
{
104
111
return <<<'MARKDOWN'
@@ -157,16 +164,24 @@ private function getPhrases()
157
164
'Sed varius a risus eget aliquam ' ,
158
165
'Nunc viverra elit ac laoreet suscipit ' ,
159
166
'Pellentesque et sapien pulvinar consectetur ' ,
167
+ 'Ubi est barbatus nix ' ,
168
+ 'Abnobas sunt hilotaes de placidus vita ' ,
169
+ 'Ubi est audax amicitia ' ,
170
+ 'Eposs sunt solems de superbus fortis ' ,
171
+ 'Vae humani generis ' ,
172
+ 'Diatrias tolerare tanquam noster caesium ' ,
173
+ 'Teres talis orgias saepe tractare de camerarius flavum sensorem ' ,
174
+ 'Silva de secundus galatae demitto quadra ' ,
175
+ 'Sunt accentores vitare salvus flavum parses ' ,
176
+ 'Potus sensim ducunt ad ferox abnoba ' ,
177
+ 'Sunt seculaes transferre talis camerarius fluctuies ' ,
178
+ 'Era brevis ratione est ' ,
179
+ 'Sunt torquises imitari velox mirabilis medicinaes ' ,
180
+ 'Cum mineralis persuadere omnes finises desiderium bi-color ' ,
181
+ 'Bassus fatalis classiss virtualiter transferre de flavum ' ,
160
182
];
161
183
}
162
184
163
- private function getRandomPostTitle ()
164
- {
165
- $ titles = $ this ->getPhrases ();
166
-
167
- return $ titles [array_rand ($ titles )];
168
- }
169
-
170
185
private function getRandomPostSummary ($ maxLength = 255 )
171
186
{
172
187
$ phrases = $ this ->getPhrases ();
0 commit comments