Skip to content

Commit

Permalink
Merge pull request #28 from abdumu/current-visit-info
Browse files Browse the repository at this point in the history
expose some informative methods
  • Loading branch information
abdumu authored Sep 10, 2019
2 parents 8f3386c + a312fcd commit aa79c94
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: php
php:
- 7.0
- 7.1
- 7.2
- 7.3
before_script:
- composer self-update
- composer install --prefer-source --no-interaction
Expand Down
2 changes: 1 addition & 1 deletion src/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function visitsTotal()
public function ip($ip)
{
return $this->visits . '_' .
snake_case("recorded_ips:" . ($this->instanceOfModel ? "{$this->id}:" : '') . $ip);
Str::snake("recorded_ips:" . ($this->instanceOfModel ? "{$this->id}:" : '') . $ip);
}


Expand Down
3 changes: 2 additions & 1 deletion src/Traits/Periods.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace awssat\Visits\Traits;

use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Exception;

trait Periods
Expand Down Expand Up @@ -42,7 +43,7 @@ protected function noExpiration($periodKey)
protected function newExpiration($period)
{
try {
$periodCarbon = $this->xHoursPeriod($period) ?? Carbon::now()->{'endOf' . studly_case($period)}();
$periodCarbon = $this->xHoursPeriod($period) ?? Carbon::now()->{'endOf' . Str::studly($period)}();
} catch (Exception $e) {
throw new Exception("Wrong period: `{$period}`! please update config/visits.php file.");
}
Expand Down
19 changes: 15 additions & 4 deletions src/Traits/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ protected function recordCountry($inc)
*/
protected function recordRefer($inc)
{
$referer = app(Referer::class)->get();
$this->redis->zincrby($this->keys->visits."_referers:{$this->keys->id}", $inc, $referer);
$this->redis->zincrby($this->keys->visits."_referers:{$this->keys->id}", $inc, $this->getVisitorReferer());
}

/**
Expand Down Expand Up @@ -56,7 +55,7 @@ protected function recordPeriods($inc)
* Gets visitor country code
* @return mixed|string
*/
protected function getVisitorCountry()
public function getVisitorCountry()
{
return strtolower(geoip()->getLocation()->iso_code);
}
Expand Down Expand Up @@ -94,7 +93,19 @@ public function getVisitorOperatingSystem()
*/
public function getVisitorLanguage()
{
$language = request()->getPreferredLanguage();
if (false !== $position = strpos($language, '_')) {
$language = substr($language, 0, $position);
}
return $language;
}

return request()->getPreferredLanguage();
/**
* Gets visitor referer
* @return mixed|string
*/
public function getVisitorReferer()
{
return app(Referer::class)->get();
}
}
3 changes: 2 additions & 1 deletion src/Visits.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Redis;
use Jaybizzle\CrawlerDetect\CrawlerDetect;

Expand Down Expand Up @@ -192,7 +193,7 @@ public function increment($inc = 1, $force = false, $ignore = [])
//NOTE: $$method is parameter also .. ($periods,$country,$refer)
foreach (['country', 'refer', 'periods', 'operatingSystem', 'language'] as $method) {
if(! in_array($method, $ignore)) {
$this->{'record'.studly_case($method)}($inc);
$this->{'record'.Str::studly($method)}($inc);
}
}
}
Expand Down

0 comments on commit aa79c94

Please sign in to comment.