Skip to content

Commit

Permalink
Merge pull request #80 from abdumu/master
Browse files Browse the repository at this point in the history
Fixes wrongtype error when increment has member that is null instead of being having always a value
  • Loading branch information
BSN4 authored May 25, 2022
2 parents f0cd6fc + b8acc9f commit 0aa19ff
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 93 deletions.
92 changes: 43 additions & 49 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,56 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.3, 7.4, 8.0, 8.1]
laravel: [6.*, 7.*, 8.*, 9.*]
php: [7.4, 8.0, 8.1]
laravel: [7.*, 8.*, 9.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: 7.*
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
- laravel: 6.*
testbench: 4.*
- laravel: 9.*
testbench: 7.*
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
exclude:
# excludes php7.3, php7.4 for laravel 9
- php: 7.3
laravel: 9.*
- php: 7.4
laravel: 9.*
# excludes laravel 6+7 on php8.1
- php: 8.1
laravel: 6.*
- php: 8.1
laravel: 7.*
# excludes laravel 6+7 on php8
- php: 8.0
laravel: 6.*
- php: 8.0
laravel: 7.*
# excludes laravel 9 on php7.4
- php: 7.4
laravel: 9.*
# excludes laravel 7 on php8
- php: 8.0
laravel: 7.*
# excludes laravel 7+8 on php8.1
- php: 8.1
laravel: 7.*
- php: 8.1
laravel: 8.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Checkout code
uses: actions/checkout@v2

- name: Install SQLite 3
run: |
sudo apt-get update
sudo apt-get install sqlite3
sudo apt-get install redis
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.composer/cache/files
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
- name: Install SQLite 3
run: |
sudo apt-get update
sudo apt-get install sqlite3
sudo apt-get install redis
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.composer/cache/files
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv, redis
coverage: none
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv, redis
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Execute tests
run: vendor/bin/phpunit tests
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Execute tests
run: vendor/bin/phpunit tests
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
"type": "library",
"description": "Laravel Redis visits counter for Eloquent models",
"keywords": [
"Laravel", "Visits", "Counter", "Package", "Redis", "Cache", "Php"
"Laravel",
"Visits",
"Counter",
"Package",
"Redis",
"Cache",
"Php"
],
"homepage": "https://github.com/awssat/laravel-visits",
"license": "MIT",
Expand All @@ -29,7 +35,7 @@
"illuminate/support": "~5.5.0 || ~5.6.0 || ~5.7.0 || ~5.8.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
"mockery/mockery": "^1.4 || ^2.0",
"orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0 || ^5.0 || ^6.0 || ^7.0",
"phpunit/phpunit": "^9.3",
"phpunit/phpunit": "^9.0",
"predis/predis": "^1.1"
},
"suggest": {
Expand Down Expand Up @@ -71,4 +77,4 @@
"config": {
"sort-packages": true
}
}
}
61 changes: 36 additions & 25 deletions src/DataEngines/RedisEngine.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Awssat\Visits\DataEngines;


Expand Down Expand Up @@ -31,10 +32,20 @@ public function setPrefix(string $prefix): DataEngine

