Skip to content
This repository was archived by the owner on Jul 28, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Command/CleanCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
try
{
$cleanCount = $geolocationApi->cleanCache();
if(is_array($cleanCount))
$cleanCount = $cleanCount['n'];
$output->writeln($cleanCount . " cache entries were removed");
}
catch (\Exception $e)
{
$output->writeln("Cache not available. Cannot clean!");
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));

return;
}
}
}
20 changes: 17 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,23 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('google_geolocation');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$supportedDrivers = array('orm', 'mongodb');

$rootNode
->children()
->scalarNode('db_driver')
->validate()
->ifNotInArray($supportedDrivers)
->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers))
->end()
->cannotBeOverwritten()
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('daily_limit')->defaultValue(2500)->end()
->scalarNode('cache_lifetime')->defaultValue(24)->end()
->end()
;

return $treeBuilder;
}
Expand Down
5 changes: 4 additions & 1 deletion DependencyInjection/GoogleGeolocationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function load(array $configs, ContainerBuilder $container)
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader->load($config['db_driver'].'.yml');

$container->setParameter('google_geolocation.geolocation_api.daily_limit', $config['daily_limit']);
$container->setParameter('google_geolocation.geolocation_api.cache_lifetime', $config['cache_lifetime']);
}
}
144 changes: 144 additions & 0 deletions Document/ApiLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace Google\GoogleGeolocationBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Gedmo\Mapping\Annotation as Gedmo;

/**
* @MongoDB\Document(
* collection="google_geolocation_api_log",
* repositoryClass="Webinfopro\Bundle\GoogleGeolocationBundle\Document\ApiLogRepository")
*/
class ApiLog
{
/**
* @MongoDB\Id
*/
protected $id;

/**
* @MongoDB\String
*/
protected $lastStatus;

/**
* @MongoDB\Int
*/
protected $requests;

/**
* @var datetime $created
*
* @MongoDb\Timestamp
* @Gedmo\Timestampable(on="create")
*/
protected $created;

/**
* @var datetime $updated
*
* @MongoDb\Timestamp
* @Gedmo\Timestampable()
*/
protected $updated;

public function __construct()
{
$this->setRequests(0);
}

public function incrementRequests()
{
$this->setRequests($this->getRequests() + 1);
}

public function getId()
{
return $this->id;
}

/**
* Set lastStatus
*
* @param string $lastStatus
*/
public function setLastStatus($lastStatus)
{
$this->lastStatus = $lastStatus;
return $this;
}

/**
* Get lastStatus
*
* @return string
*/
public function getLastStatus()
{
return $this->lastStatus;
}

/**
* Set requests
*
* @param integer $requests
*/
public function setRequests($requests)
{
$this->requests = $requests;
return $this;
}

/**
* Get requests
*
* @return integer
*/
public function getRequests()
{
return $this->requests;
}

/**
* Set created
*
* @param date $created
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}

/**
* Get created
*
* @return date
*/
public function getCreated()
{
return $this->created;
}

/**
* Set updated
*
* @param datetime $updated
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}

/**
* Get updated
*
* @return datetime
*/
public function getUpdated()
{
return $this->updated;
}
}
39 changes: 39 additions & 0 deletions Document/ApiLogRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Google\GoogleGeolocationBundle\Document;

use Doctrine\ODM\MongoDB\DocumentRepository;

class ApiLogRepository extends DocumentRepository
{
public function getLogForDate(\DateTime $date = null)
{
if (true === is_null($date))
{
$date = new \DateTime();
}

$qb = $this->createQueryBuilder('GoogleGeolocationBundle:ApiLog')
->field('created')->equals($date)
;

try {
return $qb->getQuery()
->getSingleResult();
} catch (Doctrine\ODM\MongoDB\MongoDBException $e) {
return null;
}
}

public function cleanCache()
{
$from = new \DateTime('today');

return $this->createQueryBuilder('GoogleGeolocationBundle:ApiLog')
->remove()
->field('created')->lt($from)
->getQuery()
->execute();
}

}
Loading