/**
  * @test
  */
 function testStoreWithInvalidEmails()
 {
     // Mock CampaignEmailRepository
     $this->campaignEmailRepository = Mockery::mock('App\\AmbitiousMailSender\\CampaignEmails\\CampaignEmailRepository', 'campaignEmailRepository');
     $this->app->instance('App\\AmbitiousMailSender\\CampaignEmails\\CampaignEmailRepository', $this->campaignEmailRepository);
     $this->campaignEmailRepository->shouldReceive('save')->times(2);
     // Mock Queue
     $this->queue = Mockery::mock('App\\AmbitiousMailSender\\Base\\Services\\Queue\\Queue', 'queue');
     $this->app->instance('App\\AmbitiousMailSender\\Base\\Services\\Queue\\Queue', $this->queue);
     $this->queue->shouldReceive('start')->once();
     $this->queue->shouldReceive('produce')->once();
     // expects a list of emails in the form of a json array, we'll pass through 3 for testing
     $emails = [['email_address' => '*****@*****.**', 'variables' => ''], ['email_address' => '*****@*****.**', 'variables' => ''], ['email_address' => 'test3gmail.com', 'variables' => '']];
     $postData = ['campaignId' => 50, 'emails' => json_encode($emails)];
     $this->action('POST', 'ApiV1\\EmailsController@store', $postData, [], [], [], ['PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'secret']);
     $this->assertResponseOk();
     $this->assertViewHas('apiResponse', ['success' => 1, 'response' => ['received' => 2], 'message' => '']);
 }
 /**
  * @param HttpRequest             $httpRequest
  * @param CampaignRepository      $campaignRepository
  * @param CampaignEmailRepository $campaignEmailRepository
  * @param MailTransport           $mailTransport
  */
 function index(HttpRequest $httpRequest, CampaignRepository $campaignRepository, CampaignEmailRepository $campaignEmailRepository, MailTransport $mailTransport)
 {
     $message = Request::input('message');
     $msg = json_decode($message);
     if (!$message) {
         // this is for debugging
         //{"campaignId":"5","emailsToSend":5}
         $campaignId = 5;
         $emailsToSend = 5;
     } else {
         $campaignId = $msg->campaignId;
         $emailsToSend = $msg->emailsToSend;
     }
     Log::info('Email Sending Queue Consumer - Campaign: ' . $campaignId);
     $done = 0;
     $failed = 0;
     if ($campaign = $campaignRepository->find($campaignId)) {
         $searchParams = ['campaign_id' => $campaignId, 'failed' => 0];
         $emails = $campaignEmailRepository->search($searchParams, $emailsToSend);
         foreach ($emails as $email) {
             $success = $mailTransport->send($campaign, $email, $campaignRepository);
             if ($success) {
                 $done++;
                 $campaignEmailRepository->destroy($email->id());
             } else {
                 $failed++;
                 $campaignEmailRepository->save($email->fail());
             }
         }
     }
     Log::info('Sent/Failed : ' . $done . '/' . $failed);
     // create a new consumer to replace the one we just used
     /*$requestData = [
     			'url'        => Route('queueConsumerEmailSend'),
     			'queue_name' => 'AmbitiousMailSenderEmailSend'
     		];
     		$httpRequest->post(Route('queueConsumerSetup'), $requestData, 1, true, false);*/
 }
 public function store(CampaignEmailFactory $campaignEmailFactory, CampaignEmailRepository $campaignEmailRepository, Queue $queue, HttpRequest $httpRequest)
 {
     $emails = json_decode(Request::input('emails'), true);
     $processed = 0;
     $campaignId = Request::input('campaign_id');
     foreach ($emails as $email) {
         try {
             $campaignEmail = $campaignEmailFactory->create(['campaignId' => $campaignId, 'emailAddress' => new Email($email['email_address']), 'variables' => $email['variables']]);
         } catch (InvalidArgumentException $e) {
             // if the email address is invalid, then
             //    we can just skip it
             continue;
         }
         $campaignEmailRepository->save($campaignEmail);
         $processed++;
     }
     if ($processed > 0) {
         // add this campaign to the queue for sending
         $message = json_encode(['campaignId' => $campaignId, 'emailsToSend' => $processed]);
         $queue->start('AmbitiousMailSenderEmailSend', $httpRequest);
         $queue->produce($message);
     }
     return $this->success(['received' => $processed]);
 }