Skip to content

Commit

Permalink
Clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Bader Al-Mutairi authored and Bader Al-Mutairi committed Jul 31, 2018
1 parent 194cb89 commit f8882ea
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 87 deletions.
15 changes: 0 additions & 15 deletions .editorconfig

This file was deleted.

2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

All Notable changes to `Bareq` will be documented in this file.
All Notable changes to `laravel-visits` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

Expand Down
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,46 @@ composer require awssat/laravel-visits


#### Before Laravel 5.5
In Laravel 5.4. you'll manually need to register the `if4lcon\Bareq\BareqServiceProvider::class` service provider in `config/app.php`.
In Laravel 5.4. you'll manually need to register the `awssat\Visits\VisitsServiceProvider::class` service provider in `config/app.php`.

#### Config
To adjust the library, you can publish the config file to your project using:
```
php artisan vendor:publish --provider="if4lcon\Bareq\BareqServiceProvider"
php artisan vendor:publish --provider="awssat\Visits\VisitsServiceProvider"
```

### Upgrade to 1.4.0 from 1.3.*

- Prfiex updated from `bareq` to `visits` if you missing your data try to revert prefex value to `bareq`



### Note : Redis Database Name

- By default `laravel-visits` doesn't use the default laravel redis configuration see issue (see [issue #5](https://github.com/awssat/laravel-visits/issues/5))

To prvent your data loss add a new conection on `config/database.php`

``` php

'laravel-visits' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 3, // anything from 1 to 15, except 0 (or what is set in default)
],

```

and you can define your redis connection name on `config/visits.php`

``` php

'connection' => 'default'

```


## Usage

