Completed
Push — master ( 857129...3b32ca )
by Rafał
10:38
created

ProcessArticleBodyCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 5
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 18 1
A execute() 0 34 3
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 FOS\ElasticaBundle\Manager\RepositoryManagerInterface;
20
use SWP\Bundle\ElasticSearchBundle\Criteria\Criteria;
21
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
class ProcessArticleBodyCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
27
{
28
    protected static $defaultName = 'swp:article:process:body';
29
30
    public function __construct(RepositoryManagerInterface $repositoryManager)
31
    {
32
        $this->repositoryManager = $repositoryManager;
0 ignored issues
show
Bug introduced by
The property repositoryManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
34
        parent::__construct();
35
    }
36
37
    protected function configure(): void
38
    {
39
        $this
40
            ->setName(self::$defaultName)
41
            ->setDescription('Finds articles by term and runs articles body processors on it.')
42
            ->addArgument('term', InputArgument::REQUIRED, 'Search term.')
43
            ->addOption('limit', null, InputArgument::OPTIONAL, 'Limit.', 10)
44
            ->addOption('offset', null, InputArgument::OPTIONAL, 'Offset.', 0)
45
            ->setHelp(<<<'EOT'
46
The <info>swp:article:process</info> finds articles by given term and runs article's body processors on it.
47
48
  <info>php %command.full_name% term embedded_image</info>
49
50
  <info>term</info> argument is the value of the string by which to find the articles.
51
52
EOT
53
            );
54
    }
55
56
    protected function execute(InputInterface $input, OutputInterface $output): void
57
    {
58
        $term = $input->getArgument('term');
59
60
        $currentTenant = $this->getContainer()->get('swp_multi_tenancy.tenant_context')->getTenant();
61
62
        $criteria = Criteria::fromQueryParameters(
63
            $term,
64
            [
65
                'sort' => ['publishedAt' => 'desc'],
66
                'tenantCode' => $currentTenant->getCode(),
67
            ]
68
        );
69
70
        $repository = $this->repositoryManager->getRepository($this->getContainer()->getParameter('swp.model.article.class'));
71
        $articles = $repository
72
            ->findByCriteria($criteria)
73
            ->getResults((int) $input->getOption('offset'), (int) $input->getOption('limit'));
74
75
        $output->writeln('<bg=green;options=bold>There are total of '.$articles->getTotalHits().' articles.</>');
76
77
        $articleBodyProcessorChain = $this->getContainer()->get('swp_content_bundle.processor.article_body');
78
        $articleRepository = $this->getContainer()->get('swp.repository.article');
79
80
        foreach ($articles->toArray() as $article) {
81
            foreach ($article->getMedia() as $media) {
82
                $articleBodyProcessorChain->process($article, $media);
83
            }
84
        }
85
86
        $articleRepository->flush();
87
88
        $output->writeln('<bg=green;options=bold>Done. Processed '.\count($articles->toArray()).' articles.</>');
89
    }
90
}
91