Miguel N
8942623bde
This commit moves most controller logic onto Services. Services are part of the Service-Repository pattern. The models act as repositories. Services are easily testable and are needed for the upcoming API, in order to avoid duplicated code and to maintain a single source of "truth". The User, Vacancy and Vote controllers still need their logic moved onto services.
48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
use App\Exceptions\FailedCaptchaException;
|
|
use App\Notifications\NewContact;
|
|
use App\User;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class ContactService
|
|
{
|
|
|
|
/**
|
|
* Sends a message to all admins.
|
|
*
|
|
* @throws FailedCaptchaException
|
|
*/
|
|
public function sendMessage($ipAddress, $message, $email, $challenge)
|
|
{
|
|
// TODO: now: add middleware for this verification, move to invisible captcha
|
|
$verifyrequest = Http::asForm()->post(config('recaptcha.verify.apiurl'), [
|
|
'secret' => config('recaptcha.keys.secret'),
|
|
'response' => $challenge,
|
|
'remoteip' => $ipAddress,
|
|
]);
|
|
|
|
$response = json_decode($verifyrequest->getBody(), true);
|
|
|
|
if (! $response['success']) {
|
|
throw new FailedCaptchaException('Beep beep boop... Robot? Submission failed.');
|
|
}
|
|
|
|
foreach (User::all() as $user) {
|
|
if ($user->hasRole('admin')) {
|
|
$user->notify(new NewContact(collect([
|
|
'message' => $message,
|
|
'ip' => $ipAddress,
|
|
'email' => $email,
|
|
])));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|