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