Files
firefly-iii/app/Support/CacheProperties.php

106 lines
2.2 KiB
PHP
Raw Normal View History

2015-06-01 18:41:18 +02:00
<?php
/**
* CacheProperties.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-06-01 18:41:18 +02:00
namespace FireflyIII\Support;
2015-06-03 21:15:52 +02:00
use Cache;
2015-06-01 18:41:18 +02:00
use Illuminate\Support\Collection;
/**
2017-11-15 12:25:49 +01:00
* Class CacheProperties.
2015-06-01 18:41:18 +02:00
*/
2015-06-03 18:22:47 +02:00
class CacheProperties
2015-06-01 18:41:18 +02:00
{
2017-11-15 12:25:49 +01:00
/** @var string */
protected $hash = '';
2015-06-01 18:41:18 +02:00
/** @var Collection */
protected $properties;
/**
*
*/
public function __construct()
{
$this->properties = new Collection;
2016-09-16 12:07:45 +02:00
if (auth()->check()) {
2016-09-16 12:15:58 +02:00
$this->addProperty(auth()->user()->id);
2018-06-10 16:59:41 +02:00
$this->addProperty(app('preferences')->lastActivity());
}
2015-06-01 18:41:18 +02:00
}
/**
* @param $property
*/
2018-07-27 05:03:37 +02:00
public function addProperty($property): void
2015-06-01 18:41:18 +02:00
{
$this->properties->push($property);
}
2015-06-03 21:15:52 +02:00
/**
* @return mixed
*/
public function get()
{
return Cache::get($this->hash);
2015-06-03 21:15:52 +02:00
}
2015-06-01 18:41:18 +02:00
/**
* @return string
*/
public function getHash(): string
2015-06-03 21:15:52 +02:00
{
return $this->hash;
2015-06-03 21:15:52 +02:00
}
/**
* @return bool
*/
2016-02-06 15:01:26 +01:00
public function has(): bool
2015-06-03 21:15:52 +02:00
{
2017-11-15 12:25:49 +01:00
if ('testing' === getenv('APP_ENV')) {
2015-06-06 15:36:12 +02:00
return false;
}
$this->hash();
2015-06-03 21:15:52 +02:00
return Cache::has($this->hash);
2015-06-03 21:15:52 +02:00
}
2016-01-20 15:23:36 +01:00
/**
* @param $data
*/
2018-07-27 05:03:37 +02:00
public function store($data): void
2016-01-20 15:23:36 +01:00
{
Cache::forever($this->hash, $data);
2016-01-20 15:23:36 +01:00
}
2015-06-03 21:15:52 +02:00
/**
*/
2018-07-27 05:03:37 +02:00
private function hash(): void
2015-06-01 18:41:18 +02:00
{
$content = '';
2015-06-01 18:41:18 +02:00
foreach ($this->properties as $property) {
$content .= json_encode($property);
2015-06-01 18:41:18 +02:00
}
$this->hash = substr(sha1($content), 0, 16);
2015-06-01 18:41:18 +02:00
}
2015-06-05 13:39:24 +02:00
}