Passed
Push — master ( c0f32c...e879f4 )
by Chema
04:17 queued 41s
created

CrawlCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 35
c 1
b 0
f 0
dl 0
loc 65
ccs 37
cts 37
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
A printCrawResult() 0 14 3
A createStockTickerFacade() 0 4 1
A execute() 0 19 2
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\StockTickerConfig;
9
use Chemaclass\StockTicker\StockTickerFacade;
10
use Chemaclass\StockTicker\StockTickerFactory;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
final class CrawlCommand extends Command
17
{
18
    private const DEFAULT_MAX_NEWS_TO_FETCH = 2;
19
20 2
    protected function configure(): void
21
    {
22
        $this
23 2
            ->setName('crawl')
24 2
            ->setDescription('It crawls the websites and fetch their latest news')
25 2
            ->addArgument(
26 2
                'symbols',
27 2
                InputArgument::IS_ARRAY|InputArgument::REQUIRED,
28 2
                'Which stock symbols do you want to crawl?'
29
            )
30 2
            ->addOption(
31 2
                'maxNews',
32 2
                'm',
33 2
                InputArgument::OPTIONAL,
34 2
                'Which stock symbols do you want to crawl?',
35 2
                self::DEFAULT_MAX_NEWS_TO_FETCH
36
            );
37 2
    }
38
39 2
    protected function execute(InputInterface $input, OutputInterface $output): int
40
    {
41 2
        $symbols = (array) $input->getArgument('symbols');
42
43 2
        $txt = sprintf('Crawling stock %s...', implode(', ', $symbols));
44 2
        $output->writeln($txt);
45 2
        $facade = $this->createStockTickerFacade();
46
47 2
        $crawlResult = $facade->crawlStock($symbols, (int) $input->getOption('maxNews'));
48
49 2
        if ($crawlResult->isEmpty()) {
50 1
            $output->writeln('Nothing new here...');
51
52 1
            return Command::FAILURE;
53
        }
54
55 1
        $this->printCrawResult($output, $crawlResult);
56
57 1
        return Command::SUCCESS;
58
    }
59
60 2
    private function createStockTickerFacade(): StockTickerFacade
61
    {
62 2
        return new StockTickerFacade(
63 2
            new StockTickerFactory(new StockTickerConfig())
64
        );
65
    }
66
67 1
    private function printCrawResult(OutputInterface $output, CrawlResult $crawlResult): void
68
    {
69 1
        $output->writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~');
70 1
        $output->writeln('~~~~~~ Crawl result ~~~~~~');
71 1
        $output->writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~');
72
73 1
        foreach ($crawlResult->getCompaniesGroupedBySymbol() as $symbol => $quote) {
74 1
            $output->writeln($symbol);
75
76 1
            foreach ($quote->toArray() as $key => $value) {
77 1
                $output->writeln(sprintf('# %s => %s', $key, json_encode($value)));
78
            }
79
80 1
            $output->writeln('');
81
        }
82 1
    }
83
}
84