added api endpoint to return instance specific timesheet config (#1335)

This commit is contained in:
hmr-it-jr
2020-01-04 01:44:11 +01:00
committed by Kevin Papst
parent 022dbe32e5
commit 50383673ca
7 changed files with 233 additions and 16 deletions

View File

@@ -26,6 +26,7 @@ nelmio_api_doc:
- { alias: TeamEntity, type: App\Entity\Team, groups: [Default, Entity, Team, Team_Entity] } - { alias: TeamEntity, type: App\Entity\Team, groups: [Default, Entity, Team, Team_Entity] }
- { alias: TeamCollection, type: App\Entity\Team, groups: [Default, Collection, Team] } - { alias: TeamCollection, type: App\Entity\Team, groups: [Default, Collection, Team] }
- { alias: I18nConfig, type: App\API\Model\I18n, groups: [Default] } - { alias: I18nConfig, type: App\API\Model\I18n, groups: [Default] }
- { alias: TimesheetConfig, type: App\API\Model\TimesheetConfig, groups: [Default] }
areas: areas:
path_patterns: path_patterns:
- ^/api(?!/doc) - ^/api(?!/doc)

View File

@@ -12,7 +12,9 @@ declare(strict_types=1);
namespace App\API; namespace App\API;
use App\API\Model\I18n; use App\API\Model\I18n;
use App\API\Model\TimesheetConfig;
use App\Configuration\LanguageFormattings; use App\Configuration\LanguageFormattings;
use App\Configuration\TimesheetConfiguration;
use App\Entity\User; use App\Entity\User;
use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
@@ -25,25 +27,31 @@ use Symfony\Component\HttpFoundation\Response;
/** /**
* @Security("is_granted('IS_AUTHENTICATED_REMEMBERED')") * @Security("is_granted('IS_AUTHENTICATED_REMEMBERED')")
*/ */
class ConfigurationController extends BaseApiController final class ConfigurationController extends BaseApiController
{ {
/** /**
* @var ViewHandlerInterface * @var ViewHandlerInterface
*/ */
protected $viewHandler; private $viewHandler;
/** /**
* @var LanguageFormattings * @var LanguageFormattings
*/ */
protected $formats; private $formats;
/**
* @var TimesheetConfiguration
*/
private $timesheetConfiguration;
/** /**
* @param ViewHandlerInterface $viewHandler * @param ViewHandlerInterface $viewHandler
* @param LanguageFormattings $formats * @param LanguageFormattings $formats
* @param TimesheetConfiguration $timesheetConfiguration
*/ */
public function __construct(ViewHandlerInterface $viewHandler, LanguageFormattings $formats) public function __construct(ViewHandlerInterface $viewHandler, LanguageFormattings $formats, TimesheetConfiguration $timesheetConfiguration)
{ {
$this->viewHandler = $viewHandler; $this->viewHandler = $viewHandler;
$this->formats = $formats; $this->formats = $formats;
$this->timesheetConfiguration = $timesheetConfiguration;
} }
/** /**
@@ -82,4 +90,35 @@ class ConfigurationController extends BaseApiController
return $this->viewHandler->handle($view); return $this->viewHandler->handle($view);
} }
/**
* Returns the instance specific timesheet configuration
*
* @SWG\Response(
* response=200,
* description="Returns the instance specific timesheet configuration",
* @SWG\Schema(ref="#/definitions/TimesheetConfig")
* )
*
* @Rest\Get(path="/config/timesheet")
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/
public function timesheetConfigAction(): Response
{
$model = new TimesheetConfig();
$model
->setTrackingMode($this->timesheetConfiguration->getTrackingMode())
->setDefaultBeginTime($this->timesheetConfiguration->getDefaultBeginTime())
->setActiveEntriesHardLimit($this->timesheetConfiguration->getActiveEntriesHardLimit())
->setActiveEntriesSoftLimit($this->timesheetConfiguration->getActiveEntriesSoftLimit())
->setIsAllowFutureTimes($this->timesheetConfiguration->isAllowFutureTimes())
;
$view = new View($model, 200);
$view->getContext()->setGroups(['Default', 'Config']);
return $this->viewHandler->handle($view);
}
} }

View File

@@ -11,49 +11,49 @@ declare(strict_types=1);
namespace App\API\Model; namespace App\API\Model;
class I18n final class I18n
{ {
/** /**
* Format used for 'begin' and 'end' * Format used for 'begin' and 'end'
* *
* @var string * @var string
*/ */
protected $formDateTime = ''; private $formDateTime = '';
/** /**
* Format used for toolbar queries * Format used for toolbar queries
* *
* @var string * @var string
*/ */
protected $formDate = ''; private $formDate = '';
/** /**
* Format used to display date-time values (see PHP function date_format) * Format used to display date-time values (see PHP function date_format)
* *
* @var string * @var string
*/ */
protected $dateTime = ''; private $dateTime = '';
/** /**
* Format used to display date values (see PHP function date_format) * Format used to display date values (see PHP function date_format)
* *
* @var string * @var string
*/ */
protected $date = ''; private $date = '';
/** /**
* Format used to display times (see PHP function date_format) * Format used to display times (see PHP function date_format)
* *
* @var string * @var string
*/ */
protected $time = ''; private $time = '';
/** /**
* Format used to display durations (replace: %h with hours, %m with minutes, %s with seconds) * Format used to display durations (replace: %h with hours, %m with minutes, %s with seconds)
* *
* @var string * @var string
*/ */
protected $duration = ''; private $duration = '';
/** /**
* Whether a twenty-four hour format is used (true) or 12-hours AM/PM format (false) * Whether a twenty-four hour format is used (true) or 12-hours AM/PM format (false)
* @var bool * @var bool
*/ */
protected $is24hours = true; private $is24hours = true;
/** /**
* @return string * @return string

View File

@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
/*
* 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\API\Model;
final class TimesheetConfig
{
/**
* See here: https://www.kimai.org/documentation/timesheet.html#tracking-modes
*
* @var string
*/
private $trackingMode = 'default';
/**
* @var string
*/
private $defaultBeginTime = 'now';
/**
* @var int
*/
private $activeEntriesHardLimit = 1;
/**
* @var int
*/
private $activeEntriesSoftLimit = 1;
/**
* @var bool
*/
private $isAllowFutureTimes = true;
/**
* @return string
*/
public function getTrackingMode(): string
{
return $this->trackingMode;
}
public function setTrackingMode(string $trackingMode): TimesheetConfig
{
$this->trackingMode = $trackingMode;
return $this;
}
public function getDefaultBeginTime(): string
{
return $this->defaultBeginTime;
}
public function setDefaultBeginTime(string $defaultBeginTime): TimesheetConfig
{
$this->defaultBeginTime = $defaultBeginTime;
return $this;
}
public function getActiveEntriesHardLimit(): int
{
return $this->activeEntriesHardLimit;
}
public function setActiveEntriesHardLimit(int $activeEntriesHardLimit): TimesheetConfig
{
$this->activeEntriesHardLimit = $activeEntriesHardLimit;
return $this;
}
public function getActiveEntriesSoftLimit(): int
{
return $this->activeEntriesSoftLimit;
}
public function setActiveEntriesSoftLimit(int $activeEntriesSoftLimit): TimesheetConfig
{
$this->activeEntriesSoftLimit = $activeEntriesSoftLimit;
return $this;
}
public function isAllowFutureTimes(): bool
{
return $this->isAllowFutureTimes;
}
public function setIsAllowFutureTimes(bool $isAllowFutureTimes): TimesheetConfig
{
$this->isAllowFutureTimes = $isAllowFutureTimes;
return $this;
}
}

View File

@@ -318,6 +318,9 @@
"phar-io/version": { "phar-io/version": {
"version": "1.0.1" "version": "1.0.1"
}, },
"php": {
"version": "7.2"
},
"php-cs-fixer/diff": { "php-cs-fixer/diff": {
"version": "v1.2.0" "version": "v1.2.0"
}, },

View File

@@ -16,7 +16,7 @@ use App\Entity\User;
*/ */
class ConfigurationControllerTest extends APIControllerBaseTest class ConfigurationControllerTest extends APIControllerBaseTest
{ {
public function testIsSecure() public function testIsI18nSecure()
{ {
$this->assertUrlIsSecured('/api/config/i18n'); $this->assertUrlIsSecured('/api/config/i18n');
} }
@@ -30,16 +30,43 @@ class ConfigurationControllerTest extends APIControllerBaseTest
$this->assertIsArray($result); $this->assertIsArray($result);
$this->assertNotEmpty($result); $this->assertNotEmpty($result);
$this->assertEquals(7, count($result)); $this->assertEquals(7, count($result));
$this->assertStructure($result); $this->assertI18nStructure($result);
} }
protected function assertStructure(array $result) protected function assertI18nStructure(array $result)
{ {
$expectedKeys = ['date', 'dateTime', 'duration', 'formDate', 'formDateTime', 'is24hours', 'time']; $expectedKeys = ['date', 'dateTime', 'duration', 'formDate', 'formDateTime', 'is24hours', 'time'];
$actual = array_keys($result); $actual = array_keys($result);
sort($actual); sort($actual);
sort($expectedKeys); sort($expectedKeys);
$this->assertEquals($expectedKeys, $actual, 'Activity structure does not match'); $this->assertEquals($expectedKeys, $actual, 'Config structure does not match');
}
public function testIsTimesheetSecure()
{
$this->assertUrlIsSecured('/api/config/timesheet');
}
public function testGetTimesheet()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->assertAccessIsGranted($client, '/api/config/timesheet', 'GET');
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(5, count($result));
$this->assertTimesheetStructure($result);
}
protected function assertTimesheetStructure(array $result)
{
$expectedKeys = ['activeEntriesHardLimit', 'activeEntriesSoftLimit', 'defaultBeginTime', 'isAllowFutureTimes', 'trackingMode'];
$actual = array_keys($result);
sort($actual);
sort($expectedKeys);
$this->assertEquals($expectedKeys, $actual, 'Config structure does not match');
} }
} }

