Skip to content

Commit

Permalink
Fix code review and add counter by month and year
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzerah committed Oct 3, 2018
1 parent d74d8b9 commit 1e75054
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -28,7 +28,7 @@ Then launch this command:

The application should now be running on http://127.0.0.1:8000.

Tests are made with PHPUnit
Tests are made with PHPUnit.
To run unit tests, launch this command:

`make test`
3 changes: 0 additions & 3 deletions config/services.yaml
Expand Up @@ -37,9 +37,6 @@ services:
- '@JoliCode\SecretSanta\Application\SlackApplication'
- '@JoliCode\SecretSanta\Application\DiscordApplication'

Jolicode\SecretSanta\Statistics:
public: true

JoliCode\SecretSanta\Controller\SlackController:
public: true
arguments:
Expand Down
10 changes: 1 addition & 9 deletions src/Application/DiscordApplication.php
Expand Up @@ -15,7 +15,6 @@
use JoliCode\SecretSanta\Discord\MessageSender;
use JoliCode\SecretSanta\Discord\UserExtractor;
use JoliCode\SecretSanta\SecretSanta;
use JoliCode\SecretSanta\Statistic;
use JoliCode\SecretSanta\User;
use League\OAuth2\Client\Token\AccessToken;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -33,18 +32,13 @@ class DiscordApplication implements ApplicationInterface
private $apiHelper;
private $userExtractor;
private $messageSender;
/**
* @var Statistic
*/
private $statistic;

public function __construct(RequestStack $requestStack, ApiHelper $apiHelper, UserExtractor $userExtractor, MessageSender $messageSender, Statistic $statistic)
public function __construct(RequestStack $requestStack, ApiHelper $apiHelper, UserExtractor $userExtractor, MessageSender $messageSender)
{
$this->requestStack = $requestStack;
$this->apiHelper = $apiHelper;
$this->userExtractor = $userExtractor;
$this->messageSender = $messageSender;
$this->statistic = $statistic;
}

public function getCode(): string
Expand Down Expand Up @@ -116,8 +110,6 @@ public function sendAdminMessage(SecretSanta $secretSanta, string $code, string

public function finish(SecretSanta $secretSanta)
{
$this->statistic->incrementUsageCount();

$this->getSession()->remove(self::SESSION_KEY_TOKEN);
$this->getSession()->remove(self::SESSION_KEY_ADMIN);
$this->getSession()->remove(self::SESSION_KEY_GUILD_ID);
Expand Down
7 changes: 1 addition & 6 deletions src/Application/SlackApplication.php
Expand Up @@ -14,7 +14,6 @@
use JoliCode\SecretSanta\SecretSanta;
use JoliCode\SecretSanta\Slack\MessageSender;
use JoliCode\SecretSanta\Slack\UserExtractor;
use JoliCode\SecretSanta\Statistic;
use JoliCode\SecretSanta\User;
use League\OAuth2\Client\Token\AccessToken;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -30,14 +29,12 @@ class SlackApplication implements ApplicationInterface
private $requestStack;
private $userExtractor;
private $messageSender;
private $statistic;

public function __construct(RequestStack $requestStack, UserExtractor $userExtractor, MessageSender $messageSender, Statistic $statistic)
public function __construct(RequestStack $requestStack, UserExtractor $userExtractor, MessageSender $messageSender)
{
$this->requestStack = $requestStack;
$this->userExtractor = $userExtractor;
$this->messageSender = $messageSender;
$this->statistic = $statistic;
}

public function getCode(): string
Expand Down Expand Up @@ -93,8 +90,6 @@ public function sendAdminMessage(SecretSanta $secretSanta, string $code, string

public function finish(SecretSanta $secretSanta)
{
$this->statistic->incrementUsageCount();

$this->getSession()->remove(self::SESSION_KEY_TOKEN);
$this->getSession()->remove(self::SESSION_KEY_ADMIN);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Controller/SantaController.php
Expand Up @@ -17,6 +17,7 @@
use JoliCode\SecretSanta\Rudolph;
use JoliCode\SecretSanta\SecretSanta;
use JoliCode\SecretSanta\Spoiler;
use JoliCode\SecretSanta\StatisticCollector;
use JoliCode\SecretSanta\User;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand All @@ -31,13 +32,15 @@ class SantaController extends AbstractController
private $twig;
private $logger;
private $applications;
private $statistic;

public function __construct(RouterInterface $router, \Twig_Environment $twig, LoggerInterface $logger, array $applications)
public function __construct(RouterInterface $router, \Twig_Environment $twig, LoggerInterface $logger, array $applications, StatisticCollector $statistic)
{
$this->router = $router;
$this->twig = $twig;
$this->logger = $logger;
$this->applications = $applications;
$this->statistic = $statistic;
}

public function run(MessageDispatcher $messageDispatcher, Rudolph $rudolph, Request $request, string $application): Response
Expand Down Expand Up @@ -86,6 +89,7 @@ public function run(MessageDispatcher $messageDispatcher, Rudolph $rudolph, Requ
}

if ($secretSanta->isDone()) {
$this->statistic->incrementUsageCount();
$application->finish($secretSanta);
}

Expand Down
12 changes: 7 additions & 5 deletions src/Statistic.php → src/StatisticCollector.php
Expand Up @@ -13,11 +13,8 @@

use Predis\Client;

class Statistic
class StatisticCollector
{
/**
* @var Client
*/
private $client;

public function __construct(Client $client)
Expand All @@ -27,7 +24,12 @@ public function __construct(Client $client)

public function incrementUsageCount()
{
$currentYear = date('Y');
$currentMonth = date('m');

//If the key does not exist, it is set to 0 before performing the operation
$this->client->incr('usageCount');
$this->client->hincrby('date:' . $currentYear . '-' . $currentMonth, 'usageCount', 1);
$this->client->hincrby('date:' . $currentYear, 'usageCount', 1);
$this->client->hincrby('date:total', 'usageCount', 1);
}
}

0 comments on commit 1e75054

Please sign in to comment.