| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-02-05 12:08:25 +01:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  | use Auth; | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  | use Cache; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Collection as EloquentCollection; | 
					
						
							|  |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2015-06-02 17:58:30 +02:00
										 |  |  | use Preferences as Prefs; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-06-07 10:28:26 +02:00
										 |  |  |  * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |  * @package FireflyIII\Support | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-06-03 18:22:47 +02:00
										 |  |  | class CacheProperties | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  |     /** @var  string */ | 
					
						
							|  |  |  |     protected $md5 = ''; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |     /** @var Collection */ | 
					
						
							|  |  |  |     protected $properties; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->properties = new Collection; | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  |         $this->addProperty(Auth::user()->id); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:58:30 +02:00
										 |  |  |         $this->addProperty(Prefs::lastActivity()); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param $property | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function addProperty($property) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->properties->push($property); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function get() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Cache::get($this->md5); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  |     public function getMd5() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->md5; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function has() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-06-06 23:09:12 +02:00
										 |  |  |         if (getenv('APP_ENV') == 'testing') { | 
					
						
							| 
									
										
										
										
											2015-06-06 15:36:12 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  |         $this->md5(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Cache::has($this->md5); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $data | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function store($data) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Cache::forever($this->md5, $data); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function md5() | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         foreach ($this->properties as $property) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($property instanceof Collection || $property instanceof EloquentCollection) { | 
					
						
							| 
									
										
										
										
											2015-06-05 13:39:24 +02:00
										 |  |  |                 $this->md5 .= json_encode($property->toArray()); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ($property instanceof Carbon) { | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  |                 $this->md5 .= $property->toRfc3339String(); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (is_object($property)) { | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  |                 $this->md5 .= $property->__toString(); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-06-05 13:39:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $this->md5 .= json_encode($property); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 21:15:52 +02:00
										 |  |  |         $this->md5 = md5($this->md5); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:41:18 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-05 13:39:24 +02:00
										 |  |  | } |