Lines 100.00% 23 / 23
Methods 100.00% 3 / 3
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 8 / 8 100.00% 1 / 1 1
 execute 100.00% 8 / 8 100.00% 1 / 1 3
 scrapeCity 100.00% 7 / 7 100.00% 1 / 1 1
19final class ImportCommand extends Command
20{
21    public function __construct(
22        private readonly Importer $importer,
23        private readonly ScraperCollection $scraperCollection,
24    ) {
25        parent::__construct();
26        $this->setName("import");
27        $this->setDescription("Scrape and save districts data. Overwrites existing records.");
28        $this->addArgument(
29            "city_names",
30            InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
31            "List of city names to import (default: all cities)"
32        );
33    }
34
35    protected function execute(InputInterface $input, OutputInterface $output): int
36    {
37        /** @var string[] $cityNames */
38        $cityNames = $input->getArgument("city_names");
39        try {
40            foreach ($this->scraperCollection->filterByCityNames($cityNames) as $scraper) {
41                $output->writeln("processing city: " . $scraper->getCityName());
42                $cityDTO = self::scrapeCity($scraper, $output);
43                $this->importer->import($cityDTO);
44            }
45        } catch (InvalidArgumentException $ex) {
46            throw new SymfonyConsoleInvalidArgumentException($ex->getMessage(), $ex->getCode());
47        }
48        return 0;
49    }
50
51    private static function scrapeCity(CityScraper $scraper, OutputInterface $output): CityDTO
52    {
53        $progressBar = new ProgressBar($output);
54        $progressReporter = new ProgressBarProgressReporter($progressBar);
55        $progressBar->start();
56        $cityDTO = $scraper->scrape($progressReporter);
57        $progressBar->finish();
58        $output->writeln("");
59        return $cityDTO;
60    }
61}