| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Puzzle\AMQP\Workers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Puzzle\AMQP\ReadableMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Puzzle\AMQP\Messages\BodyFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Puzzle\AMQP\WritableMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class MessageAdapter implements ReadableMessage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         $message, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         $body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 21 |  |     public function __construct(\Swarrot\Broker\Message $message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 21 |  |         $this->message = $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 21 |  |         $this->body = (new BodyFactory())->create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 21 |  |             $this->getContentType(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 20 |  |             $message->getBody() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 20 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 20 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 7 |  |     public function getRoutingKey() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 7 |  |         return $this->getAttribute('routing_key'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 21 |  |     public function getContentType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 21 |  |         return $this->getAttribute('content_type'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |     public function getAppId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |         return $this->getAttribute('app_id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 16 |  |     public function getHeaders() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 16 |  |         return $this->getAttribute('headers'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |     public function getBodyInOriginalFormat() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         return $this->body->inOriginalFormat(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 21 |  |     public function getAttribute($attributeName) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 21 |  |         $messageProperties = $this->message->getProperties(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 21 |  |         if(array_key_exists($attributeName, $messageProperties)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 21 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 20 |  |             return $messageProperties[$attributeName]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |         throw new \InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |     public function __toString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |         return json_encode(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |             'routing_key' => $this->getRoutingKey(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |             'body' => (string) $this->body, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  |             'attributes' => $this->message->getProperties(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 14 |  |     private function getHeader($headerName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 14 |  |         $headers = $this->getHeaders(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 14 |  |         if(array_key_exists($headerName, $headers)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 14 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 12 |  |             return $headers[$headerName]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |     public function getAttributes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |         return $this->message->getProperties(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 11 |  |     public function isLastRetry($retryOccurence = \Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_OCCURENCE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 11 |  |         $retryHeader = $this->getHeader(\Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_HEADER); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 11 |  |         return ($retryHeader !== null && (int) $retryHeader >= $retryOccurence); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  |     public function getRoutingKeyFromHeader() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 3 |  |         return $this->getHeader('routing_key'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |     public function cloneIntoWritableMessage(WritableMessage $writable, $copyRoutingKey = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 2 |  |         if($copyRoutingKey === true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |             $writable->changeRoutingKey($this->getRoutingKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |         $writable->setBody($this->body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |         $writable->addHeaders($this->getHeaders()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |         $attributes = $this->getAttributes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |         $skippedAttributes = array('timestamp', 'headers', 'app_id', 'routing_key'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |         foreach($attributes as $attributeName => $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |             if(! in_array($attributeName, $skippedAttributes)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |                 $writable->setAttribute($attributeName, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |         return $writable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                        
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.