1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Command\Fixture; |
6
|
|
|
|
7
|
|
|
use App\Blog\Entity\Comment; |
8
|
|
|
use App\Blog\Entity\Post; |
9
|
|
|
use App\Blog\Entity\Tag; |
10
|
|
|
use App\Blog\Tag\TagRepository; |
11
|
|
|
use App\User\User; |
12
|
|
|
use Faker\Factory; |
13
|
|
|
use Faker\Generator; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
19
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
20
|
|
|
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy; |
21
|
|
|
use Yiisoft\Yii\Cycle\Data\Writer\EntityWriter; |
22
|
|
|
|
23
|
|
|
class AddCommand extends Command |
24
|
|
|
{ |
25
|
|
|
protected static $defaultName = 'fixture/add'; |
26
|
|
|
|
27
|
|
|
private CycleDependencyProxy $promise; |
28
|
|
|
private Generator $faker; |
29
|
|
|
/** @var User[] */ |
30
|
|
|
private array $users = []; |
31
|
|
|
/** @var Tag[] */ |
32
|
|
|
private array $tags = []; |
33
|
|
|
|
34
|
|
|
private const DEFAULT_COUNT = 10; |
35
|
|
|
|
36
|
|
|
public function __construct(CycleDependencyProxy $promise) |
37
|
|
|
{ |
38
|
|
|
$this->promise = $promise; |
39
|
|
|
parent::__construct(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function configure(): void |
43
|
|
|
{ |
44
|
|
|
$this |
45
|
|
|
->setDescription('Add fixtures') |
46
|
|
|
->setHelp('This command adds random content') |
47
|
|
|
->addArgument('count', InputArgument::OPTIONAL, 'Count', self::DEFAULT_COUNT); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
51
|
|
|
{ |
52
|
|
|
$io = new SymfonyStyle($input, $output); |
53
|
|
|
|
54
|
|
|
$count = (int)$input->getArgument('count'); |
55
|
|
|
// get faker |
56
|
|
|
if (!class_exists(Factory::class)) { |
57
|
|
|
$io->error('Faker should be installed. Run `composer install --dev`'); |
58
|
|
|
return ExitCode::UNSPECIFIED_ERROR; |
59
|
|
|
} |
60
|
|
|
$this->faker = Factory::create(); |
61
|
|
|
|
62
|
|
|
try { |
63
|
|
|
$this->addUsers($count); |
64
|
|
|
$this->addTags($count); |
65
|
|
|
$this->addPosts($count); |
66
|
|
|
|
67
|
|
|
$this->saveEntities(); |
68
|
|
|
} catch (\Throwable $t) { |
69
|
|
|
$io->error($t->getMessage()); |
70
|
|
|
return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR; |
71
|
|
|
} |
72
|
|
|
$io->success('Done'); |
73
|
|
|
return ExitCode::OK; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function saveEntities(): void |
77
|
|
|
{ |
78
|
|
|
(new EntityWriter($this->promise->getORM()))->write($this->users); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function addUsers(int $count): void |
82
|
|
|
{ |
83
|
|
|
for ($i = 0; $i < $count; ++$i) { |
84
|
|
|
$login = $this->faker->firstName . rand(0, 9999); |
85
|
|
|
$user = new User($login, $login); |
86
|
|
|
$this->users[] = $user; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function addTags(int $count): void |
91
|
|
|
{ |
92
|
|
|
/** @var TagRepository $tagRepository */ |
93
|
|
|
$tagRepository = $this->promise->getORM()->getRepository(Tag::class); |
94
|
|
|
$this->tags = []; |
95
|
|
|
$tagWords = []; |
96
|
|
|
for ($i = 0, $fails = 0; $i < $count; ++$i) { |
97
|
|
|
$word = $this->faker->word(); |
98
|
|
|
if (in_array($word, $tagWords, true)) { |
99
|
|
|
--$i; |
100
|
|
|
++$fails; |
101
|
|
|
if ($fails >= $count) { |
102
|
|
|
break; |
103
|
|
|
} |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
$tagWords[] = $word; |
107
|
|
|
$tag = $tagRepository->getOrCreate($word); |
108
|
|
|
$this->tags[] = $tag; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function addPosts(int $count): void |
113
|
|
|
{ |
114
|
|
|
if (count($this->users) === 0) { |
115
|
|
|
throw new \Exception('No users'); |
116
|
|
|
} |
117
|
|
|
for ($i = 0; $i < $count; ++$i) { |
118
|
|
|
/** @var User $postUser */ |
119
|
|
|
$postUser = $this->users[array_rand($this->users)]; |
120
|
|
|
$post = new Post($this->faker->text(64), $this->faker->realText(rand(1000, 4000))); |
121
|
|
|
$postUser->addPost($post); |
122
|
|
|
$public = rand(0, 2) > 0; |
123
|
|
|
$post->setPublic($public); |
124
|
|
|
if ($public) { |
125
|
|
|
$post->setPublishedAt(new \DateTimeImmutable(date('r', rand(time(), strtotime('-2 years'))))); |
126
|
|
|
} |
127
|
|
|
// link tags |
128
|
|
|
$postTags = (array)array_rand($this->tags, rand(1, count($this->tags))); |
129
|
|
|
foreach ($postTags as $tagId) { |
130
|
|
|
$tag = $this->tags[$tagId]; |
131
|
|
|
$post->addTag($tag); |
132
|
|
|
// todo: uncomment when issue is resolved https://github.com/cycle/orm/issues/70 |
133
|
|
|
// $tag->addPost($post); |
134
|
|
|
} |
135
|
|
|
// add comments |
136
|
|
|
$commentsCount = rand(0, $count); |
137
|
|
|
for ($j = 0; $j <= $commentsCount; ++$j) { |
138
|
|
|
$comment = new Comment($this->faker->realText(rand(100, 500))); |
139
|
|
|
$commentPublic = rand(0, 3) > 0; |
140
|
|
|
$comment->setPublic($commentPublic); |
141
|
|
|
if ($commentPublic) { |
142
|
|
|
$comment->setPublishedAt(new \DateTimeImmutable(date('r', rand(time(), strtotime('-1 years'))))); |
143
|
|
|
} |
144
|
|
|
$commentUser = $this->users[array_rand($this->users)]; |
145
|
|
|
$commentUser->addComment($comment); |
146
|
|
|
$comment->setPost($post); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|