2015-06-01 11:41:18 -05:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-06-01 11:41:18 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
|
|
|
|
2015-06-02 10:44:50 -05:00
|
|
|
use Auth;
|
2015-06-03 14:15:52 -05:00
|
|
|
use Cache;
|
2015-06-01 11:41:18 -05:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
|
|
|
use Illuminate\Support\Collection;
|
2015-06-02 10:58:30 -05:00
|
|
|
use Preferences as Prefs;
|
2015-06-01 11:41:18 -05:00
|
|
|
|
|
|
|
/**
|
2015-06-03 11:22:47 -05:00
|
|
|
* Class CacheProperties
|
2015-06-01 11:41:18 -05:00
|
|
|
*
|
2015-06-07 03:28:26 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-06-01 11:41:18 -05:00
|
|
|
* @package FireflyIII\Support
|
|
|
|
*/
|
2015-06-03 11:22:47 -05:00
|
|
|
class CacheProperties
|
2015-06-01 11:41:18 -05:00
|
|
|
{
|
|
|
|
|
2015-06-03 14:15:52 -05:00
|
|
|
/** @var string */
|
|
|
|
protected $md5 = '';
|
2015-06-01 11:41:18 -05:00
|
|
|
/** @var Collection */
|
|
|
|
protected $properties;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->properties = new Collection;
|
2015-06-02 10:44:50 -05:00
|
|
|
$this->addProperty(Auth::user()->id);
|
2015-06-02 10:58:30 -05:00
|
|
|
$this->addProperty(Prefs::lastActivity());
|
2015-06-01 11:41:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $property
|
|
|
|
*/
|
|
|
|
public function addProperty($property)
|
|
|
|
{
|
|
|
|
$this->properties->push($property);
|
|
|
|
}
|
|
|
|
|
2015-06-03 14:15:52 -05:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
return Cache::get($this->md5);
|
|
|
|
}
|
2015-06-01 11:41:18 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-06 08:01:26 -06:00
|
|
|
public function getMd5(): string
|
2015-06-03 14:15:52 -05:00
|
|
|
{
|
|
|
|
return $this->md5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-06 08:01:26 -06:00
|
|
|
public function has(): bool
|
2015-06-03 14:15:52 -05:00
|
|
|
{
|
2015-06-06 16:09:12 -05:00
|
|
|
if (getenv('APP_ENV') == 'testing') {
|
2015-06-06 08:36:12 -05:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-03 14:15:52 -05:00
|
|
|
$this->md5();
|
|
|
|
|
|
|
|
return Cache::has($this->md5);
|
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
*/
|
|
|
|
public function store($data)
|
|
|
|
{
|
|
|
|
Cache::forever($this->md5, $data);
|
|
|
|
}
|
|
|
|
|
2015-06-03 14:15:52 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function md5()
|
2015-06-01 11:41:18 -05:00
|
|
|
{
|
|
|
|
foreach ($this->properties as $property) {
|
|
|
|
|
|
|
|
if ($property instanceof Collection || $property instanceof EloquentCollection) {
|
2015-06-05 06:39:24 -05:00
|
|
|
$this->md5 .= json_encode($property->toArray());
|
2015-06-01 11:41:18 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($property instanceof Carbon) {
|
2015-06-03 14:15:52 -05:00
|
|
|
$this->md5 .= $property->toRfc3339String();
|
2015-06-01 11:41:18 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is_object($property)) {
|
2015-06-03 14:15:52 -05:00
|
|
|
$this->md5 .= $property->__toString();
|
2015-06-01 11:41:18 -05:00
|
|
|
}
|
2015-06-05 06:39:24 -05:00
|
|
|
|
|
|
|
$this->md5 .= json_encode($property);
|
2015-06-01 11:41:18 -05:00
|
|
|
}
|
|
|
|
|
2015-06-03 14:15:52 -05:00
|
|
|
$this->md5 = md5($this->md5);
|
2015-06-01 11:41:18 -05:00
|
|
|
}
|
2015-06-05 06:39:24 -05:00
|
|
|
}
|