From 5a7227eb16d164fb962b94b7fab0f2cf5585a739 Mon Sep 17 00:00:00 2001 From: Abdulrahman Date: Mon, 9 Sep 2019 23:01:40 +0300 Subject: [PATCH 1/2] expose some informative methods --- .travis.yml | 2 +- src/Traits/Record.php | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 87106c1..99ff5db 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/src/Traits/Record.php b/src/Traits/Record.php index 84cd5c8..7efda45 100644 --- a/src/Traits/Record.php +++ b/src/Traits/Record.php @@ -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()); } /** @@ -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); } @@ -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(); } } From a312fcd773dab82ca341a5dada4a22c8d5d65678 Mon Sep 17 00:00:00 2001 From: Abdulrahman Date: Tue, 10 Sep 2019 14:54:42 +0300 Subject: [PATCH 2/2] fix missing string methods --- src/Keys.php | 2 +- src/Traits/Periods.php | 3 ++- src/Visits.php | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Keys.php b/src/Keys.php index 689f61a..ebf3fa9 100644 --- a/src/Keys.php +++ b/src/Keys.php @@ -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); } diff --git a/src/Traits/Periods.php b/src/Traits/Periods.php index c85ccd4..4f1d376 100644 --- a/src/Traits/Periods.php +++ b/src/Traits/Periods.php @@ -3,6 +3,7 @@ namespace awssat\Visits\Traits; use Illuminate\Support\Carbon; +use Illuminate\Support\Str; use Exception; trait Periods @@ -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."); } diff --git a/src/Visits.php b/src/Visits.php index 4419cf5..c89c370 100644 --- a/src/Visits.php +++ b/src/Visits.php @@ -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; @@ -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); } } }