firefly-iii/app/Support/CacheProperties.php

106 lines
2.2 KiB
PHP
Raw Normal View History

2015-06-01 11:41:18 -05:00
<?php
/**
* CacheProperties.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05: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 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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;
/**
2017-11-15 05:25:49 -06:00
* Class CacheProperties.
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
{
2017-11-15 05:25:49 -06:00
/** @var string */
protected $hash = '';
2015-06-01 11:41:18 -05:00
/** @var Collection */
protected $properties;
/**
*
*/
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());
}
2015-06-01 11:41:18 -05:00
}
/**
* @param $property
*/
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()
{
return Cache::get($this->hash);
2015-06-03 14:15:52 -05:00
}
2015-06-01 11:41:18 -05:00
/**
* @return string
*/
public function getHash(): string
2015-06-03 14:15:52 -05:00
{
return $this->hash;
2015-06-03 14:15:52 -05:00
}
/**
* @return bool
*/
2016-02-06 08:01:26 -06:00
public function has(): bool
2015-06-03 14:15:52 -05:00
{
if ('testing' === config('app.env')) {
2015-06-06 08:36:12 -05:00
return false;
}
$this->hash();
2015-06-03 14:15:52 -05:00
return Cache::has($this->hash);
2015-06-03 14:15:52 -05:00
}
2016-01-20 08:23:36 -06:00
/**
* @param $data
*/
2018-07-26 22:03:37 -05:00
public function store($data): void
2016-01-20 08:23:36 -06:00
{
Cache::forever($this->hash, $data);
2016-01-20 08:23:36 -06:00
}
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
{
$content = '';
2015-06-01 11:41:18 -05:00
foreach ($this->properties as $property) {
$content .= json_encode($property);
2015-06-01 11:41:18 -05:00
}
$this->hash = substr(sha1($content), 0, 16);
2015-06-01 11:41:18 -05:00
}
2015-06-05 06:39:24 -05:00
}