Passed
Pull Request — master (#49)
by Alexander
25:24 queued 10:30
created

AddCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 2
c 1
b 1
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace App\Command\Fixture;
4
5
use App\Blog\Entity\Comment;
6
use App\Blog\Entity\Post;
7
use App\Blog\Entity\Tag;
8
use App\Entity\User;
9
use App\Blog\Tag\TagRepository;
10
use Cycle\ORM\ORMInterface;
11
use Cycle\ORM\Transaction;
12
use Faker\Factory;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
use Yiisoft\Yii\Console\ExitCode;
19
20
class AddCommand extends Command
21
{
22
    private ORMInterface $orm;
23
24
    protected static $defaultName = 'fixture/add';
25
26
    public function __construct(ORMInterface $orm)
27
    {
28
        parent::__construct();
29
        $this->orm = $orm;
30
    }
31
32
    public function configure(): void
33
    {
34
        $this
35
            ->setDescription('Add fixtures')
36
            ->setHelp('This command adds random content')
37
            ->addArgument('count', InputArgument::OPTIONAL, 'Count', 10);
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output): int
41
    {
42
        $io = new SymfonyStyle($input, $output);
43
44
        $count = $input->getArgument('count');
45
        // get faker
46
        if (!class_exists(Factory::class)) {
47
            $io->error('Faker should be installed. Run `composer install --dev`');
48
            return ExitCode::UNSPECIFIED_ERROR;
49
        }
50
        $faker = Factory::create();
51
52
        // users
53
        $users = [];
54
        for ($i = 0; $i <= $count; ++$i) {
55
            $user = new User();
56
            $user->setLogin($login = $faker->firstName . rand(0, 9999));
57
            $user->setPassword($login);
58
            $users[] = $user;
59
        }
60
        // tags
61
        /** @var TagRepository $tagRepository */
62
        $tagRepository = $this->orm->getRepository(Tag::class);
63
        $tags = [];
64
        $tagWords = [];
65
        for ($i = 0, $fails = 0; $i <= $count; ++$i) {
66
            $word = $faker->word();
67
            if (in_array($word, $tagWords, true)) {
68
                --$i;
69
                ++$fails;
70
                if ($fails >= $count) {
71
                    break;
72
                }
73
                continue;
74
            }
75
            $tagWords[] = $word;
76
            $tag = $tagRepository->getOrCreate($word);
77
            $tags[] = $tag;
78
        }
79
        // posts
80
        for ($i = 0; $i <= $count; ++$i) {
81
            /** @var User $postUser */
82
            $postUser = $users[array_rand($users)];
83
            $post = new Post();
84
            $post->setUser($postUser);
85
            $postUser->addPost($post);
86
            $post->setTitle($faker->text(64));
87
            $post->setContent($faker->realText(4000));
88
            $public = rand(0, 2) > 0;
89
            $post->setPublic($public);
90
            if ($public) {
91
                $post->setPublishedAt(new \DateTimeImmutable(date('r', rand(time(), strtotime('-2 years')))));
92
            }
93
            // tags
94
            $postTags = (array)array_rand($tags, rand(1, count($tags)));
95
            foreach ($postTags as $tagId) {
96
                $post->addTag($tags[$tagId]);
97
            }
98
            // comments
99
            $commentsCount = rand(0, $count);
100
            for ($j = 0; $j <= $commentsCount; ++$j) {
101
                $comment = new Comment();
102
                $comment->setContent($faker->realText(500));
103
                $commentPublic = rand(0, 3) > 0;
104
                $comment->setPublic($commentPublic);
105
                if ($commentPublic) {
106
                    $comment->setPublishedAt(new \DateTimeImmutable(date('r', rand(time(), strtotime('-1 years')))));
107
                }
108
                $commentUser = $users[array_rand($users)];
109
                $comment->setUser($commentUser);
110
                $post->addComment($comment);
111
            }
112
        }
113
114
        try {
115
            $transaction = new Transaction($this->orm);
116
            foreach ($users as $user) {
117
                $transaction->persist($user);
118
            }
119
            $transaction->run();
120
            $io->success('Done');
121
        } catch (\Throwable $t) {
122
            $io->error($t->getMessage());
123
            return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR;
124
        }
125
126
        return ExitCode::OK;
127
    }
128
}
129