public function increment(string $key, int $value, $member = null): bool
{
if (! empty($member) || is_numeric($member)) {
$this->connection->zincrby($this->prefix.$key, $value, $member);
if (!empty($member) || is_numeric($member)) {
try {
$this->connection->zincrby($this->prefix . $key, $value, $member);
} catch (\Exception $e) {
if (strpos($e->getMessage(), 'WRONGTYPE') !== false) {
//key was not saved properly TODO: find better way to handle this to support both phpredis and predis
$this->delete($key);
} else {
throw $e;
}
return false;
}
} else {
$this->connection->incrby($this->prefix.$key, $value);
$this->connection->incrby($this->prefix . $key, $value);
}

// both methods returns integer and raise an exception in case of an error.
Expand All @@ -48,81 +59,81 @@ public function decrement(string $key, int $value, $member = null): bool

public function delete($key, $member = null): bool
{
if(is_array($key)) {
array_walk($key, function($item) {
if (is_array($key)) {
array_walk($key, function ($item) {
$this->delete($item);
});
return true;
}

if(! empty($member) || is_numeric($member)) {
return $this->connection->zrem($this->prefix.$key, $member) > 0;
if (!empty($member) || is_numeric($member)) {
return $this->connection->zrem($this->prefix . $key, $member) > 0;
} else {
return $this->connection->del($this->prefix.$key) > 0;
return $this->connection->del($this->prefix . $key) > 0;
}
}

public function get(string $key, $member = null)
{
if(! empty($member) || is_numeric($member)) {
return $this->connection->zscore($this->prefix.$key, $member);
if (!empty($member) || is_numeric($member)) {
return $this->connection->zscore($this->prefix . $key, $member);
} else {
return $this->connection->get($this->prefix.$key);
return $this->connection->get($this->prefix . $key);
}
}

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;
if (!empty($member) || is_numeric($member)) {
return $this->connection->zAdd($this->prefix . $key, $value, $member) > 0;
} else {
return (bool) $this->connection->set($this->prefix.$key, $value);
return (bool) $this->connection->set($this->prefix . $key, $value);
}
}

public function search(string $word, bool $noPrefix = true): array
{
return array_map(
function($item) use($noPrefix) {
function ($item) use ($noPrefix) {
if ($noPrefix && substr($item, 0, strlen($this->prefix)) == $this->prefix) {
return substr($item, strlen($this->prefix));
}
}

return $item;
},
$this->connection->keys($this->prefix.$word) ?? []
},
$this->connection->keys($this->prefix . $word) ?? []
);
}

public function flatList(string $key, int $limit = -1): array
{
return $this->connection->lrange($this->prefix.$key, 0, $limit);
return $this->connection->lrange($this->prefix . $key, 0, $limit);
}

public function addToFlatList(string $key, $value): bool
{
return $this->connection->rpush($this->prefix.$key, $value) !== false;
return $this->connection->rpush($this->prefix . $key, $value) !== false;
}

public function valueList(string $key, int $limit = -1, bool $orderByAsc = false, bool $withValues = false): array
{
$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
{
return (bool) $this->connection->exists($this->prefix.$key);
return (bool) $this->connection->exists($this->prefix . $key);
}

public function timeLeft(string $key): int
{
return $this->connection->ttl($this->prefix.$key);
return $this->connection->ttl($this->prefix . $key);
}

public function setExpiration(string $key, int $time): bool
{
return $this->connection->expire($this->prefix.$key, $time);
return $this->connection->expire($this->prefix . $key, $time);
}
}
}
32 changes: 16 additions & 16 deletions src/Traits/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ trait Record
*/
protected function recordCountry($inc)
{
$this->connection->increment($this->keys->visits."_countries:{$this->keys->id}", $inc, $this->getVisitorCountry());
$this->connection->increment($this->keys->visits . "_countries:{$this->keys->id}", $inc, $this->getVisitorCountry());
}

/**
* @param $inc
*/
protected function recordRefer($inc)
{
$this->connection->increment($this->keys->visits."_referers:{$this->keys->id}", $inc, $this->getVisitorReferer());
$this->connection->increment($this->keys->visits . "_referers:{$this->keys->id}", $inc, $this->getVisitorReferer());
}

/**
* @param $inc
*/
protected function recordOperatingSystem($inc)
{
$this->connection->increment($this->keys->visits."_OSes:{$this->keys->id}", $inc, $this->getVisitorOperatingSystem());
$this->connection->increment($this->keys->visits . "_OSes:{$this->keys->id}", $inc, $this->getVisitorOperatingSystem());
}

/**
* @param $inc
*/
protected function recordLanguage($inc)
{
$this->connection->increment($this->keys->visits."_languages:{$this->keys->id}", $inc, $this->getVisitorLanguage());
$this->connection->increment($this->keys->visits . "_languages:{$this->keys->id}", $inc, $this->getVisitorLanguage());
}

/**
Expand All @@ -47,7 +47,7 @@ protected function recordPeriods($inc)
$periodKey = $this->keys->period($period);

$this->connection->increment($periodKey, $inc, $this->keys->id);
$this->connection->increment($periodKey.'_total', $inc);
$this->connection->increment($periodKey . '_total', $inc);
}
}

Expand All @@ -63,7 +63,7 @@ public function getVisitorCountry()
in_array(config('cache.default'), ['file', 'dynamodb', 'database']) &&
is_array($geoipTags = config('geoip.cache_tags')) && count($geoipTags) > 0
) {
return null;
return 'zz';
}

return strtolower(geoip()->getLocation()->iso_code);
Expand All @@ -76,15 +76,15 @@ public function getVisitorCountry()
public function getVisitorOperatingSystem()
{
$osArray = [
'/windows|win32|win16|win95/i' => 'Windows',
'/iphone/i' => 'iPhone',
'/ipad/i' => 'iPad',
'/macintosh|mac os x|mac_powerpc/i' => 'MacOS',
'/(?=.*mobile)android/i' => 'AndroidMobile',
'/(?!.*mobile)android/i' => 'AndroidTablet',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/linux/i' => 'Linux',
'/windows|win32|win16|win95/i' => 'Windows',
'/iphone/i' => 'iPhone',
'/ipad/i' => 'iPad',
'/macintosh|mac os x|mac_powerpc/i' => 'MacOS',
'/(?=.*mobile)android/i' => 'AndroidMobile',
'/(?!.*mobile)android/i' => 'AndroidTablet',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/linux/i' => 'Linux',
];

foreach ($osArray as $regex => $value) {
Expand Down Expand Up @@ -115,6 +115,6 @@ public function getVisitorLanguage()
*/
public function getVisitorReferer()
{
return app(Referer::class)->get();
return app(Referer::class)->get() ?? 'direct';
}
}

0 comments on commit 0aa19ff

Please sign in to comment.