/** * @return ViewModel */ public function indexAction() { $campaign = $this->campaignRepository->getOneBySecurityKey($this->params('securityKey')); if (!$campaign) { return $this->redirect()->toRoute(static::ERROR_ROUTE); } /** @var Request $request */ $request = $this->getRequest(); $subscriber = $this->subscriberRepository->getOneByEmailAddress($request->getPost('emailAddress')); if (!$subscriber) { $subscriber = $this->subscriberRepository->getNewEntity(); } try { if ($this->manager->handlePost($subscriber, $campaign)) { if ($request->getPost('successUrl')) { return $this->redirect()->toUrl($request->getPost('successUrl')); } return $this->redirect()->toRoute($this->baseRoute, array('securityKey' => $this->params('securityKey'))); } if ($request->getPost('failureUrl')) { return $this->redirect()->toUrl($request->getPost('failureUrl')); } } catch (Manager\Exception\Db $exc) { $feedback = $this->userText->getMessage(EntityName::MESSAGE, Message::SAVE_FAILED); $this->currentMessages[FeedbackType::DANGER][] = $feedback; } $this->layout()->currentMessages = $this->currentMessages; return new ViewModel(array('form' => $this->manager->getForm(), 'campaignName' => $campaign->getName())); }
/** * @covers DmMailer\Manager\Subscribe */ public function testHandlePostQueuesSystemMessageOnSaveSuccess() { $postData = [\DmMailer\Entity\Subscriber::CAMPAIGNS => []]; $messageMock = $this->getMockBuilder('DmMailer\\Entity\\Message')->disableOriginalConstructor()->setMethods([])->getMock(); $this->messageRepoMock->expects($this->any())->method('getSystemMessage')->will($this->returnValue($messageMock)); $this->formMock->expects($this->any())->method('isValid')->will($this->returnValue(true)); $this->formMock->expects($this->any())->method('getData')->will($this->returnValue($postData)); $this->requestMock->expects($this->any())->method('isPost')->will($this->returnValue(true)); $this->requestMock->expects($this->any())->method('getPost')->will($this->returnValue([])); // Actual test $queueItemMock = $this->getMockBuilder('DmMailer\\Entity\\QueueItem')->disableOriginalConstructor()->setMethods(['setMessage', 'setSubscriber'])->getMock(); $queueItemRepoMock = $this->getMockBuilder('DmMailer\\Repository\\QueueItem')->disableOriginalConstructor()->setMethods(['getNewEntity'])->getMock(); $queueItemRepoMock->expects($this->once())->method('getNewEntity')->will($this->returnValue($queueItemMock)); $this->sut->setQueueItemRepository($queueItemRepoMock); $this->entityManagerMock->expects($this->exactly(3))->method('persist'); // Actual test ends $actualResult = $this->sut->handlePost($this->subscriberMock, $this->campaignMock); $this->assertTrue($actualResult); }
/** * @param ServiceLocatorInterface $serviceLocator * * @return SubscribeManager */ public function createService(ServiceLocatorInterface $serviceLocator) { /** @var EntityManager $entityManager */ $entityManager = $serviceLocator->get('Doctrine\\ORM\\EntityManager'); /** @var PhpRequest $request */ $request = $serviceLocator->get('request'); /** @var SubscribeForm $form */ $form = $serviceLocator->get('DmMailer\\Form\\SubscribeForm'); /** @var Paginator $paginatorFactory */ $paginatorFactory = $serviceLocator->get('DmMailer\\Service\\Factory\\Paginator'); $manager = new SubscribeManager($entityManager, $request, null, $form, $paginatorFactory); /** @var QueueItemRepository $queueItemRepository */ $queueItemRepository = $serviceLocator->get('DmMailer\\Repository\\QueueItem'); /** @var MessageRepository $messageRepository */ $messageRepository = $serviceLocator->get('DmMailer\\Repository\\Message'); $manager->setQueueItemRepository($queueItemRepository); $manager->setMessageRepository($messageRepository); return $manager; }
/** * @return JsonModel */ public function create($data = []) { $this->response->setStatusCode(400); $campaign = $this->campaignRepository->getOneBySecurityKey($this->getRequest()->getPost('securityKey')); if (!$campaign) { return $this->sendResult('Campaign not found.'); } $subscriber = $this->subscriberRepository->getOneByEmailAddress($this->getRequest()->getPost('emailAddress')); if (!$subscriber) { $subscriber = $this->subscriberRepository->getNewEntity(); } try { if ($this->manager->handlePost($subscriber, $campaign)) { $this->response->setStatusCode(201); return $this->sendResult(true); } } catch (\Exception $exc) { $this->exceptionLogger->log(0, $exc->getMessage()); $this->response->setStatusCode(500); return $this->sendResult('Unexpected error while saving data'); } return $this->sendResult($this->manager->getForm()->getMessages()); }