systemConfiguration = $systemConfiguration; } /** * @param TimesheetEntity $timesheet * @param Constraint $constraint */ public function validate($timesheet, Constraint $constraint) { if (!($constraint instanceof TimesheetLongRunning)) { throw new UnexpectedTypeException($constraint, TimesheetLongRunning::class); } if (!\is_object($timesheet) || !($timesheet instanceof TimesheetEntity)) { throw new UnexpectedTypeException($timesheet, TimesheetEntity::class); } if ($timesheet->isRunning()) { return; } // one year is currently the maximum that can be logged (which is already not logically) // the database column could hold more data, but let's limit it here if ($timesheet->getDuration() > 31536000) { $this->context->buildViolation($constraint->maximumMessage) ->setTranslationDomain('validators') ->atPath('duration') ->setCode(TimesheetLongRunning::MAXIMUM) ->addViolation(); return; } $maxMinutes = $this->systemConfiguration->getTimesheetLongRunningDuration(); if ($maxMinutes <= 0) { return; } $duration = $timesheet->getEnd()->getTimestamp() - $timesheet->getBegin()->getTimestamp(); $minutes = (int) $duration / 60; if ($minutes < $maxMinutes) { return; } $format = new \App\Utils\Duration(); $hours = $format->format($maxMinutes * 60); // raise a violation for all entries before the start of lockdown period $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $hours) ->setTranslationDomain('validators') ->atPath('duration') ->setCode(TimesheetLongRunning::LONG_RUNNING) ->addViolation(); } }