Add service to connect to redis. Add command to test service with the HubCounter service. #206

This commit is contained in:
Korina Cordero 2019-05-02 07:21:49 +00:00
parent daf0d03065
commit afc07e7c68
4 changed files with 77 additions and 6 deletions

View file

@ -75,6 +75,12 @@ services:
$ip_address: "%env(APNS_REDIS_IP_ADDRESS)%"
$port: "%env(APNS_REDIS_PORT)%"
App\Service\RedisClient:
arguments:
$scheme: "%env(REDIS_CLIENT_SCHEME)%"
$host: "%env(REDIS_CLIENT_HOST)%"
$port: "%env(REDIS_CLIENT_PORT)%"
Catalyst\APIBundle\Security\APIKeyUserProvider:
arguments:
$em: "@doctrine.orm.entity_manager"

View file

@ -0,0 +1,39 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use App\Service\HubCounter;
class TestHubCounterCommand extends Command
{
protected function configure()
{
$this->setName('test:hubcounter')
->setDescription('Test hubcounter service. Currently tests addAvailableRider and getAvailableRiderCount.')
->setHelp('Test hubcounter service. Currently tests addAvailableRider and getAvailableRiderCount.')
->addArgument('hub_id', InputArgument::REQUIRED, 'Hub ID');
}
public function __construct(HubCounter $hc)
{
$this->hc = $hc;
parent::__construct();
}
public function execute(InputInterface $input, OutputInterface $output)
{
$hub_id = $input->getArgument('hub_id');
$this->hc->addAvailableRider($hub_id);
$available_rc = $this->hc->getAvailableRiderCount($hub_id);
echo "Available Riders in Hub: " . $available_rc . "\n";
}
}

View file

@ -15,13 +15,10 @@ class HubCounter
protected $em;
protected $redis;
public function __construct(EntityManagerInterface $em)
public function __construct(EntityManagerInterface $em, RedisClient $rc)
{
$this->em = $em;
// TODO: make it read redis settings from config
// build a service maybe?
$this->redis = new Predis\Client();
$this->redis = $rc->getRedisClient();
}
// get rider key based on id
@ -45,7 +42,7 @@ class HubCounter
return $value;
// not in cache
$hub = $em->getRepository(Hub::class)->find($hub_id);
$hub = $this->em->getRepository(Hub::class)->find($hub_id);
$riders = $hub->getActiveRiders();
$rider_count = count($riders);

View file

@ -0,0 +1,29 @@
<?php
namespace App\Service;
use Predis\Client as PredisClient;
class RedisClient
{
protected $redis;
protected $scheme;
protected $host;
protected $port;
public function __construct($scheme, $host, $port)
{
$this->scheme = $scheme;
$this->host = $host;
$this->port = $port;
}
public function getRedisClient()
{
$this->redis = new PredisClient([
"scheme"=>$this->scheme,
"host"=>$this->host,
"port"=>$this->port]);
return $this->redis;
}
}