zend-blog-3-backend

Форк
0
/
PostsBatchUpdateCommand.php 
133 строки · 3.6 Кб
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: morontt
5
 * Date: 04.05.16
6
 * Time: 23:01
7
 */
8

9
namespace App\Command;
10

11
use App\Entity\Post;
12
use App\Model\Image;
13
use App\Service\PictureTagBuilder;
14
use App\Service\TextProcessor;
15
use Doctrine\ORM\EntityManager;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21

22
class PostsBatchUpdateCommand extends Command
23
{
24
    /**
25
     * @var EntityManager
26
     */
27
    private $em;
28

29
    private TextProcessor $textProcessor;
30

31
    private PictureTagBuilder $ptb;
32

33
    /**
34
     * @param EntityManagerInterface $em
35
     * @param TextProcessor $textProcessor
36
     * @param PictureTagBuilder $ptb
37
     */
38
    public function __construct(EntityManagerInterface $em, TextProcessor $textProcessor, PictureTagBuilder $ptb)
39
    {
40
        parent::__construct();
41

42
        $this->em = $em;
43
        $this->ptb = $ptb;
44
        $this->textProcessor = $textProcessor;
45

46
        $em->getConfiguration()->setSQLLogger(null);
47
    }
48

49
    protected function configure(): void
50
    {
51
        $this
52
            ->setName('mtt:posts:update')
53
            ->setDescription('Batch update all posts')
54
            ->addArgument('articleId', InputArgument::OPTIONAL, 'article ID')
55
        ;
56
    }
57

58
    /**
59
     * @param InputInterface $input
60
     * @param OutputInterface $output
61
     *
62
     * @throws \Doctrine\ORM\ORMException
63
     * @throws \JsonException
64
     *
65
     * @return int
66
     */
67
    protected function execute(InputInterface $input, OutputInterface $output): int
68
    {
69
        $startTime = microtime(true);
70

71
        $postId = $input->getArgument('articleId');
72
        $repo = $this->em->getRepository(Post::class);
73
        if ($postId) {
74
            $postGenerator = function () use ($repo, $postId) {
75
                $post = $repo->find((int)$postId);
76
                if (!$post) {
77
                    return;
78
                }
79

80
                yield [$post];
81
            };
82
        } else {
83
            $postGenerator = function () use ($repo) {
84
                $i = 0;
85
                while (true) {
86
                    $posts = $repo->getPostsForIteration($i);
87

88
                    $i++;
89
                    if (!count($posts)) {
90
                        return;
91
                    }
92

93
                    yield $posts;
94
                }
95
            };
96
        }
97

98
        $cnt = 0;
99
        foreach ($postGenerator() as $posts) {
100
            /* @var Post $post */
101
            foreach ($posts as $post) {
102
                $cnt++;
103
                $this->textProcessor->processing($post);
104
                $this->em->flush();
105

106
                $media = $post->getDefaultImage();
107
                if ($media && $media->isImage()) {
108
                    $picture = $this->ptb->featuredPictureTag($media);
109
                    $media->setPictureTag($picture);
110

111
                    $srcSet = (new Image($media))->getSrcSet();
112
                    $srcSetData = $srcSet->toArray();
113
                    if (!empty($srcSetData)) {
114
                        $media->setSrcSet(json_encode($srcSetData, JSON_THROW_ON_ERROR));
115
                    }
116

117
                    $this->em->flush();
118
                }
119
            }
120
        }
121

122
        $output->writeln('');
123
        $output->writeln(sprintf('<info>Update <comment>%d</comment> topics</info>', $cnt));
124

125
        $endTime = microtime(true);
126

127
        $output->writeln(
128
            sprintf('<info>Total time: <comment>%s</comment> sec</info>', round($endTime - $startTime, 3))
129
        );
130

131
        return 0;
132
    }
133
}
134

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.