BUG: Improves Game Imports usage

This commit is contained in:
Michel 2024-11-12 19:38:47 +01:00
parent 56fee01322
commit 4d1481fcfb
2 changed files with 47 additions and 7 deletions

View file

@ -15,6 +15,8 @@ class Game
private int|null $id; private int|null $id;
#[ORM\Column] #[ORM\Column]
private string $name; private string $name;
#[ORM\Column(nullable: true)]
private string|null $internalName;
public function getName(): string public function getName(): string
{ {
@ -25,12 +27,23 @@ class Game
{ {
return $this->id; return $this->id;
} }
public function getInternalName(): string
{
return $this->internalName ?? $this->name;
}
public function setInternalName(string $internalName): void
{
$this->internalName = $internalName;
}
/** /**
* @param string $name * @param string $name
*/ */
public function __construct(string $name) public function __construct(string $internalName, string $name)
{ {
$this->internalName = $internalName;
$this->name = $name; $this->name = $name;
} }
} }

View file

@ -3,14 +3,17 @@ declare(strict_types=1);
namespace GamesShop\Importer; namespace GamesShop\Importer;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Exception; use Exception;
use GamesShop\ContainerHandler;
use GamesShop\Entities\Account\User; use GamesShop\Entities\Account\User;
use GamesShop\Entities\Games\Game; use GamesShop\Entities\Games\Game;
use GamesShop\Entities\Games\Key; use GamesShop\Entities\Games\Key;
use GamesShop\Entities\Games\KeyAttribute; use GamesShop\Entities\Games\KeyAttribute;
use GamesShop\Entities\Games\Store; use GamesShop\Entities\Games\Store;
use GamesShop\Entities\GamesList; use GamesShop\Entities\GamesList;
use Monolog\Logger;
use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\IOFactory;
final class GameImporter final class GameImporter
@ -127,19 +130,27 @@ final class GameImporter
continue; continue;
} }
$game = array_key_exists($values['name'], $foundGames) ? $foundGames[$values['name']] : null; $internalName = $this->prepareGameName($values['name']);
$game = array_key_exists($internalName, $foundGames) ? $foundGames[$internalName] : null;
ContainerHandler::get(Logger::class)->debug('importing game ' . $internalName);
if ($game === null) { if ($game === null) {
$game = $this->entityManager->getRepository(Game::class)->findOneBy([ 'name' => $values['name'] ]); $game = $this->entityManager->getRepository(Game::class)->matching(
Criteria::create()
->where(Criteria::expr()->eq('name', $values['name']))
->orWhere(Criteria::expr()->eq('internalName', $internalName))
)->first();
} }
if ($game === null) { if ($game === false) {
$game = new Game($values['name']); $game = new Game($internalName, $values['name']);
$this->entityManager->persist($game); $this->entityManager->persist($game);
} }
$foundGames[$values['name']] = $game; $foundGames[$internalName] = $game;
$game->setInternalName($internalName);
$key = new Key( $key = new Key(
$game, $game,
$list, $list,
@ -174,4 +185,20 @@ final class GameImporter
return Store::EXTERNAL; return Store::EXTERNAL;
} }
private function prepareGameName(string $name): string
{
$replacements = [
':' => '',
'-' => '',
',' => '',
'!' => '',
'?' => '',
' ' => ' ',
' ' => '_',
];
$name = strtoupper($name);
return str_replace(array_keys($replacements), array_values($replacements), $name);
}
} }