View File

@@ -0,0 +1,46 @@
<?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\Tests\API\Model;
use App\API\Model\TimesheetConfig;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\API\Model\TimesheetConfig
*/
class TimesheetConfigTest extends TestCase
{
public function testDefaultValues()
{
$sut = new TimesheetConfig();
$this->assertTrue($sut->isAllowFutureTimes());
$this->assertEquals('now', $sut->getDefaultBeginTime());
$this->assertEquals('default', $sut->getTrackingMode());
$this->assertEquals(1, $sut->getActiveEntriesSoftLimit());
$this->assertEquals(1, $sut->getActiveEntriesHardLimit());
}
public function testSetter()
{
$sut = new TimesheetConfig();
$this->assertInstanceOf(TimesheetConfig::class, $sut->setIsAllowFutureTimes(false));
$this->assertInstanceOf(TimesheetConfig::class, $sut->setDefaultBeginTime('08:00'));
$this->assertInstanceOf(TimesheetConfig::class, $sut->setTrackingMode('punch'));
$this->assertInstanceOf(TimesheetConfig::class, $sut->setActiveEntriesSoftLimit(2));
$this->assertInstanceOf(TimesheetConfig::class, $sut->setActiveEntriesHardLimit(3));
$this->assertFalse($sut->isAllowFutureTimes());
$this->assertEquals('08:00', $sut->getDefaultBeginTime());
$this->assertEquals('punch', $sut->getTrackingMode());
$this->assertEquals(2, $sut->getActiveEntriesSoftLimit());
$this->assertEquals(3, $sut->getActiveEntriesHardLimit());
}
}