Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
91.30% covered (success)
91.30%
21 / 23
ForecastCommand
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
6.02
91.30% covered (success)
91.30%
21 / 23
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 configure
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 execute
0.00% covered (danger)
0.00%
0 / 1
4.05
85.71% covered (warning)
85.71%
12 / 14
1<?php
2
3namespace App\Presentation\Cli;
4
5use App\Application\CityResponse;
6use App\Application\GetCityQuery;
7use App\Application\GetForecastQuery;
8use App\Application\QueryBus;
9use Symfony\Component\Console\Command\Command;
10use Symfony\Component\Console\Input\InputArgument;
11use Symfony\Component\Console\Input\InputInterface;
12use Symfony\Component\Console\Output\OutputInterface;
13
14class ForecastCommand extends Command
15{
16    private const DAYS_ARGUMENT = 'days';
17
18    protected static $defaultName = 'app:forecast';
19    protected static $defaultDescription = 'Display list of cities with forecast';
20
21    public function __construct(
22        private QueryBus $queryBus,
23    ) {
24        parent::__construct();
25    }
26
27    protected function configure(): void
28    {
29        $this
30            ->addArgument(
31                self::DAYS_ARGUMENT,
32                InputArgument::OPTIONAL,
33                'Numbers of days to forecast',
34                '2'
35            );
36    }
37
38    protected function execute(InputInterface $input, OutputInterface $output): int
39    {
40        $days = $input->getArgument(self::DAYS_ARGUMENT);
41        if (! is_numeric($days)) {
42            $output->writeln('Only numeric values');
43
44            return Command::FAILURE;
45        }
46
47        if ($days > 3) {
48            $output->writeln('Maximum 3 days');
49
50            return Command::FAILURE;
51        }
52
53        /** @var CityResponse $cities */
54        $cities = $this->queryBus->handle(new GetCityQuery());
55
56        foreach ($cities->getCities() as $city) {
57            $forecasts = $this->queryBus->handle(
58                new GetForecastQuery((int) $days, $city->getLatitude(), $city->getLongitude())
59            );
60
61            $output->writeln(
62                sprintf('Processed city %s | %s', $city->getName(), $forecasts)
63            );
64        }
65
66        return Command::SUCCESS;
67    }
68}