Skip to content

Commit

Permalink
Merge pull request #1847 from ushahidi/lumen-mailer
Browse files Browse the repository at this point in the history
Replacing Ushahidi_Mailer with a Lumen implementation
  • Loading branch information
rjmackay authored Jul 5, 2017
2 parents b5b16aa + 3ad25f8 commit 924c1b7
Show file tree
Hide file tree
Showing 14 changed files with 724 additions and 139 deletions.
7 changes: 7 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function register()
$this->app->configure('cdn');
$this->app->configure('ratelimiter');
$this->app->configure('multisite');
$this->app->configure('mail');

$this->configureAuraDI();
}
Expand Down Expand Up @@ -126,6 +127,12 @@ protected function configureAuraDI()
return '';
});

// Configure mailer
$di->set('tool.mailer', $di->lazyNew('Ushahidi\App\Tools\LumenMailer', [
'mailer' => app('mailer'),
'siteConfig' => $di->lazyGet('site.config'),
'clientUrl' => $di->lazyGet('clienturl')
]));

// @todo move to auth provider?
$di->set('session.user', function () use ($di) {
Expand Down
77 changes: 77 additions & 0 deletions app/Tools/LumenMailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* Ushahidi Mailer
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

namespace Ushahidi\App\Tools;

use Ushahidi\Core\Tool\Mailer as MailerContract;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Support\Str;

class LumenMailer implements MailerContract
{
public function __construct(Mailer $mailer, $siteConfig, $clientUrl)
{
$this->mailer = $mailer;
$this->siteConfig = $siteConfig;
$this->clientUrl = $clientUrl;
}

public function send($to, $type, array $params = null)
{
// Only available type right now is 'resetpassword'
$method = "send" . Str::ucfirst($type);
if (method_exists($this, $method)) {
$this->$method($to, $params);
} else {
// Exception
throw new Exception('Unsupported mail type: ' + $type);
}
}

protected function sendResetpassword($to, $params)
{
$site_name = $this->siteConfig['name'];
$site_email = $this->siteConfig['email'];
$multisite_email = config('multisite.email');

// @todo make this more robust
if ($multisite_email) {
$from_email = $multisite_email;
} elseif ($site_email) {
$from_email = $site_email;
} else {
$from_email = false;
// Get host from lumen
// $host = app()->make('request')->getHost();
// $from_email = 'noreply@' . $host;
}

$data = [
'site_name' => $site_name,
'token' => $params['token'],
'client_url' => $this->clientUrl
];

$subject = $site_name . ': Password reset';

$this->mailer->send(
'emails/forgot-password',
$data,
function ($message) use ($to, $subject, $from_email, $site_name) {
$message->to($to);
$message->subject($subject);
if ($from_email) {
$message->from($from_email, $site_name);
}
}
);
}
}
5 changes: 0 additions & 5 deletions application/classes/Ushahidi/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ public static function init()
return getenv('INTERCOM_APP_TOKEN');
});

$di->set('tool.mailer', $di->lazyNew('Ushahidi_Mailer', [
'siteConfig' => $di->lazyGet('site.config'),
'clientUrl' => $di->lazyGet('clienturl')
]));

/**
* 1. Load the plugins
*/
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}

// Initialize the Kohana application
require __DIR__ . '/../application/kohana.php';
require_once __DIR__ . '/../application/kohana.php';

$app = require __DIR__.'/lumen.php';

Expand Down
1 change: 1 addition & 0 deletions bootstrap/lumen.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
|
*/

$app->register(Illuminate\Mail\MailServiceProvider::class);
$app->register(Ushahidi\App\Providers\AppServiceProvider::class);
$app->register(Ushahidi\App\Providers\AuthServiceProvider::class);
// $app->register(Ushahidi\App\Providers\EventServiceProvider::class);
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"kohana/minion" : "3.3.*@dev",
"ohanzee/database": "dev-namespaces",
"robmorgan/phinx": "~0.8.0",
"ushahidi/shadowhand-email": "dev-master",
"symm/gisconverter": "~1.0.5",
"twilio/sdk": "3.12.*",
"vlucas/phpdotenv": "~2.2",
Expand All @@ -53,7 +52,9 @@
"sentry/sentry": "~1.5",
"dusterio/lumen-passport": "^0.1.9",
"barryvdh/laravel-cors": "^0.9.2",
"ushahidi/kohana-validation": "dev-master"
"ushahidi/kohana-validation": "dev-master",
"illuminate/mail": "5.4.*",
"mockery/mockery": "^0.9.9"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down Expand Up @@ -102,7 +103,7 @@
"@migrate"
],
"lint" : [
"phpcs --ignore=vendor/*,application/*,modules/*,plugins/*,httpdocs/*,tests/spec/*,migrations/*,bin/*,database/* --standard=src/ruleset.xml --tab-width=4 ./",
"phpcs --ignore=vendor/*,application/*,modules/*,plugins/*,httpdocs/*,tests/spec/*,migrations/*,bin/*,database/*,resources/views/* --standard=src/ruleset.xml --tab-width=4 ./",
"phpcs --standard=tests/spec/ruleset.xml --tab-width=4 ./tests/spec/",
"phpcs --standard=migrations/ruleset.xml --tab-width=4 ./migrations/ ./database"
],
Expand Down
Loading

0 comments on commit 924c1b7

Please sign in to comment.