cleanup global context (#1943)

This commit is contained in:
Kevin Papst
2020-09-08 20:35:14 +02:00
committed by GitHub
parent 217960de60
commit e2a742b5c1
9 changed files with 54 additions and 63 deletions

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace App\API; namespace App\API;
use App\Entity\User; use App\Entity\User;
use App\Timesheet\DateTimeFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType; use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
@@ -22,4 +23,13 @@ abstract class BaseApiController extends AbstractController
{ {
public const DATE_FORMAT = DateTimeType::HTML5_FORMAT; public const DATE_FORMAT = DateTimeType::HTML5_FORMAT;
public const DATE_FORMAT_PHP = 'Y-m-d\TH:i:s'; public const DATE_FORMAT_PHP = 'Y-m-d\TH:i:s';
protected function getDateTimeFactory(?User $user = null): DateTimeFactory
{
if (null === $user) {
$user = $this->getUser();
}
return new DateTimeFactory(new \DateTimeZone($user->getTimezone()));
}
} }

View File

@@ -11,7 +11,6 @@ declare(strict_types=1);
namespace App\API; namespace App\API;
use App\Entity\Customer;
use App\Entity\Project; use App\Entity\Project;
use App\Entity\ProjectRate; use App\Entity\ProjectRate;
use App\Entity\User; use App\Entity\User;
@@ -21,7 +20,6 @@ use App\Form\API\ProjectRateApiForm;
use App\Repository\ProjectRateRepository; use App\Repository\ProjectRateRepository;
use App\Repository\ProjectRepository; use App\Repository\ProjectRepository;
use App\Repository\Query\ProjectQuery; use App\Repository\Query\ProjectQuery;
use App\Timesheet\UserDateTimeFactory;
use App\Utils\SearchTerm; use App\Utils\SearchTerm;
use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\RouteResource; use FOS\RestBundle\Controller\Annotations\RouteResource;
@@ -62,21 +60,16 @@ class ProjectController extends BaseApiController
* @var EventDispatcherInterface * @var EventDispatcherInterface
*/ */
private $dispatcher; private $dispatcher;
/**
* @var UserDateTimeFactory
*/
private $dateTime;
/** /**
* @var ProjectRateRepository * @var ProjectRateRepository
*/ */
private $projectRateRepository; private $projectRateRepository;
public function __construct(ViewHandlerInterface $viewHandler, ProjectRepository $repository, EventDispatcherInterface $dispatcher, UserDateTimeFactory $dateTime, ProjectRateRepository $projectRateRepository) public function __construct(ViewHandlerInterface $viewHandler, ProjectRepository $repository, EventDispatcherInterface $dispatcher, ProjectRateRepository $projectRateRepository)
{ {
$this->viewHandler = $viewHandler; $this->viewHandler = $viewHandler;
$this->repository = $repository; $this->repository = $repository;
$this->dispatcher = $dispatcher; $this->dispatcher = $dispatcher;
$this->dateTime = $dateTime;
$this->projectRateRepository = $projectRateRepository; $this->projectRateRepository = $projectRateRepository;
} }
@@ -143,16 +136,17 @@ class ProjectController extends BaseApiController
} }
if (!$ignoreDates) { if (!$ignoreDates) {
$factory = $this->getDateTimeFactory();
if (null !== ($begin = $paramFetcher->get('start')) && !empty($begin)) { if (null !== ($begin = $paramFetcher->get('start')) && !empty($begin)) {
$query->setProjectStart($this->dateTime->createDateTime($begin)); $query->setProjectStart($factory->createDateTime($begin));
} }
if (null !== ($end = $paramFetcher->get('end')) && !empty($end)) { if (null !== ($end = $paramFetcher->get('end')) && !empty($end)) {
$query->setProjectEnd($this->dateTime->createDateTime($end)); $query->setProjectEnd($factory->createDateTime($end));
} }
if (empty($begin) && empty($end)) { if (empty($begin) && empty($end)) {
$now = $this->dateTime->createDateTime(); $now = $factory->createDateTime();
$query->setProjectStart($now); $query->setProjectStart($now);
$query->setProjectEnd($now); $query->setProjectEnd($now);
} }

View File

@@ -12,7 +12,6 @@ declare(strict_types=1);
namespace App\API; namespace App\API;
use App\Configuration\TimesheetConfiguration; use App\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet;
use App\Entity\User; use App\Entity\User;
use App\Event\TimesheetMetaDefinitionEvent; use App\Event\TimesheetMetaDefinitionEvent;
use App\Form\API\TimesheetApiEditForm; use App\Form\API\TimesheetApiEditForm;
@@ -23,7 +22,6 @@ use App\Timesheet\RoundingService;
use App\Timesheet\TimesheetService; use App\Timesheet\TimesheetService;
use App\Timesheet\TrackingMode\TrackingModeInterface; use App\Timesheet\TrackingMode\TrackingModeInterface;
use App\Timesheet\TrackingModeService; use App\Timesheet\TrackingModeService;
use App\Timesheet\UserDateTimeFactory;
use App\Utils\SearchTerm; use App\Utils\SearchTerm;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\Controller\Annotations as Rest;
@@ -68,10 +66,6 @@ class TimesheetController extends BaseApiController
* @var TimesheetConfiguration * @var TimesheetConfiguration
*/ */
private $configuration; private $configuration;
/**
* @var UserDateTimeFactory
*/
private $dateTime;
/** /**
* @var TagRepository * @var TagRepository
*/ */
@@ -96,7 +90,6 @@ class TimesheetController extends BaseApiController
public function __construct( public function __construct(
ViewHandlerInterface $viewHandler, ViewHandlerInterface $viewHandler,
TimesheetRepository $repository, TimesheetRepository $repository,
UserDateTimeFactory $dateTime,
TimesheetConfiguration $configuration, TimesheetConfiguration $configuration,
TagRepository $tagRepository, TagRepository $tagRepository,
TrackingModeService $trackingModeService, TrackingModeService $trackingModeService,
@@ -107,7 +100,6 @@ class TimesheetController extends BaseApiController
$this->viewHandler = $viewHandler; $this->viewHandler = $viewHandler;
$this->repository = $repository; $this->repository = $repository;
$this->configuration = $configuration; $this->configuration = $configuration;
$this->dateTime = $dateTime;
$this->tagRepository = $tagRepository; $this->tagRepository = $tagRepository;
$this->trackingModeService = $trackingModeService; $this->trackingModeService = $trackingModeService;
$this->dispatcher = $dispatcher; $this->dispatcher = $dispatcher;
@@ -231,12 +223,14 @@ class TimesheetController extends BaseApiController
$query->setOrderBy($orderBy); $query->setOrderBy($orderBy);
} }
$factory = $this->getDateTimeFactory();
if (null !== ($begin = $paramFetcher->get('begin'))) { if (null !== ($begin = $paramFetcher->get('begin'))) {
$query->setBegin($this->dateTime->createDateTime($begin)); $query->setBegin($factory->createDateTime($begin));
} }
if (null !== ($end = $paramFetcher->get('end'))) { if (null !== ($end = $paramFetcher->get('end'))) {
$query->setEnd($this->dateTime->createDateTime($end)); $query->setEnd($factory->createDateTime($end));
} }
if (null !== ($active = $paramFetcher->get('active'))) { if (null !== ($active = $paramFetcher->get('active'))) {
@@ -262,7 +256,7 @@ class TimesheetController extends BaseApiController
} }
if (!empty($modifiedAfter = $paramFetcher->get('modified_after'))) { if (!empty($modifiedAfter = $paramFetcher->get('modified_after'))) {
$query->setModifiedAfter($this->dateTime->createDateTime($modifiedAfter)); $query->setModifiedAfter($factory->createDateTime($modifiedAfter));
} }
/** @var Pagerfanta $data */ /** @var Pagerfanta $data */
@@ -519,7 +513,8 @@ class TimesheetController extends BaseApiController
public function recentAction(ParamFetcherInterface $paramFetcher): Response public function recentAction(ParamFetcherInterface $paramFetcher): Response
{ {
$user = $this->getUser(); $user = $this->getUser();
$begin = $this->dateTime->createDateTime('-1 year'); $factory = $this->getDateTimeFactory();
$begin = $factory->createDateTime('-1 year');
$limit = 10; $limit = 10;
if ($this->isGranted('view_other_timesheet') && null !== ($reqUser = $paramFetcher->get('user'))) { if ($this->isGranted('view_other_timesheet') && null !== ($reqUser = $paramFetcher->get('user'))) {
@@ -534,7 +529,7 @@ class TimesheetController extends BaseApiController
} }
if (null !== ($reqBegin = $paramFetcher->get('begin'))) { if (null !== ($reqBegin = $paramFetcher->get('begin'))) {
$begin = $this->dateTime->createDateTime($reqBegin); $begin = $factory->createDateTime($reqBegin);
} }
$data = $this->repository->getRecentActivities($user, $begin, $limit); $data = $this->repository->getRecentActivities($user, $begin, $limit);
@@ -653,7 +648,7 @@ class TimesheetController extends BaseApiController
$copyTimesheet = $this->service->createNewTimesheet($user); $copyTimesheet = $this->service->createNewTimesheet($user);
$copyTimesheet $copyTimesheet
->setBegin($this->dateTime->createDateTime()) ->setBegin($this->getDateTimeFactory()->createDateTime())
->setActivity($timesheet->getActivity()) ->setActivity($timesheet->getActivity())
->setProject($timesheet->getProject()) ->setProject($timesheet->getProject())
; ;

View File

@@ -13,7 +13,6 @@ use App\Calendar\Google;
use App\Calendar\Source; use App\Calendar\Source;
use App\Configuration\CalendarConfiguration; use App\Configuration\CalendarConfiguration;
use App\Timesheet\TrackingModeService; use App\Timesheet\TrackingModeService;
use App\Timesheet\UserDateTimeFactory;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
@@ -28,14 +27,14 @@ class CalendarController extends AbstractController
/** /**
* @Route(path="/", name="calendar", methods={"GET"}) * @Route(path="/", name="calendar", methods={"GET"})
*/ */
public function userCalendar(CalendarConfiguration $configuration, UserDateTimeFactory $dateTime, TrackingModeService $service) public function userCalendar(CalendarConfiguration $configuration, TrackingModeService $service)
{ {
$mode = $service->getActiveMode(); $mode = $service->getActiveMode();
return $this->render('calendar/user.html.twig', [ return $this->render('calendar/user.html.twig', [
'config' => $configuration, 'config' => $configuration,
'google' => $this->getGoogleSources($configuration), 'google' => $this->getGoogleSources($configuration),
'now' => $dateTime->createDateTime(), 'now' => $this->getDateTimeFactory()->createDateTime(),
'is_punch_mode' => !$mode->canEditDuration() && !$mode->canEditBegin() && !$mode->canEditEnd() 'is_punch_mode' => !$mode->canEditDuration() && !$mode->canEditBegin() && !$mode->canEditEnd()
]); ]);
} }

View File

@@ -22,7 +22,6 @@ use App\Repository\InvoiceRepository;
use App\Repository\InvoiceTemplateRepository; use App\Repository\InvoiceTemplateRepository;
use App\Repository\Query\BaseQuery; use App\Repository\Query\BaseQuery;
use App\Repository\Query\InvoiceQuery; use App\Repository\Query\InvoiceQuery;
use App\Timesheet\UserDateTimeFactory;
use Exception; use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
@@ -49,10 +48,6 @@ final class InvoiceController extends AbstractController
* @var InvoiceTemplateRepository * @var InvoiceTemplateRepository
*/ */
private $templateRepository; private $templateRepository;
/**
* @var UserDateTimeFactory
*/
private $dateTimeFactory;
/** /**
* @var InvoiceRepository * @var InvoiceRepository
*/ */
@@ -62,12 +57,11 @@ final class InvoiceController extends AbstractController
*/ */
private $dispatcher; private $dispatcher;
public function __construct(ServiceInvoice $service, InvoiceTemplateRepository $templateRepository, InvoiceRepository $invoiceRepository, UserDateTimeFactory $dateTimeFactory, EventDispatcherInterface $dispatcher) public function __construct(ServiceInvoice $service, InvoiceTemplateRepository $templateRepository, InvoiceRepository $invoiceRepository, EventDispatcherInterface $dispatcher)
{ {
$this->service = $service; $this->service = $service;
$this->templateRepository = $templateRepository; $this->templateRepository = $templateRepository;
$this->invoiceRepository = $invoiceRepository; $this->invoiceRepository = $invoiceRepository;
$this->dateTimeFactory = $dateTimeFactory;
$this->dispatcher = $dispatcher; $this->dispatcher = $dispatcher;
} }
@@ -139,8 +133,9 @@ final class InvoiceController extends AbstractController
protected function getDefaultQuery(): InvoiceQuery protected function getDefaultQuery(): InvoiceQuery
{ {
$begin = $this->dateTimeFactory->createDateTime('first day of this month'); $factory = $this->getDateTimeFactory();
$end = $this->dateTimeFactory->createDateTime('last day of this month'); $begin = $factory->getStartOfMonth();
$end = $factory->getEndOfMonth();
$query = new InvoiceQuery(); $query = new InvoiceQuery();
$query->setOrder(InvoiceQuery::ORDER_ASC); $query->setOrder(InvoiceQuery::ORDER_ASC);

View File

@@ -29,7 +29,6 @@ use App\Repository\TimesheetRepository;
use App\Timesheet\TimesheetService; use App\Timesheet\TimesheetService;
use App\Timesheet\TrackingMode\TrackingModeInterface; use App\Timesheet\TrackingMode\TrackingModeInterface;
use App\Timesheet\TrackingModeService; use App\Timesheet\TrackingModeService;
use App\Timesheet\UserDateTimeFactory;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
@@ -38,10 +37,6 @@ use Symfony\Component\HttpFoundation\Response;
abstract class TimesheetAbstractController extends AbstractController abstract class TimesheetAbstractController extends AbstractController
{ {
/**
* @var UserDateTimeFactory
*/
protected $dateTime;
/** /**
* @var TimesheetRepository * @var TimesheetRepository
*/ */
@@ -64,14 +59,12 @@ abstract class TimesheetAbstractController extends AbstractController
protected $service; protected $service;
public function __construct( public function __construct(
UserDateTimeFactory $dateTime,
TimesheetRepository $repository, TimesheetRepository $repository,
TrackingModeService $trackingModeService, TrackingModeService $trackingModeService,
EventDispatcherInterface $dispatcher, EventDispatcherInterface $dispatcher,
ServiceExport $exportService, ServiceExport $exportService,
TimesheetService $timesheetService TimesheetService $timesheetService
) { ) {
$this->dateTime = $dateTime;
$this->repository = $repository; $this->repository = $repository;
$this->trackingModeService = $trackingModeService; $this->trackingModeService = $trackingModeService;
$this->dispatcher = $dispatcher; $this->dispatcher = $dispatcher;
@@ -238,16 +231,18 @@ abstract class TimesheetAbstractController extends AbstractController
$form->setData($query); $form->setData($query);
$form->submit($request->query->all(), false); $form->submit($request->query->all(), false);
$factory = $this->getDateTimeFactory();
// by default the current month is exported, but it can be overwritten // by default the current month is exported, but it can be overwritten
// this should not be removed, otherwise we would export EVERY available record in the admin section // this should not be removed, otherwise we would export EVERY available record in the admin section
// as the default toolbar query does neither limit the user nor the date-range! // as the default toolbar query does neither limit the user nor the date-range!
if (null === $query->getBegin()) { if (null === $query->getBegin()) {
$query->setBegin($this->dateTime->createDateTime('first day of this month')); $query->setBegin($factory->getStartOfMonth());
} }
$query->getBegin()->setTime(0, 0, 0); $query->getBegin()->setTime(0, 0, 0);
if (null === $query->getEnd()) { if (null === $query->getEnd()) {
$query->setEnd($this->dateTime->createDateTime('last day of this month')); $query->setEnd($factory->getEndOfMonth());
} }
$query->getEnd()->setTime(23, 59, 59); $query->getEnd()->setTime(23, 59, 59);

View File

@@ -32,13 +32,10 @@ final class UserExtension extends AbstractTypeExtension
return [FormType::class]; return [FormType::class];
} }
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
{ {
$resolver->setDefined(['user']); $resolver->setDefined(['user']);
// null needs to be allowed, as there is no user for anonymoud forms (like "forgot password" and "registration") // null needs to be allowed, as there is no user for anonymous forms (like "forgot password" and "registration")
$resolver->setAllowedTypes('user', [User::class, 'null']); $resolver->setAllowedTypes('user', [User::class, 'null']);
$resolver->setDefault('user', $this->user->getUser()); $resolver->setDefault('user', $this->user->getUser());
} }

View File

@@ -19,7 +19,7 @@ use App\Event\InvoicePreRenderEvent;
use App\Repository\InvoiceDocumentRepository; use App\Repository\InvoiceDocumentRepository;
use App\Repository\InvoiceRepository; use App\Repository\InvoiceRepository;
use App\Repository\Query\InvoiceQuery; use App\Repository\Query\InvoiceQuery;
use App\Timesheet\UserDateTimeFactory; use App\Timesheet\DateTimeFactory;
use App\Utils\FileHelper; use App\Utils\FileHelper;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -54,10 +54,6 @@ final class ServiceInvoice
* @var FileHelper * @var FileHelper
*/ */
private $fileHelper; private $fileHelper;
/**
* @var UserDateTimeFactory
*/
private $dateTimeFactory;
/** /**
* @var LanguageFormattings * @var LanguageFormattings
*/ */
@@ -67,12 +63,11 @@ final class ServiceInvoice
*/ */
private $invoiceRepository; private $invoiceRepository;
public function __construct(InvoiceDocumentRepository $repository, FileHelper $fileHelper, InvoiceRepository $invoiceRepository, UserDateTimeFactory $dateTimeFactory, LanguageFormattings $formatter) public function __construct(InvoiceDocumentRepository $repository, FileHelper $fileHelper, InvoiceRepository $invoiceRepository, LanguageFormattings $formatter)
{ {
$this->documents = $repository; $this->documents = $repository;
$this->fileHelper = $fileHelper; $this->fileHelper = $fileHelper;
$this->invoiceRepository = $invoiceRepository; $this->invoiceRepository = $invoiceRepository;
$this->dateTimeFactory = $dateTimeFactory;
$this->formatter = $formatter; $this->formatter = $formatter;
} }
@@ -272,11 +267,13 @@ final class ServiceInvoice
return []; return [];
} }
$factory = $this->getDateTimeFactory($query);
if (null === $query->getBegin()) { if (null === $query->getBegin()) {
$query->setBegin($this->dateTimeFactory->createDateTime('first day of this month')); $query->setBegin($factory->getStartOfMonth());
} }
if (null === $query->getEnd()) { if (null === $query->getEnd()) {
$query->setEnd($this->dateTimeFactory->createDateTime('last day of this month')); $query->setEnd($factory->getEndOfMonth());
} }
$query->getBegin()->setTime(0, 0, 0); $query->getBegin()->setTime(0, 0, 0);
$query->getEnd()->setTime(23, 59, 59); $query->getEnd()->setTime(23, 59, 59);
@@ -307,6 +304,17 @@ final class ServiceInvoice
return $entries; return $entries;
} }
private function getDateTimeFactory(InvoiceQuery $query): DateTimeFactory
{
$timezone = date_default_timezone_get();
if (null !== $query->getCurrentUser()) {
$timezone = $query->getCurrentUser()->getTimezone();
}
return new DateTimeFactory(new \DateTimeZone($timezone));
}
/** /**
* @param array<string, InvoiceItemInterface[]> $entries * @param array<string, InvoiceItemInterface[]> $entries
*/ */
@@ -433,7 +441,7 @@ final class ServiceInvoice
$model = new InvoiceModel(new DefaultInvoiceFormatter($this->formatter, $template->getLanguage())); $model = new InvoiceModel(new DefaultInvoiceFormatter($this->formatter, $template->getLanguage()));
$model $model
->setTemplate($template) ->setTemplate($template)
->setInvoiceDate($this->dateTimeFactory->createDateTime()) ->setInvoiceDate($this->getDateTimeFactory($query)->createDateTime())
->setQuery($query) ->setQuery($query)
; ;

View File

@@ -20,7 +20,6 @@ use App\Invoice\ServiceInvoice;
use App\Repository\InvoiceDocumentRepository; use App\Repository\InvoiceDocumentRepository;
use App\Repository\InvoiceRepository; use App\Repository\InvoiceRepository;
use App\Repository\Query\InvoiceQuery; use App\Repository\Query\InvoiceQuery;
use App\Tests\Mocks\Security\UserDateTimeFactoryFactory;
use App\Utils\FileHelper; use App\Utils\FileHelper;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Twig\Environment; use Twig\Environment;
@@ -44,9 +43,8 @@ class ServiceInvoiceTest extends TestCase
$repo = new InvoiceDocumentRepository($paths); $repo = new InvoiceDocumentRepository($paths);
$invoiceRepo = $this->createMock(InvoiceRepository::class); $invoiceRepo = $this->createMock(InvoiceRepository::class);
$userDateTime = (new UserDateTimeFactoryFactory($this))->create();
return new ServiceInvoice($repo, new FileHelper(realpath(__DIR__ . '/../../var/data/')), $invoiceRepo, $userDateTime, $formattings); return new ServiceInvoice($repo, new FileHelper(realpath(__DIR__ . '/../../var/data/')), $invoiceRepo, $formattings);
} }
public function testInvalidExceptionOnChangeState() public function testInvalidExceptionOnChangeState()