start = new \DateTime('now'); $this->employee = $employee ?? new NullEmployee(); } /** * Get id. * * @return int Account id * * @since 1.0.0 */ public function getId() : int { return $this->id; } /** * Get employee. * * @return Employee * * @since 1.0.0 */ public function getEmployee() : Employee { return $this->employee; } /** * Add a session element to the session * * @param SessionElement $element Session element * * @return void * * @since 1.0.0 */ public function addSessionElement(SessionElement $element) : void { if ($element->getStatus() === ClockingStatus::START) { foreach ($this->sessionElements as $e) { if ($e->getStatus === ClockingStatus::START) { return; } } $this->start = $element->getDatetime(); } if ($element->getStatus() === ClockingStatus::END) { if ($this->end !== null) { return; } $this->end = $element->getDatetime(); } $this->sessionElements[] = $element; \usort($this->sessionElements, function($a, $b) { return $a->getDatetime()->getTimestamp() <=> $b->getDatetime()->getTimestamp(); }); $busyTime = 0; $lastStart = $this->start; foreach ($this->sessionElements as $e) { if ($e->getStatus() === ClockingStatus::START) { continue; } if ($e->getStatus() === ClockingStatus::PAUSE || $e->getStatus() === ClockingStatus::END) { $busyTime += $e->getDatetime()->getTimestamp() - $lastStart->getTimestamp(); } if ($e->getStatus() === ClockingStatus::CONTINUE) { $lastStart = $e->getDatetime(); } } $this->busy = $busyTime; } /** * Get all session elements * * @return SessionElement[] * * @since 1.0.0 */ public function getSessionElements() : array { return $this->sessionElements; } /** * Get the status of the last session element * * @return int * * @since 1.0.0 */ public function getStatus() : int { if (\count($this->sessionElements) === 0) { return ClockingStatus::START; } \usort($this->sessionElements, function($a, $b) { return $a->getDatetime()->getTimestamp() <=> $b->getDatetime()->getTimestamp(); }); $last = \end($this->sessionElements); \reset($this->sessionElements); return $last->getStatus(); } /** * Get the total break time of a session * * @return int * * @since 1.0.0 */ public function getBreak() : int { \usort($this->sessionElements, function($a, $b) { return $a->getDatetime()->getTimestamp() <=> $b->getDatetime()->getTimestamp(); }); $breakTime = 0; $lastBreak = $this->start; foreach ($this->sessionElements as $element) { if ($element->getStatus() === ClockingStatus::START) { continue; } if ($element->getStatus() === ClockingStatus::PAUSE || $element->getStatus() === ClockingStatus::END) { $lastBreak = $element->getDatetime(); } if ($element->getStatus() === ClockingStatus::CONTINUE) { $breakTime += $element->getDatetime()->getTimestamp() - ($lastBreak->getTimestamp() ?? 0); } } return $breakTime; } /** * Get busy time * * @return int * * @since 1.0.0 */ public function getBusy() : int { return $this->busy; } /** * Get the session type * * @return int * * @since 1.0.0 */ public function getType() : int { return $this->type; } /** * Set the session type * * @param int $type Session type * * @return void * * @since 1.0.0 */ public function setType(int $type) : void { $this->type = $type; } /** * Return session start * * @return \DateTime * * @since 1.0.0 */ public function getStart() : \DateTime { return $this->start; } /** * Return session end * * @return null|\DateTime * * @since 1.0.0 */ public function getEnd() : ?\DateTime { return $this->end; } /** * {@inheritdoc} */ public function toArray() : array { return [ 'id' => $this->id, 'start' => $this->start, 'end' => $this->end, 'busy' => $this->busy, 'type' => $this->type, 'employee' => $this->employee, 'elements' => $this->sessionElements, ]; } /** * {@inheritdoc} */ public function __toString() { return (string) \json_encode($this->toArray()); } /** * {@inheritdoc} */ public function jsonSerialize() { return $this->toArray(); } }