Completed
Pull Request — 1.5 (#761)
by Paweł
17:05
created

ProcessArticleBodyCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2019 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2019 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Command;
18
19
use SWP\Bundle\ElasticSearchBundle\Criteria\Criteria;
20
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
class ProcessArticleBodyCommand extends ContainerAwareCommand
26
{
27
    protected static $defaultName = 'swp:article:process:body';
28
29
    protected function configure(): void
30
    {
31
        $this
32
            ->setName(self::$defaultName)
33
            ->setDescription('Finds articles by term and runs articles body processors on it.')
34
            ->addArgument('term', InputArgument::REQUIRED, 'Search term.')
35
            ->addOption('limit', null, InputArgument::OPTIONAL, 'Limit.', 10)
36
            ->addOption('offset', null, InputArgument::OPTIONAL, 'Offset.', 0)
37
            ->setHelp(<<<'EOT'
38
The <info>swp:article:process</info> finds articles by given term and runs article's body processors on it.
39
40
  <info>php %command.full_name% term embedded_image</info>
41
42
  <info>term</info> argument is the value of the string by which to find the articles.
43
44
EOT
45
            );
46
    }
47
48
    protected function execute(InputInterface $input, OutputInterface $output): void
49
    {
50
        $term = $input->getArgument('term');
51
52
        $currentTenant = $this->getContainer()->get('swp_multi_tenancy.tenant_context')->getTenant();
53
54
        $criteria = Criteria::fromQueryParameters(
55
            $term,
56
            [
57
                'sort' => ['publishedAt' => 'desc'],
58
                'tenantCode' => $currentTenant->getCode(),
59
            ]
60
        );
61
62
        $repositoryManager = $this->getContainer()->get('fos_elastica.manager');
63
        $repository = $repositoryManager->getRepository($this->getContainer()->getParameter('swp.model.article.class'));
64
        $articles = $repository
65
            ->findByCriteria($criteria)
66
            ->getResults((int) $input->getOption('offset'), (int) $input->getOption('limit'));
67
68
        $output->writeln('<bg=green;options=bold>There are total of '.$articles->getTotalHits().' articles.</>');
69
70
        $articleBodyProcessorChain = $this->getContainer()->get('swp_content_bundle.processor.article_body');
71
        $articleRepository = $this->getContainer()->get('swp.repository.article');
72
73
        foreach ($articles->toArray() as $article) {
74
            foreach ($article->getMedia() as $media) {
75
                $articleBodyProcessorChain->process($article, $media);
76
            }
77
        }
78
79
        $articleRepository->flush();
80
81
        $output->writeln('<bg=green;options=bold>Done. Processed '.\count($articles->toArray()).' articles.</>');
82
    }
83
}
84