added activity events for plugins (#2516)
This commit is contained in:
104
src/Activity/ActivityService.php
Normal file
104
src/Activity/ActivityService.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Activity;
|
||||
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Project;
|
||||
use App\Event\ActivityCreateEvent;
|
||||
use App\Event\ActivityCreatePostEvent;
|
||||
use App\Event\ActivityCreatePreEvent;
|
||||
use App\Event\ActivityMetaDefinitionEvent;
|
||||
use App\Event\ActivityUpdatePostEvent;
|
||||
use App\Event\ActivityUpdatePreEvent;
|
||||
use App\Repository\ActivityRepository;
|
||||
use App\Validator\ValidationFailedException;
|
||||
use InvalidArgumentException;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
* @final
|
||||
*/
|
||||
class ActivityService
|
||||
{
|
||||
/**
|
||||
* @var ActivityRepository
|
||||
*/
|
||||
private $repository;
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
private $dispatcher;
|
||||
/**
|
||||
* @var ValidatorInterface
|
||||
*/
|
||||
private $validator;
|
||||
|
||||
public function __construct(ActivityRepository $activityRepository, EventDispatcherInterface $dispatcher, ValidatorInterface $validator)
|
||||
{
|
||||
$this->repository = $activityRepository;
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
public function createNewActivity(?Project $project = null): Activity
|
||||
{
|
||||
$activity = new Activity();
|
||||
|
||||
if ($project !== null) {
|
||||
$activity->setProject($project);
|
||||
}
|
||||
|
||||
$this->dispatcher->dispatch(new ActivityMetaDefinitionEvent($activity));
|
||||
$this->dispatcher->dispatch(new ActivityCreateEvent($activity));
|
||||
|
||||
return $activity;
|
||||
}
|
||||
|
||||
public function saveNewActivity(Activity $activity): Activity
|
||||
{
|
||||
if (null !== $activity->getId()) {
|
||||
throw new InvalidArgumentException('Cannot create activity, already persisted');
|
||||
}
|
||||
|
||||
$this->validateActivity($activity);
|
||||
|
||||
$this->dispatcher->dispatch(new ActivityCreatePreEvent($activity));
|
||||
$this->repository->saveActivity($activity);
|
||||
$this->dispatcher->dispatch(new ActivityCreatePostEvent($activity));
|
||||
|
||||
return $activity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Activity $activity
|
||||
* @param string[] $groups
|
||||
* @throws ValidationFailedException
|
||||
*/
|
||||
private function validateActivity(Activity $activity, array $groups = []): void
|
||||
{
|
||||
$errors = $this->validator->validate($activity, null, $groups);
|
||||
|
||||
if ($errors->count() > 0) {
|
||||
throw new ValidationFailedException($errors, 'Validation Failed');
|
||||
}
|
||||
}
|
||||
|
||||
public function updateActivity(Activity $activity): Activity
|
||||
{
|
||||
$this->validateActivity($activity);
|
||||
|
||||
$this->dispatcher->dispatch(new ActivityUpdatePreEvent($activity));
|
||||
$this->repository->saveActivity($activity);
|
||||
$this->dispatcher->dispatch(new ActivityUpdatePostEvent($activity));
|
||||
|
||||
return $activity;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Activity\ActivityService;
|
||||
use App\Configuration\SystemConfiguration;
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\ActivityRate;
|
||||
@@ -57,12 +58,17 @@ final class ActivityController extends AbstractController
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
private $dispatcher;
|
||||
/**
|
||||
* @var ActivityService
|
||||
*/
|
||||
private $activityService;
|
||||
|
||||
public function __construct(ActivityRepository $repository, SystemConfiguration $configuration, EventDispatcherInterface $dispatcher)
|
||||
public function __construct(ActivityRepository $repository, SystemConfiguration $configuration, EventDispatcherInterface $dispatcher, ActivityService $activityService)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->configuration = $configuration;
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->activityService = $activityService;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,10 +189,7 @@ final class ActivityController extends AbstractController
|
||||
*/
|
||||
public function createAction(Request $request, ?Project $project = null)
|
||||
{
|
||||
$activity = new Activity();
|
||||
if (null !== $project) {
|
||||
$activity->setProject($project);
|
||||
}
|
||||
$activity = $this->activityService->createNewActivity($project);
|
||||
|
||||
$event = new ActivityMetaDefinitionEvent($activity);
|
||||
$this->dispatcher->dispatch($event);
|
||||
@@ -196,7 +199,7 @@ final class ActivityController extends AbstractController
|
||||
|
||||
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
||||
try {
|
||||
$this->repository->saveActivity($activity);
|
||||
$this->activityService->saveNewActivity($activity);
|
||||
$this->flashSuccess('action.update.success');
|
||||
|
||||
return $this->redirectToRoute('admin_activity');
|
||||
@@ -226,7 +229,7 @@ final class ActivityController extends AbstractController
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
try {
|
||||
$this->repository->saveActivity($activity);
|
||||
$this->activityService->updateActivity($activity);
|
||||
$this->flashSuccess('action.update.success');
|
||||
|
||||
if ($this->isGranted('view', $activity)) {
|
||||
@@ -286,7 +289,7 @@ final class ActivityController extends AbstractController
|
||||
|
||||
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
||||
try {
|
||||
$this->repository->saveActivity($activity);
|
||||
$this->activityService->updateActivity($activity);
|
||||
$this->flashSuccess('action.update.success');
|
||||
|
||||
return $this->redirectToRoute('activity_details', ['id' => $activity->getId()]);
|
||||
|
||||
@@ -23,6 +23,9 @@ use InvalidArgumentException;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
* @final
|
||||
*/
|
||||
class CustomerService
|
||||
{
|
||||
private $repository;
|
||||
|
||||
34
src/Event/AbstractActivityEvent.php
Normal file
34
src/Event/AbstractActivityEvent.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Event;
|
||||
|
||||
use App\Entity\Activity;
|
||||
use Symfony\Contracts\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* Base event class to used with activity manipulations.
|
||||
*/
|
||||
abstract class AbstractActivityEvent extends Event
|
||||
{
|
||||
/**
|
||||
* @var Activity
|
||||
*/
|
||||
private $activity;
|
||||
|
||||
public function __construct(Activity $activity)
|
||||
{
|
||||
$this->activity = $activity;
|
||||
}
|
||||
|
||||
public function getActivity(): Activity
|
||||
{
|
||||
return $this->activity;
|
||||
}
|
||||
}
|
||||
17
src/Event/ActivityCreateEvent.php
Normal file
17
src/Event/ActivityCreateEvent.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Event;
|
||||
|
||||
/**
|
||||
* Triggered for new activity instances, which might or might not be saved.
|
||||
*/
|
||||
final class ActivityCreateEvent extends AbstractActivityEvent
|
||||
{
|
||||
}
|
||||
17
src/Event/ActivityCreatePostEvent.php
Normal file
17
src/Event/ActivityCreatePostEvent.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Event;
|
||||
|
||||
/**
|
||||
* Triggered for activity instances, which were just saved.
|
||||
*/
|
||||
final class ActivityCreatePostEvent extends AbstractActivityEvent
|
||||
{
|
||||
}
|
||||
17
src/Event/ActivityCreatePreEvent.php
Normal file
17
src/Event/ActivityCreatePreEvent.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Event;
|
||||
|
||||
/**
|
||||
* Triggered for activity instances, which are just about to being saved.
|
||||
*/
|
||||
final class ActivityCreatePreEvent extends AbstractActivityEvent
|
||||
{
|
||||
}
|
||||
17
src/Event/ActivityUpdatePostEvent.php
Normal file
17
src/Event/ActivityUpdatePostEvent.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Event;
|
||||
|
||||
/**
|
||||
* Triggered for activity instances, which were updated.
|
||||
*/
|
||||
final class ActivityUpdatePostEvent extends AbstractActivityEvent
|
||||
{
|
||||
}
|
||||
17
src/Event/ActivityUpdatePreEvent.php
Normal file
17
src/Event/ActivityUpdatePreEvent.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Event;
|
||||
|
||||
/**
|
||||
* Triggered for activity instances, which are about to being updated.
|
||||
*/
|
||||
final class ActivityUpdatePreEvent extends AbstractActivityEvent
|
||||
{
|
||||
}
|
||||
@@ -23,6 +23,9 @@ use InvalidArgumentException;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
* @final
|
||||
*/
|
||||
class ProjectService
|
||||
{
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user