| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Jsq\CacheEncryption\Iron; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Iron; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Iron\PasswordInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Jsq\CacheEncryption\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Jsq\CacheEncryption\PoolDecorator as BasePoolDecorator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Psr\Cache\CacheItemInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Psr\Cache\CacheItemPoolInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class PoolDecorator extends BasePoolDecorator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var CacheItemPoolInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $decorated; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** @var PasswordInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $password; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $cipher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 20 |  | View Code Duplication |     public function __construct( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         CacheItemPoolInterface $decorated, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $password, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $cipher = Iron\Iron::DEFAULT_ENCRYPTION_METHOD | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         if (!class_exists(Iron\Iron::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             // @codeCoverageIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             throw new \RuntimeException('You must install' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 . ' jsq/iron-php to use the Iron decorator.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             // @codeCoverageIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         parent::__construct($decorated); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->decorated = $decorated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->password = Iron\normalize_password($password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->cipher = $cipher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected function decorate(CacheItemInterface $inner) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return new ItemDecorator( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $inner, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $this->password, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $this->cipher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public function save(CacheItemInterface $item) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $this->finalizeItem($item); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         return $this->decorated->save($item->getDecorated()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function saveDeferred(CacheItemInterface $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->finalizeItem($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $this->decorated->saveDeferred($item->getDecorated()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     private function finalizeItem(CacheItemInterface $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if ($item instanceof ItemDecorator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             return $item->finalize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         throw new InvalidArgumentException('The provided cache item cannot' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             . ' be saved, as it did not originate from this cache.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  |