| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace LE_ACME2\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use LE_ACME2\Connector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use LE_ACME2\Order; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use LE_ACME2\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use LE_ACME2\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use LE_ACME2\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use LE_ACME2\Utilities; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use LE_ACME2\SingletonTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class OrderResponse extends AbstractKeyValuableCache { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     use SingletonTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private const _FILE = 'CacheResponse'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private const _DEPRECATED_FILE = 'DirectoryNewOrderResponse'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $_responses = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function exists(Order $order) : bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $cacheFile = $order->getKeyDirectoryPath() . self::_FILE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $deprecatedCacheFile = $order->getKeyDirectoryPath() . self::_DEPRECATED_FILE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         return file_exists($cacheFile) || file_exists($deprecatedCacheFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param Order $order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return Response\Order\AbstractOrder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @throws Exception\InvalidResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @throws Exception\RateLimitReached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function get(Order $order): Response\Order\AbstractOrder { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $accountIdentifier = $this->_getObjectIdentifier($order->getAccount()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $orderIdentifier = $this->_getObjectIdentifier($order); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if(!isset($this->_responses[$accountIdentifier])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $this->_responses[$accountIdentifier] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if(array_key_exists($orderIdentifier, $this->_responses[$accountIdentifier])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             return $this->_responses[ $accountIdentifier ][ $orderIdentifier ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->_responses[ $accountIdentifier ][ $orderIdentifier ] = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $cacheFile = $order->getKeyDirectoryPath() . self::_FILE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $deprecatedCacheFile = $order->getKeyDirectoryPath() . self::_DEPRECATED_FILE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if(file_exists($deprecatedCacheFile) && !file_exists($cacheFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             rename($deprecatedCacheFile, $cacheFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if(file_exists($cacheFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $rawResponse = Connector\RawResponse::getFromString(file_get_contents($cacheFile)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $response = new Response\Order\Create($rawResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             if( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $response->getStatus() != Response\Order\AbstractOrder::STATUS_VALID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 Utilities\Logger::getInstance()->add( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     Utilities\Logger::LEVEL_DEBUG, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     get_class() . '::' . __FUNCTION__ . ' (cache did not satisfy, status "' . $response->getStatus() . '")' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $request = new Request\Order\Get($order, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $response = $request->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $this->set($order, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             Utilities\Logger::getInstance()->add( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 Utilities\Logger::LEVEL_DEBUG, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 get_class() . '::' . __FUNCTION__ .  ' (from cache, status "' . $response->getStatus() . '")' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->_responses[$accountIdentifier][$orderIdentifier] = $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         throw new \RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             self::_FILE . ' could not be found for order: ' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             '- Path: ' . $order->getKeyDirectoryPath() . PHP_EOL . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             '- Subjects: ' . var_export($order->getSubjects(), true) . PHP_EOL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     public function set(Order $order, Response\Order\AbstractOrder $response = null) : void { | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         $accountIdentifier = $this->_getObjectIdentifier($order->getAccount()); | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         $orderIdentifier = $this->_getObjectIdentifier($order); | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         $filePath = $order->getKeyDirectoryPath() . self::_FILE; | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         if($response === null) { | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |             unset($this->_responses[$accountIdentifier][$orderIdentifier]); | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |             if(file_exists($filePath)) { | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |                 unlink($filePath); | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |             return; | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |         $this->_responses[$accountIdentifier][$orderIdentifier] = $response; | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |         file_put_contents($filePath, $response->getRaw()->toString()); | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  | } |