first commit
This commit is contained in:
70
app/Observers/AttachmentObserver.php
Normal file
70
app/Observers/AttachmentObserver.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Contracts\AttachmentAble;
|
||||
use App\Contracts\AttachmentService;
|
||||
use App\Helpers\AttachmentHelper;
|
||||
use App\Models\Attachment;
|
||||
use App\Models\Project;
|
||||
use App\Models\Task;
|
||||
|
||||
class AttachmentObserver
|
||||
{
|
||||
public function __construct(private readonly AttachmentService $service)
|
||||
{
|
||||
}
|
||||
|
||||
public function parentCreated(AttachmentAble $parent, $requestData): void
|
||||
{
|
||||
if (isset($requestData['attachmentsRelation']) && count($requestData['attachmentsRelation']) > 0) {
|
||||
$this->service->attach($parent, $requestData['attachmentsRelation']);
|
||||
}
|
||||
if (isset($requestData['attachmentsToRemove']) && count($requestData['attachmentsToRemove']) > 0) {
|
||||
$this->service->deleteAttachments($requestData['attachmentsToRemove']);
|
||||
}
|
||||
}
|
||||
|
||||
public function parentUpdated(AttachmentAble $parent, $requestData): void
|
||||
{
|
||||
if ($parent instanceof Task && isset($parent->getChanges()['project_id'])) {
|
||||
$this->service->handleProjectChange($parent);
|
||||
}
|
||||
if (isset($requestData['attachmentsRelation']) && count($requestData['attachmentsRelation']) > 0) {
|
||||
$this->service->attach($parent, $requestData['attachmentsRelation']);
|
||||
}
|
||||
if (isset($requestData['attachmentsToRemove']) && count($requestData['attachmentsToRemove']) > 0) {
|
||||
$this->service->deleteAttachments($requestData['attachmentsToRemove']);
|
||||
}
|
||||
}
|
||||
|
||||
public function parentDeleted(AttachmentAble $parent): void
|
||||
{
|
||||
$this->service->handleParentDeletion($parent);
|
||||
}
|
||||
|
||||
public function projectDeleted(Project $project): void
|
||||
{
|
||||
$this->service->handleProjectDeletion($project);
|
||||
}
|
||||
|
||||
public function attachmentCreated(Attachment $attachment, array $requestData): void
|
||||
{
|
||||
if ($this->service->storeFile($requestData['attachment'], $attachment) === false){
|
||||
// TODO: throw exception or abort_if?
|
||||
}
|
||||
}
|
||||
|
||||
public function subscribe(): array
|
||||
{
|
||||
return array_merge([
|
||||
'event.after.action.attachment.create' => [[__CLASS__, 'attachmentCreated']],
|
||||
'event.after.action.projects.destroy' => [[__CLASS__, 'projectDeleted']]
|
||||
], AttachmentHelper::getEvents(
|
||||
parentCreated: [[__CLASS__, 'parentCreated']],
|
||||
parentUpdated: [[__CLASS__, 'parentUpdated']],
|
||||
parentDeleted: [[__CLASS__, 'parentDeleted']],
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
85
app/Observers/TimeIntervalObserver.php
Normal file
85
app/Observers/TimeIntervalObserver.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Contracts\ScreenshotService;
|
||||
use App\Models\TimeInterval;
|
||||
use App\Models\CronTaskWorkers;
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class TimeIntervalObserver
|
||||
{
|
||||
|
||||
/**
|
||||
* Handle the TimeInterval "created" event.
|
||||
*
|
||||
* @param TimeInterval $timeInterval
|
||||
* @return void
|
||||
*/
|
||||
public function created(TimeInterval $timeInterval): void
|
||||
{
|
||||
$viewRecord = CronTaskWorkers::firstOrNew(
|
||||
[
|
||||
'user_id' => $timeInterval->user_id,
|
||||
'task_id' => $timeInterval->task_id
|
||||
]
|
||||
);
|
||||
|
||||
$viewRecord->user_id = $timeInterval->user_id;
|
||||
$viewRecord->task_id = $timeInterval->task_id;
|
||||
|
||||
$viewRecord->offset += Carbon::parse($timeInterval->end_at)->diffInSeconds($timeInterval->start_at);
|
||||
|
||||
$viewRecord->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the TimeInterval "updated" event.
|
||||
*
|
||||
* @param TimeInterval $timeInterval
|
||||
* @return void
|
||||
*/
|
||||
public function updated(TimeInterval $timeInterval): void
|
||||
{
|
||||
$oldInterval = new TimeInterval($timeInterval->getOriginal());
|
||||
|
||||
$this->deleted($oldInterval);
|
||||
$this->created($timeInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the TimeInterval "deleting" event.
|
||||
*
|
||||
* @param TimeInterval $timeInterval
|
||||
* @return void
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function deleting(TimeInterval $timeInterval): void
|
||||
{
|
||||
$screenshotService = app()->make(ScreenshotService::class);
|
||||
$screenshotService->destroyScreenshot($timeInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the TimeInterval "deleted" event.
|
||||
*
|
||||
* @param TimeInterval $timeInterval
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(TimeInterval $timeInterval): void
|
||||
{
|
||||
$viewRecord = CronTaskWorkers::firstWhere([
|
||||
['user_id', '=', $timeInterval->user_id],
|
||||
['task_id', '=', $timeInterval->task_id]
|
||||
]);
|
||||
|
||||
if ($viewRecord === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$viewRecord->offset -= Carbon::parse($timeInterval->end_at)->diffInSeconds($timeInterval->start_at);
|
||||
|
||||
$viewRecord->save();
|
||||
}
|
||||
}
|
||||
32
app/Observers/UserObserver.php
Normal file
32
app/Observers/UserObserver.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Mail\UserCreated;
|
||||
use App\Models\User;
|
||||
use Mail;
|
||||
use Illuminate\Support\Str;
|
||||
use Settings;
|
||||
|
||||
class UserObserver
|
||||
{
|
||||
/**
|
||||
* Handle the user "creating" event.
|
||||
*
|
||||
* @param User $user
|
||||
* @return void
|
||||
*/
|
||||
public function creating(User $user): void
|
||||
{
|
||||
if (!$user->password || request('send_invite')) {
|
||||
$password = request('password') ?? Str::random();
|
||||
|
||||
$user->password = $password;
|
||||
$user->invitation_sent = true;
|
||||
|
||||
$language = Settings::scope('core')->get('language', 'en');
|
||||
|
||||
Mail::to($user->email)->locale($language)->send(new UserCreated($user->email, $password));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user