It's simple. Using `visits` helper as:
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
K{
"name": "awssat/laravel-visits",
"type": "library",
"description": "Laravel Redis visits counter for Eloquent models",
"keywords": [
"Laravel", "Visits", "Counter", "Package", "Redis", "Cache", "Php", "Bareq"
"Laravel", "Visits", "Counter", "Package", "Redis", "Cache", "Php"
],
"homepage": "https://github.com/awssat/laravel-visits",
"license": "MIT",
Expand All @@ -30,15 +30,15 @@
},
"autoload": {
"psr-4": {
"if4lcon\\Bareq\\": "src"
"awssat\\Visits\\": "src"
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"if4lcon\\Bareq\\Tests\\": "tests"
"awssat\\Visits\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -52,10 +52,10 @@
},
"laravel": {
"providers": [
"if4lcon\\Bareq\\BareqServiceProvider"
"awssat\\Visits\\VisitsServiceProvider"
],
"aliases": {
"Visits": "if4lcon\\Bareq\\Visits"
"Visits": "awssat\\Visits\\Visits"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/Keys.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace if4lcon\Bareq;
namespace awssat\Visits;

use Illuminate\Database\Eloquent\Model;

Expand All @@ -23,7 +23,7 @@ class Keys
public function __construct($subject, $tag)
{
$this->modelName = strtolower(str_plural(class_basename(is_string($subject) ? $subject : get_class($subject))));
$this->prefix = config('bareq.redis_keys_prefix');
$this->prefix = config('visits.redis_keys_prefix');
$this->testing = app()->environment('testing') ? 'testing:' : '';
$this->primary = (new $subject)->getKeyName();
$this->tag = $tag;
Expand Down
4 changes: 1 addition & 3 deletions src/Reset.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

namespace if4lcon\Bareq;

use Illuminate\Support\Facades\Redis;
namespace awssat\Visits;

class Reset extends Visits
{
Expand Down
2 changes: 1 addition & 1 deletion src/Visits.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace if4lcon\Bareq;
namespace awssat\Visits;

use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;
Expand Down
4 changes: 2 additions & 2 deletions src/VisitsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace if4lcon\Bareq;
namespace awssat\Visits;

use Illuminate\Support\ServiceProvider;

class BareqServiceProvider extends ServiceProvider
class VisitsServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
Expand Down
13 changes: 12 additions & 1 deletion src/config/visits.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
| Redis prefix
|--------------------------------------------------------------------------
*/
'redis_keys_prefix' => 'bareq',
'redis_keys_prefix' => 'visits',

/*
|--------------------------------------------------------------------------
Expand All @@ -43,5 +43,16 @@
*/
'always_fresh' => false,

/*
|--------------------------------------------------------------------------
| Redis Database Connection Name
|--------------------------------------------------------------------------
|
| When using "redis" you may specify a
| connection that should be used to manage your database storage. This should
| correspond to a connection in your database configuration options.
|
*/
'connection' => 'laravel-visits',
];

2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{
function visits($subject, $tag = 'visits')
{
return new \if4lcon\Bareq\Visits($subject, $tag);
return new \awssat\Visits\Visits($subject, $tag);
}
}
80 changes: 30 additions & 50 deletions tests/Feature/VisitsTest.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,41 @@
<?php

namespace if4lcon\Bareq\Tests\Feature;
namespace awssat\Visits\Tests\Feature;

use awssat\Visits\Tests\TestCase;
use Carbon\Carbon;
use if4lcon\Bareq\Tests\Post;
use awssat\Visits\Tests\Post;
use Illuminate\Support\Facades\Redis;
use if4lcon\Bareq\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class VisitsTest extends TestCase
{
use RefreshDatabase;

protected static $runSetup = false;
protected $redis;

public function setUp()
{
parent::setUp();

if (static::$runSetup) {

$this->app['config']['database.redis.laravel-visits'] = [
$this->app['config']['database.redis.laravel-visits'] = [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_timeout' => 60,
];
'database' => 3,
];

$cc = Redis::keys('bareq:testing:*');
$this->redis = Redis::connection('laravel-visits');

if (count($cc)) {
Redis::del($cc);
}
if (count($cc = $this->redis->keys('visits:testing:*'))) {
$this->redis->del($cc);
}
}

/** @test * */
public function config_test_laravel_visits()
public function laravel_visits_is_the_default_connection()
{
$this->app['config']['database.redis.laravel-visits'] = [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_timeout' => 60,
];

$this->assertEquals('laravel-visits', visits(Post::create()->fresh())->connection());
}

/** @test * */
public function config_test_default()
{
$this->assertEmpty(visits(Post::create()->fresh())->connection());

static::$runSetup = true;
$this->assertEquals('laravel-visits', config('visits.connection'));
}

/** @test */
Expand All @@ -68,11 +48,11 @@ public function multi_tags_storing()
visits($userA, 'clicks')->increment();
visits($userA, 'clicks2')->increment();

$keys = Redis::keys('bareq:testing:*');
$keys = $this->redis->keys('visits:testing:*');

$this->assertContains('bareq:testing:posts_visits', $keys);
$this->assertContains('bareq:testing:posts_clicks', $keys);
$this->assertContains('bareq:testing:posts_clicks2', $keys);
$this->assertContains('visits:testing:posts_visits', $keys);
$this->assertContains('visits:testing:posts_clicks', $keys);
$this->assertContains('visits:testing:posts_clicks2', $keys);
}

/** @test */
Expand Down Expand Up @@ -200,7 +180,7 @@ public function it_reset_counter()

$this->assertEquals(
[2, 3],
visits('if4lcon\Bareq\Tests\Post')->top(5)->pluck('id')->toArray()
visits('awssat\Visits\Tests\Post')->top(5)->pluck('id')->toArray()
);

}
Expand All @@ -218,10 +198,10 @@ public function reset_specific_ip()
'129.0.0.2',
'124.0.0.2'
];
$key = config('bareq.redis_keys_prefix') . ":testing:recorded_ips:Post_1:";
$key = "visits:testing:recorded_ips:Post_1:";

foreach ($ips as $ip) {
Redis::set( $key . $ip, true, 'EX', 15 * 60, 'NX');
$this->redis->set( $key . $ip, true, 'EX', 15 * 60, 'NX');
}

visits($post)->increment(10);
Expand All @@ -234,7 +214,7 @@ public function reset_specific_ip()
visits($post)->reset('ips', '127.0.0.1');


$ips_in_redis = collect(Redis::keys(config('bareq.redis_keys_prefix') . ":testing:recorded_ips:*"))->map(function ($ip) use ($key) {
$ips_in_redis = collect($this->redis->keys(config('visits.redis_keys_prefix') . ":testing:recorded_ips:*"))->map(function ($ip) use ($key) {
return str_replace($key, '', $ip);
});

Expand Down Expand Up @@ -278,32 +258,32 @@ public function it_shows_proper_tops_and_lows()

$this->assertEquals(
collect($arr)->sort()->reverse()->keys()->take(10)->toArray(),
visits('if4lcon\Bareq\Tests\Post')->period('day')->top(10)->pluck('id')->toArray()
visits('awssat\Visits\Tests\Post')->period('day')->top(10)->pluck('id')->toArray()
);

$this->assertEquals(
collect($arr)->sort()->keys()->take(10)->toArray(),
visits('if4lcon\Bareq\Tests\Post')->period('day')->low(11)->pluck('id')->toArray()
visits('awssat\Visits\Tests\Post')->period('day')->low(11)->pluck('id')->toArray()
);


visits('if4lcon\Bareq\Tests\Post')->period('day')->reset();
visits('awssat\Visits\Tests\Post')->period('day')->reset();

$this->assertEquals(0,
visits('if4lcon\Bareq\Tests\Post')->period('day')->count()
visits('awssat\Visits\Tests\Post')->period('day')->count()
);

$this->assertEmpty(
visits('if4lcon\Bareq\Tests\Post')->period('day')->top(10)
visits('awssat\Visits\Tests\Post')->period('day')->top(10)
);

$this->assertNotEmpty(
visits('if4lcon\Bareq\Tests\Post')->top(10)
visits('awssat\Visits\Tests\Post')->top(10)
);

$this->assertEquals(
collect($arr)->sum(),
visits('if4lcon\Bareq\Tests\Post')->count()
visits('awssat\Visits\Tests\Post')->count()
);

}
Expand Down Expand Up @@ -392,15 +372,15 @@ public function it_list_from_cache()
visits($post3)->forceIncrement(2);
visits($post4)->forceIncrement(1);

$fresh = visits('if4lcon\Bareq\Tests\Post')->top()->pluck('name');
$fresh = visits('awssat\Visits\Tests\Post')->top()->pluck('name');

$post5->update(['name' => 'changed']);

$cached = visits('if4lcon\Bareq\Tests\Post')->top()->pluck('name');
$cached = visits('awssat\Visits\Tests\Post')->top()->pluck('name');

$this->assertEquals($fresh->first(), $cached->first());

$fresh2 = visits('if4lcon\Bareq\Tests\Post')
$fresh2 = visits('awssat\Visits\Tests\Post')
->fresh()
->top()
->pluck('name');
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace if4lcon\Bareq\Tests;
namespace awssat\Visits\Tests;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use if4lcon\Bareq\BareqServiceProvider;
use awssat\Visits\VisitsServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Spatie\Referer\Referer;
use Spatie\Referer\CaptureReferer;
Expand Down Expand Up @@ -56,7 +56,7 @@ protected function getPackageProviders($app)
return [
GeoIPServiceProvider::class,
RefererServiceProvider::class,
BareqServiceProvider::class,
VisitsServiceProvider::class,
];
}

Expand Down

0 comments on commit f8882ea

Please sign in to comment.