/** * @MethodInterceptor(class-expression=.*Service.*,expression=.*) */ public function manageTransaction(MethodInvocation $methodInvocation) { $method = $methodInvocation->getMethod(); $class = $methodInvocation->getClass(); if (strncmp($method, '_', 1) === 0 || strncmp($method, 'set', 3) === 0) { return $methodInvocation->proceed(); } $this->logger->debug("Serving for: {$class}::{$method}"); self::$count++; try { if (self::$count === 1) { $this->logger->debug('Beginning transaction'); $this->entityManager->beginTransaction(); } $result = $methodInvocation->proceed(); if (self::$count === 1) { $this->logger->debug('Commiting transaction'); $this->entityManager->flush(); $this->entityManager->commit(); } self::$count--; return $result; } catch (\Exception $exception) { if (self::$count === 1) { $this->logger->error('Exception: ' . $exception->getMessage() . ' occurred, rollbacking transaction'); $this->entityManager->rollback(); } self::$count--; throw $exception; } }
/** * The proxy will call this method when an aspected method is called. * * @param MethodInvocation $invocation Method invocation "descriptor". * * @return void */ public function invoke(MethodInvocation $invocation) { $interceptors = $this->getInterceptors($invocation->getMethod()); if ($interceptors != false) { return $this->_callInterceptors($invocation, $interceptors); } return $invocation->proceed(); }