| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Marwan Al-Soltany <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Marwan Al-Soltany 2020 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * the LICENSE file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace MAKS\AmqpAgent\Worker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use MAKS\AmqpAgent\Config\AmqpAgentParameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * A trait containing the implementation of the workers command interface/functions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | trait WorkerCommandTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * The prefix that should be used to define an array as a command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public static $commandPrefix = AmqpAgentParameters::COMMAND_PREFIX; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * The recommended way of defining a command array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public static $commandSyntax = AmqpAgentParameters::COMMAND_SYNTAX; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Constructs a command from passed data to a command array following the recommended pattern. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param string $name The name of the command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string $value The value of the command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param mixed $parameters [optional] Additional parameters to add to the command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param string $argument [optional] The key to use to store the parameters under. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 7 |  |     public static function makeCommand(string $name, string $value, $parameters = null, string $argument = 'params'): array | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 7 |  |         $prefix = static::$commandPrefix; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $result = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 7 |  |             $prefix => [] | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 7 |  |         if ($name && $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 7 |  |             $result[$prefix] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 7 |  |                 $name => $value | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 7 |  |             if ($parameters) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 2 |  |                 $result[$prefix][$argument] = $parameters; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 7 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Checks whether an array is a command following the recommended pattern. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param mixed $data The data that should be checked. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 6 |  |     public static function isCommand($data): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 6 |  |         $prefix = static::$commandPrefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 6 |  |         $result = $data && is_array($data) && array_key_exists($prefix, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 6 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Checks whether a specific command (command name) exists in the command array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param array $data The array that should be checked. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param string|null $name The name of the command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param string|null $value The value of the command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |     public static function hasCommand(array $data, string $name = null, ?string $value = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  |         $prefix = static::$commandPrefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |         $result = static::isCommand($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 4 |  |         $result = ($result && $name && array_key_exists($name, $data[$prefix])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 4 |  |             ? true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 4 |  |             : $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 4 |  |         if ($result && $name && $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |             $result = isset($data[$prefix][$name]) && $data[$prefix][$name] === $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 4 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * Returns the content of a specific key in the command array, used for example to get the additional parameters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param array $data The array that should be checked. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param string $key [optional] The array key name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param string|null $sub [optional] The array nested array key name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |     public static function getCommand(array $data, string $key = 'params', ?string $sub = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |         $prefix = static::$commandPrefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |         $result = static::isCommand($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |         if ($result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |             $result = $data[$prefix]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |         if ($result && $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  |             $result = array_key_exists($key, $data[$prefix]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 2 |  |                 ? $data[$prefix][$key] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |                 : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |         if ($result && $sub) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |             $result = array_key_exists($sub, $data[$prefix][$key]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |                 ? $data[$prefix][$key][$sub] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |                 : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 2 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |