allow custom export repositories (#2182)

This commit is contained in:
Kevin Papst
2020-12-10 15:00:14 +01:00
committed by GitHub
parent c3fe8d1c39
commit 7a3388646b
18 changed files with 320 additions and 72 deletions

View File

@@ -46,6 +46,8 @@ trait RendererTrait
}
$id = $customerId . '_' . $projectId;
$type = $exportItem->getType();
$category = $exportItem->getCategory();
if (!isset($summary[$id])) {
$summary[$id] = [
@@ -56,6 +58,24 @@ trait RendererTrait
'rate' => 0,
'rate_internal' => 0,
'duration' => 0,
'type' => [],
'types' => [],
];
}
if (!isset($summary[$id]['type'][$type])) {
$summary[$id]['type'][$type] = [
'rate' => 0,
'rate_internal' => 0,
'duration' => 0,
];
}
if (!isset($summary[$id]['types'][$type][$category])) {
$summary[$id]['types'][$type][$category] = [
'rate' => 0,
'rate_internal' => 0,
'duration' => 0,
];
}
@@ -75,12 +95,23 @@ trait RendererTrait
}
$summary[$id]['rate'] += $exportItem->getRate();
$summary[$id]['type'][$type]['rate'] += $exportItem->getRate();
$summary[$id]['types'][$type][$category]['rate'] += $exportItem->getRate();
if (method_exists($exportItem, 'getInternalRate')) {
$summary[$id]['rate_internal'] += $exportItem->getInternalRate();
$summary[$id]['type'][$type]['rate_internal'] += $exportItem->getInternalRate();
$summary[$id]['types'][$type][$category]['rate_internal'] += $exportItem->getInternalRate();
} else {
$summary[$id]['rate_internal'] += $exportItem->getRate();
$summary[$id]['type'][$type]['rate_internal'] += $exportItem->getRate();
$summary[$id]['types'][$type][$category]['rate_internal'] += $exportItem->getRate();
}
$summary[$id]['duration'] += $duration;
$summary[$id]['type'][$type]['duration'] += $duration;
$summary[$id]['types'][$type][$category]['duration'] += $duration;
$summary[$id]['activities'][$activityId]['rate'] += $exportItem->getRate();
$summary[$id]['activities'][$activityId]['duration'] += $duration;
}

View File

@@ -0,0 +1,38 @@
<?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\Export;
use App\Repository\Query\ExportQuery;
interface ExportRepositoryInterface
{
/**
* This method will receive ALL exported items, loaded from all repositories.
* Be careful to only handle the ones, which belong to your repository.
*
* @param ExportItemInterface[] $items
* @return void
*/
public function setExported(array $items): void;
/**
* @param ExportQuery $query
* @return ExportItemInterface[]
*/
public function getExportItemsForQuery(ExportQuery $query): iterable;
/**
* Returns the type of this repository.
* Must match the value returned by your entities via ExportItemInterface::getType().
*
* @return string
*/
public function getType(): string;
}

View File

@@ -9,17 +9,22 @@
namespace App\Export;
use App\Repository\Query\ExportQuery;
final class ServiceExport
{
/**
* @var ExportRendererInterface[]
*/
private $renderer = [];
/**
* @var TimesheetExportInterface[]
*/
private $exporter = [];
/**
* @var ExportRepositoryInterface[]
*/
private $repositories = [];
public function addRenderer(ExportRendererInterface $renderer): ServiceExport
{
@@ -72,4 +77,29 @@ final class ServiceExport
return null;
}
public function addExportRepository(ExportRepositoryInterface $repository): ServiceExport
{
$this->repositories[] = $repository;
return $this;
}
public function getExportItems(ExportQuery $query)
{
$items = [];
foreach ($this->repositories as $repository) {
$items = array_merge($items, $repository->getExportItemsForQuery($query));
}
return $items;
}
public function setExported(array $items): void
{
foreach ($this->repositories as $repository) {
$repository->setExported($items);
}
}
}

View File

@@ -0,0 +1,57 @@
<?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\Export;
use App\Entity\Timesheet;
use App\Repository\Query\ExportQuery;
use App\Repository\TimesheetRepository;
final class TimesheetExportRepository implements ExportRepositoryInterface
{
/**
* @var TimesheetRepository
*/
private $repository;
public function __construct(TimesheetRepository $repository)
{
$this->repository = $repository;
}
/**
* @param Timesheet[] $items
*/
public function setExported(array $items): void
{
$timesheets = [];
foreach ($items as $item) {
if ($item instanceof Timesheet) {
$timesheets[] = $item;
}
}
if (empty($timesheets)) {
return;
}
$this->repository->setExported($timesheets);
}
public function getExportItemsForQuery(ExportQuery $query): iterable
{
return $this->repository->getTimesheetsForQuery($query, true);
}
public function getType(): string
{
return 'timesheet';
}
}