-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemonImportTypesCommand.php
More file actions
61 lines (56 loc) · 1.99 KB
/
PokemonImportTypesCommand.php
File metadata and controls
61 lines (56 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace App\Command;
use App\Entity\PokemonTypes;
use App\Repository\PokemonTypesRepository;
use App\Service\IApiCall;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpClient\Exception\ClientException;
#[AsCommand(
name: 'pokemon:import-types',
description: 'Imports all Pokemon-Types to local Database with PokeAPI',
)]
class PokemonImportTypesCommand extends Command {
/**
* @param IApiCall $apiInterface
* @param EntityManagerInterface $em
*/
public function __construct(private readonly IApiCall $apiInterface, private readonly EntityManagerInterface $em) {
parent::__construct('pokemon:import-types');
}
/**
* @return void
*/
protected function configure(): void {
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);
/** @var PokemonTypesRepository $typesRepository */
$typesRepository = $this->em->getRepository(PokemonTypes::class);
for ($i = 0; $i < 1000; $i++) {
try {
$type = $this->apiInterface->getPokemonType($i + 1);
$typesRepository->add($type, true);
} catch (ClientException) {
break;
} catch (Exception $exception) {
$io->error($exception->getMessage());
return Command::FAILURE;
}
$io->success('Der Typ ' . $type->getTypeName() . ' wurde erfolgreich importiert');
sleep(2);
}
$io->success('All Types have been imported');
return Command::SUCCESS;
}
}