| Conditions | 6 |
| Paths | 9 |
| Total Lines | 97 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 34 | public function run($payload) |
||
| 35 | { |
||
| 36 | // Decode payload |
||
| 37 | $this->logger->info( |
||
| 38 | 'Using payload decoder', |
||
| 39 | array('payloadDecoder' => get_class($this->payloadDecoder)) |
||
| 40 | ); |
||
| 41 | $messages = $this->payloadDecoder->decode($payload); |
||
| 42 | |||
| 43 | $this->logger->info('Decoded payload', $messages); |
||
| 44 | |||
| 45 | // Execute applicable message strategies |
||
| 46 | foreach ($messages as $message) { |
||
| 47 | $matched = false; |
||
| 48 | $language = new ExpressionLanguage(); |
||
| 49 | foreach ($this->messageStrategies as $expression => $spec) { |
||
| 50 | // Log |
||
| 51 | $this->logger->info( |
||
| 52 | 'Evaluating expression against message', |
||
| 53 | array( |
||
| 54 | 'expression' => $expression, |
||
| 55 | 'messageId' => $message->id, |
||
| 56 | ) |
||
| 57 | ); |
||
| 58 | $this->logger->debug( |
||
| 59 | 'Evaluating expression against message', |
||
| 60 | array( |
||
| 61 | 'expression' => $expression, |
||
| 62 | 'message' => $message, |
||
| 63 | 'messageId' => $message->id, |
||
| 64 | ) |
||
| 65 | ); |
||
| 66 | |||
| 67 | // Evaluate expression against message |
||
| 68 | if ($language->evaluate($expression, array('message' => $message))) { |
||
| 69 | $matched = true; |
||
| 70 | |||
| 71 | // Instanciate strategy |
||
| 72 | $strategy = $spec[0]; |
||
| 73 | $strategy->setMessage($message); |
||
| 74 | |||
| 75 | // Log |
||
| 76 | $this->logger->info( |
||
| 77 | 'Expression matched, executing related strategy', |
||
| 78 | array( |
||
| 79 | 'expression' => $expression, |
||
| 80 | 'strategy' => get_class($strategy), |
||
| 81 | 'messageId' => $message->id, |
||
| 82 | ) |
||
| 83 | ); |
||
| 84 | |||
| 85 | // Global success event |
||
| 86 | $strategy->on($strategy->success(), function (MessageEvent $event) use ($strategy) { |
||
| 87 | $this->logger->info( |
||
| 88 | 'Strategy execution succeeded', |
||
| 89 | array( |
||
| 90 | 'strategy' => get_class($strategy), |
||
| 91 | 'messageId' => $strategy->getMessage()->id, |
||
| 92 | ) |
||
| 93 | ); |
||
| 94 | $this->getEventDispatcher()->dispatch($this->success(), $event); |
||
| 95 | }); |
||
| 96 | |||
| 97 | // Global error event |
||
| 98 | $strategy->on($strategy->error(), function (ErrorEvent $event) use ($strategy) { |
||
| 99 | $this->logger->info( |
||
| 100 | 'Strategy execution failed', |
||
| 101 | array( |
||
| 102 | 'strategy' => get_class($strategy), |
||
| 103 | 'messageId' => $strategy->getMessage()->id, |
||
| 104 | 'error' => $event->getError(), |
||
| 105 | ) |
||
| 106 | ); |
||
| 107 | $this->getEventDispatcher()->dispatch($this->error(), $event); |
||
| 108 | }); |
||
| 109 | |||
| 110 | // Execute strategy |
||
| 111 | $strategy->execute(); |
||
| 112 | |||
| 113 | // Continue to search for strategies applicable to message ? |
||
| 114 | if ($spec[1] !== true) { |
||
| 115 | break; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | // Throw an error if no appropriate strategy was found |
||
| 121 | if (!$matched) { |
||
| 122 | throw new NoMessageStrategyException( |
||
| 123 | sprintf( |
||
| 124 | 'No applicable strategy found for message - %s', |
||
| 125 | json_encode(array('message' => $message), JSON_UNESCAPED_SLASHES) |
||
| 126 | ) |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 168 |