Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwedgbury authored and StyleCIBot committed May 13, 2020
1 parent f8b0f36 commit 11a0d53
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/ShopWiredThrottle.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Appoly\ShopWiredPHPSDK;

use Redis;
Expand Down Expand Up @@ -28,8 +27,9 @@ public static function isBucketFull()
$recentEvents = self::getRecentEvents();
//dump("Bucket count " . count($recentEvents));

if(count($recentEvents) >= self::BUCKET_SIZE)
if (count($recentEvents) >= self::BUCKET_SIZE) {
return true;
}

return false;
}
Expand All @@ -44,14 +44,13 @@ public static function addEvent()
public static function leakEvents()
{
$lastLeaked = (float) self::cacheGet('shopwired_last_leaked');
if($lastLeaked && $lastLeaked >= (microtime(true) - 1)) {
if ($lastLeaked && $lastLeaked >= (microtime(true) - 1)) {
return;
}


$lastLeakedSecondsAgo = (int) floor(microtime(true) - $lastLeaked);

foreach(range(1, $lastLeakedSecondsAgo) as $leakEachSecond) {
foreach (range(1, $lastLeakedSecondsAgo) as $leakEachSecond) {
//dump("Leaking " . $lastLeakedSecondsAgo);
$recentEvents = self::getRecentEvents();

Expand All @@ -61,41 +60,47 @@ public static function leakEvents()
}
}

public static function getRecentEvents() {
public static function getRecentEvents()
{
$recentEvents = json_decode(self::cacheGet('shopwired_events'));
$recentEvents = $recentEvents ? $recentEvents : [];

return $recentEvents;
}

public static function cacheGet($key) {
public static function cacheGet($key)
{
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

return $redis->get($key);
}

public static function cacheSet($key, $value) {
public static function cacheSet($key, $value)
{
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$redis->set($key, $value);
}

public static function redisConnect() {
public static function redisConnect()
{
try {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
} catch(\Exception $e) {
throw new \Exception("ShopWired: Error connecting to redis for request throttling. Please add ShopWiredThrottle::disable();", 500);
} catch (\Exception $e) {
throw new \Exception('ShopWired: Error connecting to redis for request throttling. Please add ShopWiredThrottle::disable();', 500);
}
}

public static function disable() {
public static function disable()
{
self::$enabled = false;
}

public static function enable() {
public static function enable()
{
self::$enabled = true;
}
}

0 comments on commit 11a0d53

Please sign in to comment.