Skip to content

Commit

Permalink
Merge pull request #90 from hpatoio/master
Browse files Browse the repository at this point in the history
Fixing issue #87
  • Loading branch information
liuggio committed Nov 21, 2013
2 parents f33eb84 + 7ed6afa commit c47bb43
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/config/config_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ imports:
services:
stats_persister:
class: PUGX\StatsBundle\Service\NullPersister
stats_reader:
class: PUGX\StatsBundle\Service\NullReader

framework:
router: { resource: "%kernel.root_dir%/config/routing_dev.yml" }
Expand Down
8 changes: 7 additions & 1 deletion src/PUGX/BadgeBundle/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class PageController extends ContainerAware
*/
public function homeAction($repository = 'doctrine/orm')
{
return array('repository' => $repository);

$redisReader = $this->container->get('stats_reader');

return array(
'repository' => $repository,
'total_access' => $redisReader->totalAccess()
);
}
}
3 changes: 3 additions & 0 deletions src/PUGX/BadgeBundle/Resources/views/Page/home.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

<div id="container" class="container-narrow">

<div class="row-fluid bundles">
<div class="span12"><p>{{ total_access }} badges served so far.</p><hr class="styled"></div>
</div>
<div class="jumbotron">
<h1>Badge Poser</h1>
<p class="lead">Pimp your README!</p>
Expand Down
5 changes: 5 additions & 0 deletions src/PUGX/StatsBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
<parameters>
<parameter key="stats_listener.class">PUGX\StatsBundle\Listener\StatsListener</parameter>
<parameter key="stats_persister.class">PUGX\StatsBundle\Service\RedisPersister</parameter>
<parameter key="stats_reader.class">PUGX\StatsBundle\Service\RedisReader</parameter>
</parameters>

<services>
<service id="stats_persister" class="%stats_persister.class%">
<argument type="service" id="snc_redis.default"/>
</service>

<service id="stats_reader" class="%stats_reader.class%">
<argument type="service" id="snc_redis.default"/>
</service>

<service id="stats_listener" class="%stats_listener.class%">
<argument type="service" id="stats_persister"/>
Expand Down
33 changes: 33 additions & 0 deletions src/PUGX/StatsBundle/Service/NullReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the badge-poser package.
*
* (c) PUGX <http://pugx.github.io/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\StatsBundle\Service;

/**
* Class NullReader
*
* @author Simone Fumagalli <[email protected]>
*/
class NullReader implements ReaderInterface
{
public static $totalAccess = false;

/**
* Return the total accesses.
*
* @return Integer
*/
public function totalAccess()
{
return static::$totalAccess;
}

}
28 changes: 28 additions & 0 deletions src/PUGX/StatsBundle/Service/ReaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the badge-poser package.
*
* (c) PUGX <http://pugx.github.io/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\StatsBundle\Service;

/**
* Class ReaderInterface
*
* @author Simone Fumagalli <[email protected]>
*/
Interface ReaderInterface
{
/**
* Read total accesses.
*
* @return integer
*/
public function totalAccess();

}
58 changes: 58 additions & 0 deletions src/PUGX/StatsBundle/Service/RedisReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the badge-poser package.
*
* (c) PUGX <http://pugx.github.io/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\StatsBundle\Service;

/**
* Class RedisStats
*
* @author Simone Fumagalli <[email protected]>
*/
class RedisReader implements ReaderInterface
{
const KEY_PREFIX = 'STAT';
const KEY_TOTAL = 'TOTAL';

private $redis;
private $keyTotal;
private $keyPrefix;

public function __construct($redis, $keyTotal = self::KEY_TOTAL, $keyPrefix = self::KEY_PREFIX)
{
$this->redis = $redis;
$this->keyPrefix = $keyPrefix;
$this->keyTotal = $this->concatenateKeys($keyPrefix, $keyTotal);
}

/**
* Generate the Key with the default prefix.
*
* @param string $prefix
* @param string $keyName
*
* @return string
*/
private function concatenateKeys($prefix, $keyName)
{
return sprintf("%s.%s", $prefix, $keyName);
}

/**
* Read total accesses.
*
* @return integer
*/
public function totalAccess()
{
return $this->redis->get($this->keyTotal);
}

}

0 comments on commit c47bb43

Please sign in to comment.