-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemonFixAllCommand.php
More file actions
86 lines (76 loc) · 3.52 KB
/
PokemonFixAllCommand.php
File metadata and controls
86 lines (76 loc) · 3.52 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace App\Command;
use App\Entity\Pokemon;
use App\Entity\PokemonTypes;
use App\Service\IApiCall;
use App\Service\IUploaderClass;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'pokemon:fix:all',
description: 'Fixes all Pokemon via the PokeAPI',
)]
class PokemonFixAllCommand extends Command {
/**
* @param IApiCall $apiInterface
* @param EntityManagerInterface $em
*/
public function __construct(private readonly IApiCall $apiInterface,
private readonly EntityManagerInterface $em,
private readonly IUploaderClass $uploader) {
parent::__construct('pokemon:fix:all');
}
public function compareTypes(PokemonTypes $a, PokemonTypes $b): int {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
protected function configure(): void {
$this
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description');
}
protected function execute(InputInterface $input, OutputInterface $output): int { //Todo: Do all fix
$io = new SymfonyStyle($input, $output);
/** @var Pokemon $localPkmnRepository */
$localPkmnRepository = $this->em->getRepository(Pokemon::class)->findAll();
$localTypeRepository = $this->em->getRepository(PokemonTypes::class);
/** @var Pokemon $localPokemon */
foreach ($localPkmnRepository as $key => $localPokemon) {
$remotePokemon = $this->apiInterface->getPokemon($localPokemon->getPokedexId());
$remoteType = $remotePokemon->getPokemonType();
$localType = $localPokemon->getPokemonType();
$typeDiff = array_udiff($remoteType->toArray(), $localType->toArray(), array($this, 'compareTypes'));
if ($localPokemon->getChangedByAdmin()) {
$io->warning('Pokemon ' . $localPokemon->getName() . ' was changed by an Admin. Skipping...');
continue;
}
if ($typeDiff) {
$io->info($localPokemon->getName() . ' hat keine oder falsche Typen!');
sleep(2);
foreach ($remoteType as $value => $item) {
$remoteTypeName = $item->getTypeName();
$localTypeName = $localTypeRepository->findOneBy(array('typeName' => $remoteTypeName));
$localPokemon->addPokemonType($localTypeName);
$io->info('Für ' . $localPokemon->getName() . ' wurde der Typ: ' . $remoteTypeName . ' gefunden!');
}
sleep(1);
$localPokemon->setHeight($remotePokemon->getHeight());
$localPokemon->setPokedexId($remotePokemon->getPokedexId());
$localPokemon->setImage($remotePokemon->getImage());
$this->em->flush();
$io->success('Das Pokemon ' . $localPokemon->getName() . ' wurde korrigiert!');
sleep(3);
}
}
$io->success('Alle Vorgänge erfolgreich abgeschlossen!');
return Command::SUCCESS;
}
}