Completed
Push — api/develop ( ceb0fc...52cb6e )
by Bertrand
25:03
created

TimelogRepository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 108
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A paginate() 0 10 2
A hasNoLatestTimein() 0 10 4
A latest() 0 4 1
A hasNoLatestTimeout() 0 10 3
A range() 0 9 1
1
<?php
2
3
/**
4
 * This file is part of the HRis Software package.
5
 *
6
 * HRis - Human Resource and Payroll System
7
 *
8
 * @link http://github.com/HB-Co/HRis
9
 */
10
namespace HRis\Api\Repositories\Time;
11
12
use HRis\Api\Eloquent\Employee;
13
use HRis\Api\Eloquent\Timelog;
14
use HRis\Api\Repositories\Repository;
15
16
class TimelogRepository extends Repository
17
{
18
    /**
19
     * TimelogRepository constructor.
20
     *
21
     * @author Harlequin Doyon
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct(new Timelog());
26
    }
27
28
    /**
29
     * Pagination.
30
     *
31
     * @param int    $id
32
     * @param string $sort
33
     * @param string $direction
34
     *
35
     * @return \Illuminate\Database\Eloquent\Collection|static[]
36
     *
37
     * @author Harlequin Doyon
38
     */
39
    public function paginate($id = null, $sort = 'id', $direction = 'asc')
40
    {
41
        if (is_null($id)) {
42
            return $this->model->paginate(ROWS_PER_PAGE);
43
        }
44
45
        return $this->model->whereEmployeeId($id)
46
            ->orderBy($sort, $direction)
47
            ->paginate(ROWS_PER_PAGE);
48
    }
49
50
    /**
51
     * Check the latest timelog if user has no time in.
52
     *
53
     * @param Employee $employee
54
     *
55
     * @return \Illuminate\Database\Eloquent\Model
56
     *
57
     * @author Harlequin Doyon
58
     */
59
    public function hasNoLatestTimein(Employee $employee)
60
    {
61
        $timelog = $this->latest($employee);
62
63
        if (!isset($timelog) || (!empty($timelog->in) && !empty($timelog->out))) {
64
            return true;
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * Fetch the latest record of the table.
72
     *
73
     * @param Employee $employee
74
     *
75
     * @return \Illuminate\Database\Eloquent\Model
76
     *
77
     * @author Harlequin Doyon
78
     */
79
    public function latest(Employee $employee)
80
    {
81
        return $this->model->where('employee_id', $employee->id)->orderBy('created_at', 'desc')->first();
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<HRis\Api\Eloquent\Employee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
    }
83
84
    /**
85
     * Check the latest timelog if user has no time out.
86
     *
87
     * @param Employee $employee
88
     *
89
     * @return bool
90
     *
91
     * @author Harlequin Doyon
92
     */
93
    public function hasNoLatestTimeout(Employee $employee)
94
    {
95
        $timelog = $this->latest($employee);
96
97
        if (isset($timelog) && is_null($timelog->out)) {
98
            return true;
99
        }
100
101
        return false;
102
    }
103
104
    /**
105
     * Returns timelog in a given range.
106
     *
107
     * @param $start
108
     * @param $end
109
     * @param $employee_id
110
     * @param $rows_per_page
111
     *
112
     * @return mixed
113
     */
114
    public function range($start, $end, $employee_id, $rows_per_page)
115
    {
116
        return $this->model
117
            ->whereBetween('in', [$start, $end])
118
            ->where('employee_id', $employee_id)
119
            ->select('id', 'employee_id', 'in', 'out', 'rendered_hours', 'created_at')
120
            ->orderBy('id', 'desc')
121
            ->paginate($rows_per_page);
122
    }
123
}
124