* added unit tests and documentation #112 * fixed DashboardController * added CONTRIBUTION guidelines * fixed license year
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
|
||||
namespace App\Doctrine;
|
||||
|
||||
use App\Entity\UserPreference;
|
||||
use App\Timesheet\CalculatorInterface;
|
||||
use Doctrine\Common\EventSubscriber;
|
||||
use Doctrine\ORM\Event\PreUpdateEventArgs;
|
||||
use Doctrine\ORM\Event\LifecycleEventArgs;
|
||||
@@ -20,6 +20,27 @@ use App\Entity\Timesheet;
|
||||
*/
|
||||
class TimesheetSubscriber implements EventSubscriber
|
||||
{
|
||||
/**
|
||||
* @var CalculatorInterface[]
|
||||
*/
|
||||
protected $calculator;
|
||||
|
||||
/**
|
||||
* TimesheetSubscriber constructor.
|
||||
* @param iterable $calculators
|
||||
*/
|
||||
public function __construct(iterable $calculators)
|
||||
{
|
||||
foreach ($calculators as $calculator) {
|
||||
if (!($calculator instanceof CalculatorInterface)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Invalid TimesheetCalculator implementation given. Expected CalculatorInterface but received ' .
|
||||
get_class($calculator)
|
||||
);
|
||||
}
|
||||
}
|
||||
$this->calculator = $calculators;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
@@ -55,26 +76,12 @@ class TimesheetSubscriber implements EventSubscriber
|
||||
{
|
||||
$entity = $args->getObject();
|
||||
|
||||
if ($entity instanceof Timesheet) {
|
||||
if ($entity->getEnd() !== null) {
|
||||
$duration = $entity->getEnd()->getTimestamp() - $entity->getBegin()->getTimestamp();
|
||||
$entity->setDuration($duration);
|
||||
if (!($entity instanceof Timesheet)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO allow to set hourly rate on activity, project and customer and prefer these
|
||||
|
||||
$rate = $this->calculateRate($entity);
|
||||
$entity->setRate($rate);
|
||||
}
|
||||
foreach ($this->calculator as $calculator) {
|
||||
$calculator->calculate($entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $entity
|
||||
* @return float
|
||||
*/
|
||||
protected function calculateRate(Timesheet $entity)
|
||||
{
|
||||
$hourlyRate = (float) $entity->getUser()->getPreferenceValue(UserPreference::HOURLY_RATE, 0);
|
||||
return (float) $hourlyRate * ($entity->getDuration() / 3600);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user