diff --git a/config/services.yaml b/config/services.yaml index 154d6708..269d1a48 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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" diff --git a/src/Command/TestHubCounterCommand.php b/src/Command/TestHubCounterCommand.php new file mode 100644 index 00000000..e08c11dd --- /dev/null +++ b/src/Command/TestHubCounterCommand.php @@ -0,0 +1,39 @@ +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"; + } +} diff --git a/src/Service/HubCounter.php b/src/Service/HubCounter.php index 6fa52819..13388ecc 100644 --- a/src/Service/HubCounter.php +++ b/src/Service/HubCounter.php @@ -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); diff --git a/src/Service/RedisClient.php b/src/Service/RedisClient.php new file mode 100644 index 00000000..1c3c35b8 --- /dev/null +++ b/src/Service/RedisClient.php @@ -0,0 +1,29 @@ +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; + } +}