rbrecruiter/app/Services/ContactService.php
Miguel N 8942623bde
Added services
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.
2021-07-25 22:54:15 +01:00

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,
])));
}
}
}
}