| @@ 15-43 (lines=29) @@ | ||
| 12 | ||
| 13 | use GpsLab\Component\Command\Command; |
|
| 14 | ||
| 15 | trait SwitchCommandHandlerTrait |
|
| 16 | { |
|
| 17 | /** |
|
| 18 | * @param Command $command |
|
| 19 | */ |
|
| 20 | public function handle(Command $command) |
|
| 21 | { |
|
| 22 | call_user_func([$this, $this->getHandleMethod($command)], $command); |
|
| 23 | } |
|
| 24 | ||
| 25 | /** |
|
| 26 | * @param Command $command |
|
| 27 | * |
|
| 28 | * @return string |
|
| 29 | */ |
|
| 30 | private function getHandleMethod(Command $command) |
|
| 31 | { |
|
| 32 | $class = get_class($command); |
|
| 33 | ||
| 34 | if ('Command' === substr($class, -7)) { |
|
| 35 | $class = substr($class, 0, -7); |
|
| 36 | } |
|
| 37 | ||
| 38 | $class = str_replace('_', '\\', $class); // convert names for classes not in namespace |
|
| 39 | $parts = explode('\\', $class); |
|
| 40 | ||
| 41 | return 'handle'.end($parts); |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||
| @@ 15-45 (lines=31) @@ | ||
| 12 | ||
| 13 | use GpsLab\Component\Query\Query; |
|
| 14 | ||
| 15 | trait SwitchQueryHandlerTrait |
|
| 16 | { |
|
| 17 | /** |
|
| 18 | * @param Query $query |
|
| 19 | * |
|
| 20 | * @return mixed |
|
| 21 | */ |
|
| 22 | public function handle(Query $query) |
|
| 23 | { |
|
| 24 | return call_user_func([$this, $this->getHandleMethod($query)], $query); |
|
| 25 | } |
|
| 26 | ||
| 27 | /** |
|
| 28 | * @param Query $query |
|
| 29 | * |
|
| 30 | * @return string |
|
| 31 | */ |
|
| 32 | private function getHandleMethod(Query $query) |
|
| 33 | { |
|
| 34 | $class = get_class($query); |
|
| 35 | ||
| 36 | if ('Query' === substr($class, -5)) { |
|
| 37 | $class = substr($class, 0, -5); |
|
| 38 | } |
|
| 39 | ||
| 40 | $class = str_replace('_', '\\', $class); // convert names for classes not in namespace |
|
| 41 | $parts = explode('\\', $class); |
|
| 42 | ||
| 43 | return 'handle'.end($parts); |
|
| 44 | } |
|
| 45 | } |
|
| 46 | ||