I rightly do not know what this all is
This commit is contained in:
parent
af6b2b752e
commit
287c1f67c5
78 changed files with 3484 additions and 3365 deletions
|
@ -1,166 +1,166 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GamesShop\Importer;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exception;
|
||||
use GamesShop\Entities\Account\User;
|
||||
use GamesShop\Entities\Games\Game;
|
||||
use GamesShop\Entities\Games\Key;
|
||||
use GamesShop\Entities\Games\KeyAttribute;
|
||||
use GamesShop\Entities\Games\Store;
|
||||
use GamesShop\Entities\GamesList;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
|
||||
final class GameImporter
|
||||
{
|
||||
private const HEADER_ROW_INDEX = 1;
|
||||
|
||||
private const STORE_ADDITIONAL_CASES = [
|
||||
'epic' => Store::EPICGAMES,
|
||||
'ea' => Store::ORIGIN,
|
||||
'eaplay' => Store::ORIGIN,
|
||||
'ubisoft' => Store::UPLAY,
|
||||
'activision' => Store::BATTLENET
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
) { }
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return ImportColumnInterpretation[]
|
||||
*/
|
||||
public function interpret(string $path): array {
|
||||
$spreadsheet = IOFactory::load($path);
|
||||
|
||||
$worksheet = $spreadsheet->getSheet(0);
|
||||
$result = [];
|
||||
foreach ($worksheet->getColumnIterator() as $column) {
|
||||
$columnIndex = $column->getColumnIndex();
|
||||
|
||||
$value = $worksheet->getCell(sprintf('%s%d', $columnIndex, self::HEADER_ROW_INDEX))->getValueString();
|
||||
|
||||
if (empty(trim($value))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$guessedAttribute = $this->guessAttribute($value);
|
||||
$result[] = new ImportColumnInterpretation(
|
||||
$columnIndex,
|
||||
$value,
|
||||
$guessedAttribute
|
||||
);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function guessAttribute(string $value): KeyAttribute|null {
|
||||
$value = trim($value);
|
||||
$value = strtolower($value);
|
||||
$value = str_replace(' ', '_', $value);
|
||||
|
||||
$attribute = match($value) {
|
||||
'key' => KeyAttribute::KEY,
|
||||
'name', 'game_name', 'game' => KeyAttribute::GAME_NAME,
|
||||
'from' => KeyAttribute::FROM,
|
||||
'store', 'for' => KeyAttribute::STORE,
|
||||
default => null
|
||||
};
|
||||
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $columnDefinitions
|
||||
*/
|
||||
public function import(string $path, array $columnDefinitions, GamesList $list): array {
|
||||
$spreadsheet = IOFactory::load($path);
|
||||
|
||||
$worksheet = $spreadsheet->getSheet(0);
|
||||
|
||||
$totalRows = 0;
|
||||
$addedAmount = 0;
|
||||
|
||||
foreach ($worksheet->getRowIterator(self::HEADER_ROW_INDEX + 1) as $row) {
|
||||
$totalRows++;
|
||||
$values = [
|
||||
'name' => null,
|
||||
'key' => null,
|
||||
'from' => null,
|
||||
'store' => null,
|
||||
'store_link' => null
|
||||
];
|
||||
|
||||
foreach ($columnDefinitions as $columnIndex => $attribute) {
|
||||
$value = $worksheet->getCell(sprintf('%s%d', $columnIndex, $row->getRowIndex()))->getValueString();
|
||||
|
||||
switch(KeyAttribute::from($attribute)) {
|
||||
case KeyAttribute::NONE:
|
||||
break;
|
||||
case KeyAttribute::GAME_NAME:
|
||||
$values['name'] = $value;
|
||||
break;
|
||||
case KeyAttribute::KEY:
|
||||
$values['key'] = $value;
|
||||
break;
|
||||
case KeyAttribute::FROM:
|
||||
$values['from'] = $value;
|
||||
break;
|
||||
case KeyAttribute::STORE:
|
||||
$store = $this->interpretStore($value);
|
||||
$values['store'] = $store;
|
||||
if ($store === Store::EXTERNAL) {
|
||||
$values['store_link'] = $value;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($values['key'] === null || $values['name'] === null || $values['store'] === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$game = $this->entityManager->getRepository(Game::class)->findOneBy([ 'name' => $values['name'] ]);
|
||||
if ($game === null) {
|
||||
$game = new Game($values['name']);
|
||||
}
|
||||
|
||||
$key = new Key(
|
||||
$game,
|
||||
$list,
|
||||
$values['key'],
|
||||
$values['store'],
|
||||
$values['store_link'],
|
||||
$values['from'],
|
||||
);
|
||||
|
||||
$this->entityManager->persist($game);
|
||||
$this->entityManager->persist($key);
|
||||
$addedAmount++;
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return [$totalRows, $addedAmount];
|
||||
}
|
||||
|
||||
private function interpretStore(string $storeString): Store {
|
||||
$storeString = trim($storeString);
|
||||
$storeString = strtolower($storeString);
|
||||
$storeString = str_replace(' ', '', $storeString);
|
||||
|
||||
$triedConversion = Store::tryFrom($storeString);
|
||||
if ($triedConversion !== null) {
|
||||
return $triedConversion;
|
||||
}
|
||||
|
||||
if (array_key_exists($storeString, self::STORE_ADDITIONAL_CASES)) {
|
||||
return self::STORE_ADDITIONAL_CASES[$storeString];
|
||||
}
|
||||
|
||||
return Store::EXTERNAL;
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GamesShop\Importer;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exception;
|
||||
use GamesShop\Entities\Account\User;
|
||||
use GamesShop\Entities\Games\Game;
|
||||
use GamesShop\Entities\Games\Key;
|
||||
use GamesShop\Entities\Games\KeyAttribute;
|
||||
use GamesShop\Entities\Games\Store;
|
||||
use GamesShop\Entities\GamesList;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
|
||||
final class GameImporter
|
||||
{
|
||||
private const HEADER_ROW_INDEX = 1;
|
||||
|
||||
private const STORE_ADDITIONAL_CASES = [
|
||||
'epic' => Store::EPICGAMES,
|
||||
'ea' => Store::ORIGIN,
|
||||
'eaplay' => Store::ORIGIN,
|
||||
'ubisoft' => Store::UPLAY,
|
||||
'activision' => Store::BATTLENET
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
) { }
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return ImportColumnInterpretation[]
|
||||
*/
|
||||
public function interpret(string $path): array {
|
||||
$spreadsheet = IOFactory::load($path);
|
||||
|
||||
$worksheet = $spreadsheet->getSheet(0);
|
||||
$result = [];
|
||||
foreach ($worksheet->getColumnIterator() as $column) {
|
||||
$columnIndex = $column->getColumnIndex();
|
||||
|
||||
$value = $worksheet->getCell(sprintf('%s%d', $columnIndex, self::HEADER_ROW_INDEX))->getValueString();
|
||||
|
||||
if (empty(trim($value))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$guessedAttribute = $this->guessAttribute($value);
|
||||
$result[] = new ImportColumnInterpretation(
|
||||
$columnIndex,
|
||||
$value,
|
||||
$guessedAttribute
|
||||
);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function guessAttribute(string $value): KeyAttribute|null {
|
||||
$value = trim($value);
|
||||
$value = strtolower($value);
|
||||
$value = str_replace(' ', '_', $value);
|
||||
|
||||
$attribute = match($value) {
|
||||
'key' => KeyAttribute::KEY,
|
||||
'name', 'game_name', 'game' => KeyAttribute::GAME_NAME,
|
||||
'from' => KeyAttribute::FROM,
|
||||
'store', 'for' => KeyAttribute::STORE,
|
||||
default => null
|
||||
};
|
||||
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $columnDefinitions
|
||||
*/
|
||||
public function import(string $path, array $columnDefinitions, GamesList $list): array {
|
||||
$spreadsheet = IOFactory::load($path);
|
||||
|
||||
$worksheet = $spreadsheet->getSheet(0);
|
||||
|
||||
$totalRows = 0;
|
||||
$addedAmount = 0;
|
||||
|
||||
foreach ($worksheet->getRowIterator(self::HEADER_ROW_INDEX + 1) as $row) {
|
||||
$totalRows++;
|
||||
$values = [
|
||||
'name' => null,
|
||||
'key' => null,
|
||||
'from' => null,
|
||||
'store' => null,
|
||||
'store_link' => null
|
||||
];
|
||||
|
||||
foreach ($columnDefinitions as $columnIndex => $attribute) {
|
||||
$value = $worksheet->getCell(sprintf('%s%d', $columnIndex, $row->getRowIndex()))->getValueString();
|
||||
|
||||
switch(KeyAttribute::from($attribute)) {
|
||||
case KeyAttribute::NONE:
|
||||
break;
|
||||
case KeyAttribute::GAME_NAME:
|
||||
$values['name'] = $value;
|
||||
break;
|
||||
case KeyAttribute::KEY:
|
||||
$values['key'] = $value;
|
||||
break;
|
||||
case KeyAttribute::FROM:
|
||||
$values['from'] = $value;
|
||||
break;
|
||||
case KeyAttribute::STORE:
|
||||
$store = $this->interpretStore($value);
|
||||
$values['store'] = $store;
|
||||
if ($store === Store::EXTERNAL) {
|
||||
$values['store_link'] = $value;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($values['key'] === null || $values['name'] === null || $values['store'] === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$game = $this->entityManager->getRepository(Game::class)->findOneBy([ 'name' => $values['name'] ]);
|
||||
if ($game === null) {
|
||||
$game = new Game($values['name']);
|
||||
}
|
||||
|
||||
$key = new Key(
|
||||
$game,
|
||||
$list,
|
||||
$values['key'],
|
||||
$values['store'],
|
||||
$values['store_link'],
|
||||
$values['from'],
|
||||
);
|
||||
|
||||
$this->entityManager->persist($game);
|
||||
$this->entityManager->persist($key);
|
||||
$addedAmount++;
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return [$totalRows, $addedAmount];
|
||||
}
|
||||
|
||||
private function interpretStore(string $storeString): Store {
|
||||
$storeString = trim($storeString);
|
||||
$storeString = strtolower($storeString);
|
||||
$storeString = str_replace(' ', '', $storeString);
|
||||
|
||||
$triedConversion = Store::tryFrom($storeString);
|
||||
if ($triedConversion !== null) {
|
||||
return $triedConversion;
|
||||
}
|
||||
|
||||
if (array_key_exists($storeString, self::STORE_ADDITIONAL_CASES)) {
|
||||
return self::STORE_ADDITIONAL_CASES[$storeString];
|
||||
}
|
||||
|
||||
return Store::EXTERNAL;
|
||||
}
|
||||
}
|
|
@ -1,26 +1,26 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GamesShop\Importer;
|
||||
|
||||
use GamesShop\Entities\Games\KeyAttribute;
|
||||
use JsonSerializable;
|
||||
|
||||
final readonly class ImportColumnInterpretation implements JsonSerializable
|
||||
{
|
||||
public function __construct(
|
||||
public string $index,
|
||||
public string $displayName,
|
||||
public KeyAttribute|null $guessedAttribute
|
||||
)
|
||||
{ }
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'index' => $this->index,
|
||||
'displayName' => $this->displayName,
|
||||
'guessedAttribute' => $this->guessedAttribute ?? null
|
||||
];
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GamesShop\Importer;
|
||||
|
||||
use GamesShop\Entities\Games\KeyAttribute;
|
||||
use JsonSerializable;
|
||||
|
||||
final readonly class ImportColumnInterpretation implements JsonSerializable
|
||||
{
|
||||
public function __construct(
|
||||
public string $index,
|
||||
public string $displayName,
|
||||
public KeyAttribute|null $guessedAttribute
|
||||
)
|
||||
{ }
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'index' => $this->index,
|
||||
'displayName' => $this->displayName,
|
||||
'guessedAttribute' => $this->guessedAttribute ?? null
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue