File: //home/timetracker.panomity.com/tm.old/Repository/TaskService.php
<?php
/*
* This file is part of the TaskManagementBundle for Kimai 2.
* All rights reserved by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace KimaiPlugin\TaskManagementBundle\Repository;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Timesheet\TimesheetService;
use DateTime;
use KimaiPlugin\TaskManagementBundle\Entity\Task;
final class TaskService
{
/**
* @var TaskRepository
*/
private $tasks;
/**
* @var TimesheetService
*/
private $timesheets;
public function __construct(TaskRepository $task, TimesheetService $timesheet)
{
$this->tasks = $task;
$this->timesheets = $timesheet;
}
/**
* @param string|int $id
* @return Task|null
*/
public function findTask($id): ?Task
{
return $this->tasks->find($id);
}
/**
* @param Task $task
* @param User|null $user fallback user if none is set within task
*/
public function startTask(Task $task, ?User $user): void
{
$this->logWork($task, null, null, $user);
}
public function logWork(Task $task, ?DateTime $begin = null, ?DateTime $end = null, ?User $user = null): Timesheet
{
$timesheet = new Timesheet();
$timesheet
->setProject($task->getProject())
->setActivity($task->getActivity())
->setDescription($task->getDescription())
;
if ($task->getUser() !== null) {
$timesheet->setUser($task->getUser());
} elseif ($user !== null) {
$timesheet->setUser($user);
}
foreach ($task->getTags() as $tag) {
$timesheet->addTag($tag);
}
if ($begin !== null) {
$timesheet->setBegin($begin);
}
if ($end !== null) {
$timesheet->setEnd($end);
}
$this->timesheets->prepareNewTimesheet($timesheet);
$this->timesheets->saveNewTimesheet($timesheet);
$task->addTimesheet($timesheet);
$task->setStatus(Task::STATUS_IN_PROGRESS);
$this->tasks->saveTask($task);
return $timesheet;
}
/**
* Makes sure, that there is no active timesheet fpr the given user and task.
*
* @param Task $task
* @param User $user
*/
public function stopTask(Task $task, User $user): void
{
$timesheet = $task->getActiveRecord($user);
if (null !== $timesheet) {
$this->timesheets->stopTimesheet($timesheet);
}
}
private function stopActiveRecords(Task $task): void
{
$timesheets = $task->getActiveRecords();
foreach ($timesheets as $timesheet) {
$this->timesheets->stopTimesheet($timesheet);
}
}
public function deleteTask(Task $task): void
{
$this->stopActiveRecords($task);
$this->tasks->deleteTask($task);
}
public function closeTask(Task $task): void
{
$this->stopActiveRecords($task);
$task->setStatus(Task::STATUS_DONE);
$this->tasks->saveTask($task);
}
public function reopenTask(Task $task): void
{
$task->setStatus(Task::STATUS_IN_PROGRESS);
$this->tasks->saveTask($task);
}
public function unassignTask(Task $task): void
{
$this->stopTask($task, $task->getUser());
$task->setUser(null);
$task->setStatus(Task::STATUS_PENDING);
$this->tasks->saveTask($task);
}
public function assignTask(Task $task, User $user): void
{
$task->setUser($user);
$task->setStatus(Task::STATUS_IN_PROGRESS);
$this->tasks->saveTask($task);
}
}