CrawlCommand::printCrawResult()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chemaclass\StockTicker\Infrastructure\Command;
6
7
use Chemaclass\StockTicker\Domain\Crawler\CrawlResult;
8
use Chemaclass\StockTicker\StockTickerFacade;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
final class CrawlCommand extends Command
15
{
16
    private const DEFAULT_MAX_NEWS_TO_FETCH = 2;
17
18 2
    protected function configure(): void
19
    {
20
        $this
21 2
            ->setName('crawl')
22 2
            ->setDescription('It crawls the websites and fetch their latest news')
23 2
            ->addArgument(
24 2
                'symbols',
25 2
                InputArgument::IS_ARRAY|InputArgument::REQUIRED,
26 2
                'Which stock symbols do you want to crawl?',
27
            )
28 2
            ->addOption(
29 2
                'maxNews',
30 2
                'm',
31 2
                InputArgument::OPTIONAL,
32 2
                'Max number of news to fetch per crawled site',
33 2
                self::DEFAULT_MAX_NEWS_TO_FETCH,
34
            );
35 2
    }
36
37 2
    protected function execute(InputInterface $input, OutputInterface $output): int
38
    {
39 2
        $symbols = (array) $input->getArgument('symbols');
40
41 2
        $output->writeln(sprintf('<comment>Crawling stock for symbols: %s</comment>', implode(', ', $symbols)));
42 2
        $facade = new StockTickerFacade();
43
44 2
        $crawlResult = $facade->crawlStock($symbols, (int) $input->getOption('maxNews'));
45
46 2
        if ($crawlResult->isEmpty()) {
47 1
            $output->writeln('<question>Nothing new here...</question>');
48
49 1
            return Command::INVALID;
50
        }
51
52 1
        $this->printCrawResult($output, $crawlResult);
53
54 1
        return Command::SUCCESS;
55
    }
56
57 1
    private function printCrawResult(OutputInterface $output, CrawlResult $crawlResult): void
58
    {
59 1
        $output->writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~');
60 1
        $output->writeln('~~~~~~ Crawl result ~~~~~~');
61 1
        $output->writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~');
62
63 1
        foreach ($crawlResult->getCompaniesGroupedBySymbol() as $symbol => $quote) {
64 1
            $output->writeln("Symbol: <options=bold,underscore>{$symbol}</>");
65
66 1
            foreach ($quote->toArray() as $key => $value) {
67 1
                $output->writeln(sprintf('# <comment>%s</comment> => <info>%s</info>', $key, json_encode($value)));
68
            }
69
70 1
            $output->writeln('');
71
        }
72 1
    }
73
}
74