Add command to create api users from existing riders. #617
This commit is contained in:
parent
c85ad3ab4f
commit
ea2f0e639d
1 changed files with 68 additions and 0 deletions
68
src/Command/CreateRiderAPIUserCommand.php
Normal file
68
src/Command/CreateRiderAPIUserCommand.php
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use Catalyst\APIBundle\Entity\User as APIUser;
|
||||
|
||||
use App\Entity\Rider;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class CreateRiderAPIUserCommand extends Command
|
||||
{
|
||||
protected $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('api:user-create-for-rider')
|
||||
->setDescription('Create API users for existing riders.')
|
||||
->setHelp('Creates API users for existing riders.');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
error_log('Creating api users...');
|
||||
// get all existing riders
|
||||
$riders = $this->em->getRepository(Rider::class)->findAll();
|
||||
|
||||
foreach ($riders as $rider)
|
||||
{
|
||||
// create api user for each rider
|
||||
// no need to generate the keys.
|
||||
// Secret and API keys are generated in constructor
|
||||
$api_user = new APIUser();
|
||||
|
||||
// set name to rider's last name + first name
|
||||
$rider_name = $rider->getLastName() . '_' . $rider->getFirstName();
|
||||
$api_user->setName($rider_name);
|
||||
|
||||
// set rider to api_user
|
||||
$api_user->setRider($rider);
|
||||
|
||||
// set meta
|
||||
$meta = ['rider_id' => $rider->getID()];
|
||||
$api_user->setMetaData($meta);
|
||||
|
||||
$this->em->persist($api_user);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue