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.
This commit is contained in:
2021-07-25 22:54:15 +01:00
parent c739933668
commit 8942623bde
44 changed files with 1308 additions and 691 deletions

View File

@@ -0,0 +1,47 @@
<?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,
])));
}
}
}
}