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