| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  *  This file is part of the Simple S3 package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * (c) Mauro Cassani<https://github.com/mauretto78> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Matecat\SimpleS3\Commands\Handlers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Aws\ResultInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Matecat\SimpleS3\Commands\CommandHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class RestoreItem extends CommandHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Send a basic restore request for an archived copy of an object back into Amazon S3. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * For a complete reference: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * https://docs.aws.amazon.com/cli/latest/reference/s3api/restore-object.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 1 |  |     public function handle(array $params = []): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 1 |  |         $bucketName = $params[ 'bucket' ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 1 |  |         $keyName    = $params[ 'key' ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 1 |  |         $days       = (isset($params[ 'days' ])) ? $params[ 'days' ] : 5; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 1 |  |         $tier       = (isset($params[ 'tier' ])) ? $params[ 'tier' ] : 'Expedited'; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |         if ($this->client->hasEncoder()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 1 |  |             $keyName = $this->client->getEncoder()->encode($keyName); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  |         $allowedTiers = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |                 'Bulk', | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |                 'Expedited', | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |                 'Standard', | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 1 |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |         if ($tier and !in_array($tier, $allowedTiers)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             throw new InvalidArgumentException(sprintf('%s is not a valid tier value. Allowed values are: [' . implode(',', $allowedTiers) . ']', $tier)); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 1 |  |             $request = $this->client->getConn()->restoreObject([ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |                     'Bucket'         => $bucketName, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 1 |  |                     'Key'            => $keyName, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 1 |  |                     'RestoreRequest' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  |                             'Days'                 => $days, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 1 |  |                             'GlacierJobParameters' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 1 |  |                                     'Tier' => $tier, | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 1 |  |                             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 1 |  |             ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 1 |  |             if (($request instanceof ResultInterface) and $request[ '@metadata' ][ 'statusCode' ] === 202) { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 1 |  |                 $this->commandHandlerLogger?->log($this, sprintf('A request for restore \'%s\' item in \'%s\' bucket was successfully sended', $keyName, $bucketName)); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 1 |  |                 if ($this->client->hasCache()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |                     $this->client->getCache()->set($bucketName, $keyName, ''); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  |                 return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             $this->commandHandlerLogger?->log($this, sprintf('Something went wrong during sending restore questo for \'%s\' item in \'%s\' bucket', $keyName, $bucketName), 'warning'); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         } catch (Exception $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             $this->commandHandlerLogger?->logExceptionAndReturnFalse($e); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |     public function validateParams(array $params = []): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         return ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |                 isset($params[ 'bucket' ]) and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |                 isset($params[ 'key' ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  |