protected function generateTriggerEventCode($operationName, $params, $type, $paramsExist = false)
    {
        $config   = $this->getOperationConfig($operationName, 'events', array());
        $code     = '';
        $serviceId  = strtolower($this->serviceConfig['service_id']);
        $responseInjected = false;
        
        if (!empty($config)) {
            foreach ($config as $specs) {
                if ($specs['type'] === $type) {
                    
                    if (is_string($specs['args'])) {
                        $specs['args'] = array($specs['args']);
                    }
                    
                    if (!$specs['name']) {
                        $specs['name'] = $type . '.' . $serviceId . '.' . $operationName; 
                    }
                    
                    $specs['name'] = str_replace('<service>', $serviceId, strtolower($specs['name']));
                    
                    if (!$paramsExist) {
                        $code .= '$__event_params = new \ArrayObject();' . "\n";
                        
                        foreach ($params as $name) {
                            if ($specs['args'] === null || in_array($name, $specs['args'])) {
                                $code .= '$__event_params["'.$name.'"] = $'.$name.';' . "\n";
                            }
                        }
                        
                        // Overwrite with params
                        if (isset($specs['params']) && is_array($specs['params']) && !empty($specs['params'])) {
                            foreach ($specs['params'] as $name => $value) {
                                $code .= '$__event_params["'.$name.'"] = '.var_export($value, true).';' . "\n";
                            }
                        }
                        
                        $paramsExist = true;
                    }
 
                    if ($type == self::EVENT_POST && !$responseInjected) {
                        $code .= '$__event_params["__response"] = $response;' . "\n";
                        $responseInjected = true;
                    }
                    
                    $code .= '// Trigger "'.$type.'" event' . "\n";
                    $code .= 'if ($this->__commandStack->count()) {' . "\n";
                    $code .= '    $__event = new \ValuSo\Broker\ServiceEvent('.var_export($specs['name'], true).', $this->__wrappedObject, $__event_params);' . "\n";
                    $code .= '    $__event->setCommand($this->__commandStack->top());' . "\n";
                    
                    $code .= '    $this->getEventManager()->trigger($__event);' . "\n";
                    $code .= '}' . "\n";
                }
            }
            
            return $code;
        } else {
            return '';
        }
    }