| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * CacheProperties.php | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											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/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											2019-07-31 16:53:09 +02:00
										 |  |  |  * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							| 
									
										
										
										
											2017-11-04 07:10:21 +01:00
										 |  |  |     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()); | 
					
						
							| 
									
										
										
										
											2016-03-03 08:31:18 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-11-04 07:10:21 +01:00
										 |  |  |         return Cache::get($this->hash); | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-11-04 07:10:21 +01:00
										 |  |  |     public function getHash(): string | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-11-04 07:10:21 +01: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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-15 07:59:02 +01:00
										 |  |  |         if ('testing' === config('app.env')) { | 
					
						
							| 
									
										
										
										
											2015-06-06 15:36:12 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-04 07:10:21 +01:00
										 |  |  |         $this->hash(); | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 07:10:21 +01: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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-11-04 07:10:21 +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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-11-04 07:10:21 +01:00
										 |  |  |         $content = ''; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |         foreach ($this->properties as $property) { | 
					
						
							| 
									
										
										
										
											2017-11-04 07:10:21 +01:00
										 |  |  |             $content .= json_encode($property); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-04 07:10:21 +01:00
										 |  |  |         $this->hash = substr(sha1($content), 0, 16); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-05 13:39:24 +02:00
										 |  |  | } |