Skip to content

Instantly share code, notes, and snippets.

@angelsk
Created July 31, 2018 11:33
Show Gist options
  • Save angelsk/cae1f060a79b91d75891b6f40ddb1fe7 to your computer and use it in GitHub Desktop.
Save angelsk/cae1f060a79b91d75891b6f40ddb1fe7 to your computer and use it in GitHub Desktop.
PackratRecipes - Generate SQL for inserting multi-collection collections
<?php
namespace App\Command;
use App\Service\PackratApi;
use Doctrine\ORM\EntityManagerInterface;
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;
class PackratRecipesCommand extends Command
{
protected static $defaultName = 'packrat:recipes';
/**
* @var PackratApi
*/
private $packratApi;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @param PackratApi $packratApi
*/
public function __construct(PackratApi $packratApi, EntityManagerInterface $entityManager)
{
$this->packratApi = $packratApi;
$this->entityManager = $entityManager;
parent::__construct();
}
protected function configure()
{
$this->setDescription('Process recipes from the Packrat API');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$collectionApiId = 644; // Hardcoded for now
$collection = $this->packratApi->getCollection($collectionApiId);
$dbCollection = $this->getCollectionFromDatabase($collectionApiId);
$collectionIds = [];
$io->success('Found collection: ' . $collection['name']);
foreach ($collection['cards'] as $packratCardId) {
$card = $this->packratApi->getCard($packratCardId);
$dbCard = $this->getCardFromDatabase($packratCardId);
$recipe = [];
$relatedCollectionIds = [$dbCollection['collection_id']];
foreach ($card['recipe'] as $packratRecipeId) {
$recipeCard = $this->packratApi->getCard($packratRecipeId);
$dbRecipeCard = $this->getCardFromDatabase($packratRecipeId);
$recipeCollection = $this->getCollectionFromDatabase($recipeCard['collection_id']);
$recipe[] = [
'api' => $recipeCard,
'db' => $dbRecipeCard,
'collection' => $recipeCollection
];
$relatedCollectionIds[] = $recipeCollection['collection_id'];
}
$io->block(
sprintf(
'INSERT INTO card_recipe (card_id, ingredient_1, ingredient_2, ingredient_3) VALUES (%d, %d, %d, %d);',
$dbCard['card_id'],
$recipe[0]['db']['card_id'],
$recipe[1]['db']['card_id'],
$recipe[2]['db']['card_id']
)
);
$tweet = sprintf(
"New recipe: %s (%s, %d points) = %s (%s) + %s (%s) + %s (%s) #packrat",
$dbCard['card_name'],
$dbCollection['collection_name'],
$dbCard['point_value'],
$recipe[0]['db']['card_name'],
$recipe[0]['collection']['collection_name'],
$recipe[1]['db']['card_name'],
$recipe[1]['collection']['collection_name'],
$recipe[2]['db']['card_name'],
$recipe[2]['collection']['collection_name']
);
if (strlen($tweet) > 280) {
$io->error('Tweet too long');
}
$io->block(
sprintf(
'INSERT INTO queue (type, tweet) VALUES ("new_recipe", "%s");',
$tweet
)
);
$collectionIds = array_merge($collectionIds, $relatedCollectionIds);
foreach ($recipe as $ing) {
$collection = $ing['collection'];
$related = [];
if ($collection['related_collection_id'] !== '0') {
$related = explode(',', $collection['related_collection_id']);
}
$related = array_unique(array_merge($related, $relatedCollectionIds));
$related = array_diff($related, [$collection['collection_id']]);
$io->block(
sprintf(
'UPDATE collection SET related_collection_id = "%s" WHERE collection_id = %d;',
implode(',', $related),
$collection['collection_id']
)
);
}
}
$io->block(
sprintf(
'UPDATE collection SET related_collection_id = "%s" WHERE collection_id = %d;',
implode(',', array_unique($collectionIds)),
$dbCollection['collection_id']
)
);
}
private function getCollectionFromDatabase(int $packratId): array
{
$conn = $this->entityManager->getConnection();
$sql = '
SELECT * FROM collection c
WHERE c.packrat_id = :packrat_id
';
$stmt = $conn->prepare($sql);
$stmt->execute(['packrat_id' => $packratId]);
return $stmt->fetchAll()[0];
}
private function getCardFromDatabase(int $packratId): array
{
$conn = $this->entityManager->getConnection();
$sql = '
SELECT * FROM card c
WHERE c.packrat_id = :packrat_id
';
$stmt = $conn->prepare($sql);
$stmt->execute(['packrat_id' => $packratId]);
return $stmt->fetchAll()[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment