HoursWorkedCalculator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 36
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A calculateHoursWorkedInWeek() 0 11 1
A getWeekStartAndEndDates() 0 11 1
1
<?php
2
3
namespace Scheduler\Domain\Model\Shift;
4
5
use DateTimeInterface;
6
use DateTimeImmutable;
7
use Scheduler\Domain\Model\User\User;
8
9
class HoursWorkedCalculator
10
{
11
    const DAY_OF_WEEK_START = 1;
12
    const DAY_OF_WEEK_END = 7;
13
14
    private $shiftMapper;
15
16
    public function __construct(ShiftMapper $shiftMapper)
17
    {
18
        $this->shiftMapper = $shiftMapper;
19
    }
20
21
    public function calculateHoursWorkedInWeek(User $employee, DateTimeInterface $weekOf)
22
    {
23
        list($start, $end) = $this->getWeekStartAndEndDates($weekOf);
24
25
        $shifts = $this->shiftMapper->findShiftsInTimePeriodByEmployeeId($start, $end, $employee->getId());
0 ignored issues
show
Security Bug introduced by
It seems like $start defined by $this->getWeekStartAndEndDates($weekOf) on line 23 can also be of type false; however, Scheduler\Domain\Model\S...imePeriodByEmployeeId() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $end defined by $this->getWeekStartAndEndDates($weekOf) on line 23 can also be of type false; however, Scheduler\Domain\Model\S...imePeriodByEmployeeId() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
26
        $hours = array_reduce($shifts, function ($total, $shift) {
27
            return $total + $shift->getHours();
28
        });
29
30
        return new HoursWorkedSummary($start, $end, $hours);
0 ignored issues
show
Security Bug introduced by
It seems like $start defined by $this->getWeekStartAndEndDates($weekOf) on line 23 can also be of type false; however, Scheduler\Domain\Model\S...dSummary::__construct() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $end defined by $this->getWeekStartAndEndDates($weekOf) on line 23 can also be of type false; however, Scheduler\Domain\Model\S...dSummary::__construct() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
31
    }
32
33
    private function getWeekStartAndEndDates(DateTimeInterface $weekOf)
34
    {
35
        $year = $weekOf->format('Y');
36
        $week = $weekOf->format('W');
37
        $date = new DateTimeImmutable("00:00:00");
38
39
        $start = $date->setISODate($year, $week, self::DAY_OF_WEEK_START);
40
        $end = $date->setISODate($year, $week, self::DAY_OF_WEEK_END);
41
42
        return [$start, $end];
43
    }
44
}
45