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

98 lines
2.4 KiB
PHP
Raw Normal View History

2015-06-01 18:41:18 +02:00
<?php
2022-12-29 19:42:26 +01:00
/**
* CacheProperties.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02: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/>.
*/
declare(strict_types=1);
2015-06-01 18:41:18 +02:00
namespace FireflyIII\Support;
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
{
2021-04-06 13:30:09 +02:00
protected string $hash = '';
protected Collection $properties;
2015-06-01 18:41:18 +02:00
public function __construct()
{
2022-10-30 14:24:37 +01:00
$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
}
/**
2023-06-21 12:34:58 +02:00
* @param mixed $property
2015-06-01 18:41:18 +02:00
*/
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()
{
2023-12-20 19:35:52 +01:00
return \Cache::get($this->hash);
2015-06-03 21:15:52 +02:00
}
2015-06-01 18:41:18 +02:00
public function getHash(): string
2015-06-03 21:15:52 +02:00
{
return $this->hash;
2015-06-03 21:15:52 +02:00
}
2016-02-06 15:01:26 +01:00
public function has(): bool
2015-06-03 21:15:52 +02:00
{
if ('testing' === config('app.env')) {
2015-06-06 15:36:12 +02:00
return false;
}
$this->hash();
2015-06-03 21:15:52 +02:00
2023-12-20 19:35:52 +01:00
return \Cache::has($this->hash);
2015-06-03 21:15:52 +02:00
}
/**
2023-12-20 19:35:52 +01:00
* @param mixed $data
2015-06-03 21:15:52 +02:00
*/
2023-12-20 19:35:52 +01:00
public function store($data): void
{
\Cache::forever($this->hash, $data);
}
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) {
2021-04-06 13:30:09 +02:00
try {
2021-05-24 08:57:02 +02:00
$content .= json_encode($property, JSON_THROW_ON_ERROR);
2023-12-20 19:35:52 +01:00
} catch (\JsonException $e) {
2021-04-06 13:30:09 +02:00
// @ignoreException
2022-12-29 19:42:26 +01:00
$content .= hash('sha256', (string)time());
2021-04-06 13:30:09 +02:00
}
2015-06-01 18:41:18 +02:00
}
2020-04-11 06:42:21 +02:00
$this->hash = substr(hash('sha256', $content), 0, 16);
2015-06-01 18:41:18 +02:00
}
2015-06-05 13:39:24 +02:00
}