Skip to content

Commit

Permalink
fixes eloquent issues #45 #41
Browse files Browse the repository at this point in the history
  • Loading branch information
abdumu committed Apr 4, 2020
1 parent f18f7bf commit 9bee98f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/DataEngines/DataEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
public function connect(string $connection): self;
public function setPrefix(string $prefix): self;

public function increment(string $key, int $value, ?string $member = null): bool;
public function decrement(string $key, int $value, ?string $member = null): bool;
public function increment(string $key, int $value, $member = null): bool;
public function decrement(string $key, int $value, $member = null): bool;

public function delete($key, ?string $member = null): bool;
public function get(string $key, ?string $member = null);
public function set(string $key, $value, ?string $member = null): bool;
public function delete($key, $member = null): bool;
public function get(string $key, $member = null);
public function set(string $key, $value, $member = null): bool;

public function flatList(string $key, int $limit): array;
public function addToFlatList(string $key, $value): bool;
Expand Down
14 changes: 7 additions & 7 deletions src/DataEngines/EloquentEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setPrefix(string $prefix): DataEngine
return $this;
}

public function increment(string $key, int $value, ?string $member = null): bool
public function increment(string $key, int $value, $member = null): bool
{
if (! empty($member) || is_numeric($member)) {
$row = $this->model->firstOrNew(['primary_key' => $this->prefix.$key, 'secondary_key' => $member]);
Expand All @@ -42,12 +42,12 @@ public function increment(string $key, int $value, ?string $member = null): bool
return $row->save();
}

public function decrement(string $key, int $value, ?string $member = null): bool
public function decrement(string $key, int $value, $member = null): bool
{
return $this->increment($key, -$value, $member);
}

public function delete($key, ?string $member = null): bool
public function delete($key, $member = null): bool
{
if(is_array($key)) {
array_walk($key, function($item) {
Expand All @@ -63,7 +63,7 @@ public function delete($key, ?string $member = null): bool
}
}

public function get(string $key, ?string $member = null)
public function get(string $key, $member = null)
{
if(! empty($member) || is_numeric($member)) {
return $this->model->where(['primary_key' => $this->prefix.$key, 'secondary_key' => $member])
Expand All @@ -80,16 +80,16 @@ public function get(string $key, ?string $member = null)
}
}

public function set(string $key, $value, ?string $member = null): bool
public function set(string $key, $value, $member = null): bool
{
if(! empty($member) || is_numeric($member)) {
return $this->model->create([
return $this->model->updateOrCreate([
'primary_key' => $this->prefix.$key,
'secondary_key' => $member,
'score' => $value,
]) instanceof Model;
} else {
return $this->model->create([
return $this->model->updateOrCreate([
'primary_key' => $this->prefix.$key,
'score' => $value,
]) instanceof Model;
Expand Down
16 changes: 8 additions & 8 deletions src/DataEngines/RedisEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ public function setPrefix(string $prefix): DataEngine
return $this;
}

public function increment(string $key, int $value, ?string $member = null): bool
public function increment(string $key, int $value, $member = null): bool
{
if (! empty($member) || is_numeric($member)) {
$this->connection->zincrby($this->prefix.$key, $value, $member);
} else {
$this->connection->incrby($this->prefix.$key, $value);
}
// both methods returns integer and raise an excpetion in case of an error.

// both methods returns integer and raise an exception in case of an error.
return true;
}

public function decrement(string $key, int $value, ?string $member = null): bool
public function decrement(string $key, int $value, $member = null): bool
{
return $this->increment($key, -$value, $member);
}

public function delete($key, ?string $member = null): bool
public function delete($key, $member = null): bool
{
if(is_array($key)) {
array_walk($key, function($item) {
Expand All @@ -62,7 +62,7 @@ public function delete($key, ?string $member = null): bool
}
}

public function get(string $key, ?string $member = null)
public function get(string $key, $member = null)
{
if(! empty($member) || is_numeric($member)) {
return $this->connection->zscore($this->prefix.$key, $member);
Expand All @@ -71,7 +71,7 @@ public function get(string $key, ?string $member = null)
}
}

public function set(string $key, $value, ?string $member = null): bool
public function set(string $key, $value, $member = null): bool
{
if(! empty($member) || is_numeric($member)) {
return $this->connection->zAdd($this->prefix.$key, $value, $member) > 0;
Expand Down Expand Up @@ -108,7 +108,7 @@ public function valueList(string $key, int $limit = -1, bool $orderByAsc = false
{
$range = $orderByAsc ? 'zrange' : 'zrevrange';

return $this->connection->$range($this->prefix.$key, 0, $limit, $this->isPHPRedis ? $withValues : ['withscores' => $withValues]);
return $this->connection->$range($this->prefix.$key, 0, $limit, $this->isPHPRedis ? $withValues : ['withscores' => $withValues]) ?: [];
}

public function exists(string $key): bool
Expand Down
40 changes: 30 additions & 10 deletions src/Visits.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Awssat\Visits;

use Awssat\Visits\Models\Visit;
use Awssat\Visits\Traits\{Lists, Periods, Record, Setters};
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -63,31 +64,38 @@ class Visits
*/
public $globalIgnore = [];

/**
* @var array
*/
public $config = [];

/**
* @param \Illuminate\Database\Eloquent\Model $subject any model
* @param string $tag use only if you want to use visits on multiple models
*/
public function __construct($subject = null, $tag = 'visits')
{
$config = config('visits');
$this->config = config('visits');

$this->connection = $this->determineConnection($config['engine'] ?? 'redis')
->connect($config['connection'])
->setPrefix($config['keys_prefix'] ?? $config['redis_keys_prefix'] ?? 'visits');
$this->connection = $this->determineConnection($this->config['engine'] ?? 'redis')
->connect($this->config['connection'])
->setPrefix($this->config['keys_prefix'] ?? $this->config['redis_keys_prefix'] ?? 'visits');

if(! $this->connection) {
return;
}

$this->periods = $config['periods'];
$this->ipSeconds = $config['remember_ip'];
$this->fresh = $config['always_fresh'];
$this->ignoreCrawlers = $config['ignore_crawlers'];
$this->globalIgnore = $config['global_ignore'];
$this->periods = $this->config['periods'];
$this->ipSeconds = $this->config['remember_ip'];
$this->fresh = $this->config['always_fresh'];
$this->ignoreCrawlers = $this->config['ignore_crawlers'];
$this->globalIgnore = $this->config['global_ignore'];
$this->subject = $subject;
$this->keys = new Keys($subject, preg_replace('/[^a-z0-9_]/i', '', $tag));

$this->periodsSync();
if (! empty($this->keys->id)) {
$this->periodsSync();
}
}

protected function determineConnection($name)
Expand Down Expand Up @@ -200,6 +208,7 @@ protected function isCrawler()
public function increment($inc = 1, $force = false, $ignore = [])
{
if ($force || (!$this->isCrawler() && !$this->recordedIp())) {

$this->connection->increment($this->keys->visits, $inc, $this->keys->id);
$this->connection->increment($this->keys->visitsTotal(), $inc);

Expand Down Expand Up @@ -262,4 +271,15 @@ public function expireAt($period, $time)
$periodKey = $this->keys->period($period);
return $this->connection->setExpiration($periodKey, $time);
}

/**
* To be used with models to integrate relationship with visits model.
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function relation()
{
$prefix = $this->config['keys_prefix'] ?? $this->config['redis_keys_prefix'] ?? 'visits';

return $this->subject->hasOne(Visit::class, 'secondary_key')->where('primary_key', $prefix.':'.$this->keys->visits);
}
}
1 change: 0 additions & 1 deletion tests/Feature/EloquentVisitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Awssat\Visits\Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Redis;

class EloquentVisitsTest extends VisitsTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ protected function getEnvironmentSetUp($app)
'driver' => 'sqlite',
'database' => ':memory:',
]);
$app['config']->set('visits.global_ignore', []);

}
/**
* Run migrations for tables used for testing purposes.
Expand Down

0 comments on commit 9bee98f

Please sign in to comment.