Open rattfieldnz opened 7 years ago
I'm also using version Laravel 5.4.28, and version 3.1.4 of this package. If it will also help, my full Composer file is attached...
Here is a slight modification I have made to the Session class to make it 'work'...
Constructor:
<?php
namespace App\ThirdParty\PragmaRX\Tracker\Vendor\Laravel\Support;
use Illuminate\Support\Facades\Input;
use PragmaRX\Tracker\Support\Minutes;
use Session as LaravelSession;
/**
* Class Session
*
* This class is a modified copy of pragmarx/tracker's Session class,
* will be used until issue at https://github.com/antonioribeiro/tracker/issues/297
* is resolved (non numeric value for 'days' causing exception/error).
*
* @package App\ThirdParty\PragmaRX\Tracker\Vendor\Laravel\Support
*/
class Session
{
private $minutes;
public function __construct()
{
LaravelSession::put('tracker.stats.days', $this->getValue('days', 1));
LaravelSession::put('tracker.stats.page', $this->getValue('page', 'visits'));
$this->minutes = $this->getMinutesFromDays(LaravelSession::get('tracker.stats.days'));
}
/**
* @return Minutes
*/
public function getMinutes()
{
return $this->minutes;
}
/**
* Get total amount of minutes from given amount of days.
* Returns number of minutes for 1 day by default.
*
* @param $days
*
* @return \PragmaRX\Tracker\Support\Minutes
*/
public function getMinutesFromDays($days): Minutes {
// Use 0 as default number of days.
// Will return 0 minutes from current time by default,
// if entered value is not numeric.
return !is_numeric($days) ?
new Minutes(60 * 24 * 0) :
new Minutes(60 * 24 * $days);
}
public function getValue($variable, $default = null)
{
if (Input::has($variable)) {
$value = Input::get($variable);
} else {
$value = LaravelSession::get('tracker.stats.'.$variable, $default);
}
return $value;
}
}
You will notice the change in the class constructor... I have basically created a function called 'getMinutesFromDays'. This does a basic check to see if the value is numeric - it returns a default value if not, otherwise it returns the intended value generated from the value passed in. This is the simplest fix I came up with; however, it would be good to see if this is sensible enough from other users (and appropriate changes made).
In the new function I have created, I have specified a return type of 'Minutes'. This will only work in PHP 7+ (http://php.net/manual/en/functions.returning-values.php). If you're using an earlier suitable version of PHP, just remove this return type specified (and colon).
If my modification is sensible/good enough, let me know and I'll submit a pull request :).
Hey there,
First of all, I'm glad I have come across your package! I was going to do this from scratch, but figured there's no point in re-inventing the wheel (especially when you want to get a project finished sooner rather than later)...
To get right to the point...
I visit my project's URL on my Vagrant testing server @ http://freebtc.website.new.localhost:8080/stats/errors (your equivalent being stats/?page=errors), and I see information as below:
Ironically, you will see the first two error messages are ones I'm submitting this issue for :).
I select '7 days' as my chosen timeframe, which works as expected:
Being the annoying person I am, I enter a back-tick as a value for 'days'. This is what produces the error I've submitted the issue for:
The full stack trace is also below:
The issue seems very straight-forward to solve :). Line 19 of your Session class (vendor/pragmarx/tracker/src/Vendor/Laravel/Support/Session.php) contains the following:
$this->minutes = new Minutes(60 * 24 * LaravelSession::get('tracker.stats.days'));
A quick sanity check on the 'LaravelSession::get('tracker.stats.days')' value could fix this... Would performing an 'is_int' check be sufficient, considering any other logic which could contribute to the value being obtained? I will test that theory out, and get back to you on my findings...