Add service to connect to redis. Add command to test service with the HubCounter service. #206
This commit is contained in:
parent
daf0d03065
commit
afc07e7c68
4 changed files with 77 additions and 6 deletions
|
|
@ -75,6 +75,12 @@ services:
|
||||||
$ip_address: "%env(APNS_REDIS_IP_ADDRESS)%"
|
$ip_address: "%env(APNS_REDIS_IP_ADDRESS)%"
|
||||||
$port: "%env(APNS_REDIS_PORT)%"
|
$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:
|
Catalyst\APIBundle\Security\APIKeyUserProvider:
|
||||||
arguments:
|
arguments:
|
||||||
$em: "@doctrine.orm.entity_manager"
|
$em: "@doctrine.orm.entity_manager"
|
||||||
|
|
|
||||||
39
src/Command/TestHubCounterCommand.php
Normal file
39
src/Command/TestHubCounterCommand.php
Normal 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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -15,13 +15,10 @@ class HubCounter
|
||||||
protected $em;
|
protected $em;
|
||||||
protected $redis;
|
protected $redis;
|
||||||
|
|
||||||
public function __construct(EntityManagerInterface $em)
|
public function __construct(EntityManagerInterface $em, RedisClient $rc)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
|
$this->redis = $rc->getRedisClient();
|
||||||
// TODO: make it read redis settings from config
|
|
||||||
// build a service maybe?
|
|
||||||
$this->redis = new Predis\Client();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get rider key based on id
|
// get rider key based on id
|
||||||
|
|
@ -45,7 +42,7 @@ class HubCounter
|
||||||
return $value;
|
return $value;
|
||||||
|
|
||||||
// not in cache
|
// not in cache
|
||||||
$hub = $em->getRepository(Hub::class)->find($hub_id);
|
$hub = $this->em->getRepository(Hub::class)->find($hub_id);
|
||||||
$riders = $hub->getActiveRiders();
|
$riders = $hub->getActiveRiders();
|
||||||
$rider_count = count($riders);
|
$rider_count = count($riders);
|
||||||
|
|
||||||
|
|
|
||||||
29
src/Service/RedisClient.php
Normal file
29
src/Service/RedisClient.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue