Skip to content

Commit

Permalink
integrate with EmailOctopus api
Browse files Browse the repository at this point in the history
  • Loading branch information
roelgonzalez committed Mar 12, 2019
1 parent b5070e7 commit ec011a1
Show file tree
Hide file tree
Showing 17 changed files with 407 additions and 88 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ BUGSNAG_API_KEY=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=
AWS_BUCKET=
AWS_BUCKET=

EMAIL_OCTOPUS_API_KEY=
EMAIL_OCTOPUS_CONTACT_LISTS_VATGOODIES_NEWSLETTER=
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- Nothing yet...

## [1.1.0](https://github.com/vatsimgoodies/vatgoodies.com/compare/v1.0.2..v1.1.0)
### Added
Integration with EmailOctopus API.

## [1.0.2](https://github.com/vatsimgoodies/vatgoodies.com/compare/v1.0.1...v1.0.2) - 2019-03-11
### Added
Dynamic discord link: https://vatgoodies.com/discord
Expand All @@ -22,4 +26,4 @@ Dynamic discord link: https://vatgoodies.com/discord
### Changed
- Back-ups are no longer including all application code files (only database)

[Unreleased]: https://github.com/vatsimgoodies/vatgoodies.com/compare/v1.0.2...HEAD
[Unreleased]: https://github.com/vatsimgoodies/vatgoodies.com/compare/v1.1.0...HEAD
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[![StyleCI](https://github.styleci.io/repos/121784641/shield?branch=develop)](https://github.styleci.io/repos/121784641)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/367a3d1bf509419aa1942d51c9fcb3c0)](https://www.codacy.com/app/roelgonzalez/vatgoodies.com?utm_source=github.com&utm_medium=referral&utm_content=vatsimgoodies/vatgoodies.com&utm_campaign=Badge_Grade)
[![StyleCI](https://github.styleci.io/repos/121784641/shield?branch=master)](https://github.styleci.io/repos/121784641)
[![Maintainability](https://api.codeclimate.com/v1/badges/437bc5f2ee5dad338cc1/maintainability)](https://codeclimate.com/github/vatsimgoodies/vatgoodies.com/maintainability)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
[![Discord](https://img.shields.io/discord/545254906257342493.svg?color=7289DA&label=Discord&style=popout)](https://discord.gg/aQkKcf5)
Expand Down
16 changes: 0 additions & 16 deletions app/Http/Controllers/SubscriptionConfirmationController.php

This file was deleted.

13 changes: 10 additions & 3 deletions app/Http/Controllers/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\SubscriptionService;
use App\Http\Requests\StoreSubscription;

class SubscriptionController extends Controller
{
public function store(StoreSubscription $request, SubscriptionService $subscriptionService)
protected $subscriptionService;

public function __construct(SubscriptionService $subscriptionService)
{
$subscriptionService->processSubscriptionRequest($request);
$this->subscriptionService = $subscriptionService;
}

public function store(Request $request, string $token)
{
$this->subscriptionService->storeSubscription($request, $token);

return redirect()->route('landing.show');
}
Expand Down
26 changes: 26 additions & 0 deletions app/Http/Controllers/SubscriptionRequestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Controllers;

use App\Services\SubscriptionService;
use App\Http\Requests\StoreSubscriptionRequest;

class SubscriptionRequestController extends Controller
{
/**
* @var SubscriptionService
*/
protected $subscriptionService;

public function __construct(SubscriptionService $subscriptionService)
{
$this->subscriptionService = $subscriptionService;
}

public function store(StoreSubscriptionRequest $request)
{
$this->subscriptionService->storeSubscriptionRequest($request);

return redirect()->route('landing.show');
}
}
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ class Kernel extends HttpKernel
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'spam-protection' => \Spatie\Honeypot\ProtectAgainstSpam::class,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Http\FormRequest;

class StoreSubscription extends FormRequest
class StoreSubscriptionRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
Expand Down
29 changes: 29 additions & 0 deletions app/Libraries/EmailOctopusApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Libraries;

use Ixudra\Curl\Facades\Curl;

class EmailOctopusApi
{
protected $baseApiUrl;

protected $apiKey;

public function __construct()
{
$this->baseApiUrl = config('emailoctopus.general.base-api-url');
$this->apiKey = config('emailoctopus.general.api-key');
}

public function createContactOfAList(string $listId, $emailAddress)
{
$url = sprintf('%s/lists/%s/contacts', $this->baseApiUrl, $listId);

return Curl::to($url)->withData([
'api_key' => $this->apiKey,
'email_address' => $emailAddress,
])->asJsonRequest()
->post();
}
}
78 changes: 72 additions & 6 deletions app/Services/SubscriptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,41 @@
namespace App\Services;

use App\Models\Subscription;
use Illuminate\Http\Request;
use App\Libraries\EmailOctopusApi;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Session;
use App\Http\Requests\StoreSubscription;
use App\Http\Requests\StoreSubscriptionRequest;
use App\Mail\ConfirmYourSubscriptionMailable;

class SubscriptionService
{
public function processSubscriptionRequest(StoreSubscription $request)
/**
* @var EmailOctopusApi
*/
protected $emailOctopusApi;

/**
* @var string
*/
protected $contactListId;

public function __construct(EmailOctopusApi $emailOctopusApi)
{
$this->emailOctopusApi = $emailOctopusApi;
$this->contactListId = config('emailoctopus.contact-lists.vatgoodies_newsletter');
}

/**
* The method that will handle the store for SubscriptionRequestController.
*
* @param StoreSubscriptionRequest $request
* @return Subscription
*/
public function storeSubscriptionRequest(StoreSubscriptionRequest $request)
{
$this->existsByEmail($request->email);

$subscription = new Subscription();
$subscription->email = $request->email;
$subscription->token = md5(now()->timestamp);
Expand All @@ -24,16 +50,56 @@ public function processSubscriptionRequest(StoreSubscription $request)
return $subscription;
}

public function processConfirmation($token)
/**
* The method that will handle the store for the SubscriptionController.
*
* @param Request $request
* @param string $token
* @return \Illuminate\Http\RedirectResponse|mixed
*/
public function storeSubscription(Request $request, string $token)
{
$subscription = Subscription::where('token', $token)->first();

$subscription = $this->getByToken($token);

if ($subscription === null) {
$request->session()->flash('failure', 'The requested e-mail could not be found, thus could not be confirmed.');
return redirect()->to('landing.show');
}

$response = json_decode($this->emailOctopusApi->createContactOfAList($this->contactListId, $subscription->email));

if (isset($response->created_at) === false) {
$request->session()->flash('failure', 'Something went wrong during your confirmation, please try again.');
return redirect()->to('landing.show');
}

$subscription->confirmed = true;

$subscription->save();

Session::flash('success', 'You have successfully confirmed your subscription to stay posted about VATGoodies.com');

return $subscription;
}

/**
* Get subscription based on token.
*
* @param string $token
* @return mixed
*/
public function getByToken(string $token)
{
return Subscription::where('token', $token)->first();
}

/**
* See if a subscription exists based on given email.
*
* @param $email
* @return mixed
*/
public function existsByEmail($email)
{
return Subscription::where('email', $email)->exists();
}
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"type": "project",
"require": {
"php": "^7.1.3",
"ext-json": "*",
"bugsnag/bugsnag-laravel": "^2.0",
"fideloper/proxy": "^4.0",
"ixudra/curl": "^6.16",
"laravel/envoy": "^1.4",
"laravel/framework": "5.6.*",
"laravel/tinker": "^1.0",
Expand Down
Loading

0 comments on commit ec011a1

Please sign in to comment.