/**
  * @param int                     $campaignId
  * @param CampaignEventRepository $campaignEventRepository
  * @param CampaignRepository      $campaignRepository
  * @return \Response
  */
 public function show($campaignId, CampaignEventRepository $campaignEventRepository, CampaignRepository $campaignRepository)
 {
     $campaign = $campaignRepository->find($campaignId);
     if (!$campaign) {
         return $this->failure('Invalid Campaign ID');
     }
     // possible events to pass
     $availableEvents = ['clicked', 'opened', 'complained', 'bounced', 'dropped'];
     $event = Request::get('event');
     if ($event && !in_array($event, $availableEvents)) {
         $this->failure('Invalid parameter: event. Options are ' . implode(" | ", $availableEvents));
     }
     $offset = Request::get('offset', 0);
     $limit = Request::get('limit', 100);
     $params['id'] = $campaign->remoteCampaignId();
     $params['event'] = $event;
     $params['recipient'] = Request::get('recipient', null);
     $params['count'] = Request::get('count', false);
     $campaignEventRepository->setDomain($campaign->getFromEmailDomain());
     $campaignStats = $campaignEventRepository->search($params, $limit, $offset);
     $final = [];
     foreach ($campaignStats as $entry) {
         $final[] = $entry->toArray();
     }
     return $this->success($final);
 }
 /**
  * @param Campaign $campaign
  * @param Mailgun  $mg
  * @param string   $domain
  * @param          $campaignRepository
  * @return string
  */
 private function setRemoteCampaignId(Campaign $campaign, MailGun $mg, $domain, CampaignRepository $campaignRepository)
 {
     $params['name'] = $campaign->campaignName();
     $result = $mg->post($domain . '/campaigns', $params);
     $campaign->setRemoteCampaignId($result->http_response_body->campaign->id);
     $campaignRepository->save($campaign);
     return $campaign;
 }
 /**
  * @param WebHookReceiver    $webHookReceiver
  * @param WebHookRelay       $webHookRelay
  * @param CampaignRepository $campaignRepository
  * @param ClientRepository   $clientRepository
  * @param HttpRequest        $httpRequest
  */
 public function index(WebHookReceiver $webHookReceiver, WebHookRelay $webHookRelay, CampaignRepository $campaignRepository, ClientRepository $clientRepository, HttpRequest $httpRequest)
 {
     $vars = $webHookReceiver->receiveHook(Request::all());
     Log::info('Web Hook Received : ' . print_r($vars, true));
     $campaign = $campaignRepository->findByDomain($vars['domain']);
     $client = $clientRepository->find($campaign->clientId());
     $webHookRelay->relay($vars, $client, $httpRequest);
 }
 /**
  * @param int                     $campaignId
  * @param CampaignStatsRepository $campaignStatsRepository
  * @param CampaignRepository      $campaignRepository
  * @return \Response
  */
 public function show($campaignId, CampaignStatsRepository $campaignStatsRepository, CampaignRepository $campaignRepository)
 {
     $campaign = $campaignRepository->find($campaignId);
     if (!$campaign) {
         $this->failure('Invalid Campaign ID');
     }
     if ($campaign->remoteCampaignId()) {
         $campaignStatsRepository->setDomain($campaign->getFromEmailDomain());
         $campaignStats = $campaignStatsRepository->find($campaign->remoteCampaignId());
         return $this->success($campaignStats->stats());
     } else {
         return $this->success(['delivered' => 0, 'dropped' => 0, 'opened' => 0, 'clicked' => 0, 'complained' => 0, 'bounced' => 0, 'unsubscribed' => 0]);
     }
 }
 /**
  * @test
  */
 function testShow()
 {
     $campaignData = ['id' => 50, 'domain' => 'gmail.com', 'fromEmail' => new Email('*****@*****.**'), 'remoteCampaignId' => 'hyght'];
     $this->campaignRepository = Mockery::mock('App\\AmbitiousMailSender\\Campaigns\\CampaignRepository', 'CampaignRepository');
     $this->campaignRepository->shouldReceive('find')->once()->andReturn(new \App\AmbitiousMailSender\Campaigns\Campaign($campaignData));
     $this->app->instance('App\\AmbitiousMailSender\\Campaigns\\CampaignRepository', $this->campaignRepository);
     $campaignStatsData = ['id' => 1, 'submitted' => 100, 'delivered' => 60, 'dropped' => 10, 'bounced' => 5, 'clicked' => 60, 'opened' => 55, 'complained' => 0, 'unsubscribed' => 0];
     $this->campaignStatsRepository = Mockery::mock('App\\AmbitiousMailSender\\CampaignStats\\CampaignStatsRepository', 'CampaignStatsRepository');
     $this->campaignStatsRepository->shouldReceive('setDomain')->with('gmail.com');
     $this->campaignStatsRepository->shouldReceive('find')->with('hyght')->once()->andReturn(new \App\AmbitiousMailSender\CampaignStats\CampaignStats($campaignStatsData));
     $this->app->instance('App\\AmbitiousMailSender\\CampaignStats\\CampaignStatsRepository', $this->campaignStatsRepository);
     $this->action('GET', 'ApiV1\\CampaignController@show', ['campaignId' => 50], [], [], [], ['PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'secret']);
     unset($campaignStatsData['id']);
     $campaignStatsData['delivering'] = 30;
     $this->assertResponseOk();
     $this->assertViewHas('apiResponse', ['success' => 1, 'response' => $campaignStatsData, '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);*/
 }
 function testShow()
 {
     $campaignData = ['id' => 50, 'domain' => 'gmail.com', 'fromEmail' => new Email('*****@*****.**'), 'remoteCampaignId' => 'hyght'];
     $this->campaignRepository = Mockery::mock('App\\AmbitiousMailSender\\Campaigns\\CampaignRepository', 'CampaignRepository');
     $this->app->instance('App\\AmbitiousMailSender\\Campaigns\\CampaignRepository', $this->campaignRepository);
     $this->campaignRepository->shouldReceive('find')->with(50)->once()->andReturn(new \App\AmbitiousMailSender\Campaigns\Campaign($campaignData));
     $this->campaignEventRepository = Mockery::mock('App\\AmbitiousMailSender\\CampaignEvents\\CampaignEventRepository', 'CampaignEventRepository');
     $this->app->instance('App\\AmbitiousMailSender\\CampaignEvents\\CampaignEventRepository', $this->campaignEventRepository);
     $this->campaignEventRepository->shouldReceive('setDomain')->with('gmail.com');
     // these are the parameters that we expect to get passed to the mocked repository
     $params = ['id' => 'hyght', 'event' => 'clicked', 'recipient' => null, 'count' => null];
     // create three events, this is what the mocked repo will return
     $event1Data = ['id' => 1, 'event' => 'clicked', 'recipient' => new Email('*****@*****.**')];
     $event2Data = ['id' => 2, 'event' => 'clicked', 'recipient' => new Email('*****@*****.**')];
     $event3Data = ['id' => 3, 'event' => 'clicked', 'recipient' => new Email('*****@*****.**')];
     $this->campaignEventRepository->shouldReceive('search')->with($params, 100, 0)->andReturn(Collection::make([new CampaignEvent($event1Data), new CampaignEvent($event2Data), new CampaignEvent($event3Data)]));
     // data that we're passing in for the request
     $urlData = ['campaignId' => 50, 'offset' => 0, 'limit' => 100, 'event' => 'clicked'];
     $this->action('GET', 'ApiV1\\CampaignEventsController@show', $urlData, [], [], [], ['PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'secret']);
     $this->assertResponseOk();
     // this is the final array that should be produced
     $campaignEventData = [['id' => 1, 'tags' => null, 'event' => 'clicked', 'recipient' => '*****@*****.**', 'created_at' => null, 'updated_at' => null], ['id' => 2, 'tags' => null, 'event' => 'clicked', 'recipient' => '*****@*****.**', 'created_at' => null, 'updated_at' => null], ['id' => 3, 'tags' => null, 'event' => 'clicked', 'recipient' => '*****@*****.**', 'created_at' => null, 'updated_at' => null]];
     $this->assertViewHas('apiResponse', ['success' => 1, 'response' => $campaignEventData, 'message' => '']);
 }
 /**
  * @param CampaignRepository $campaignRepository
  */
 public function index(CampaignRepository $campaignRepository)
 {
     $campaign = $this->campaignRepository->find(1);
 }