TimelogRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the HRis Software package.
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * Licensed under the 3-clause BSD License.
9
 *
10
 * This source file is subject to the 3-clause BSD License that is
11
 * bundled with this package in the LICENSE file.
12
 *
13
 * @version    alpha
14
 *
15
 * @author     Bertrand Kintanar <[email protected]>
16
 * @license    BSD License (3-clause)
17
 * @copyright  (c) 2014-2016, b8 Studios, Ltd
18
 *
19
 * @link       http://github.com/HB-Co/HRis
20
 */
21
22
namespace HRis\Api\Repositories\Time;
23
24
use HRis\Api\Repositories\Repository;
25
use Irradiate\Eloquent\Employee;
26
use Irradiate\Eloquent\Timelog;
27
28
class TimelogRepository extends Repository
29
{
30
    /**
31
     * TimelogRepository constructor.
32
     *
33
     * @author Harlequin Doyon
34
     */
35 6
    public function __construct()
36
    {
37 6
        parent::__construct(new Timelog());
38 6
    }
39
40
    /**
41
     * Pagination.
42
     *
43
     * @param int    $id
44
     * @param string $sort
45
     * @param string $direction
46
     *
47
     * @return \Illuminate\Database\Eloquent\Collection|static[]
48
     *
49
     * @author Harlequin Doyon
50
     */
51
    public function paginate($id = null, $sort = 'id', $direction = 'asc')
52
    {
53
        if (is_null($id)) {
54
            return $this->model->paginate(ROWS_PER_PAGE);
55
        }
56
57
        return $this->model->whereEmployeeId($id)
58
            ->orderBy($sort, $direction)
59
            ->paginate(ROWS_PER_PAGE);
60
    }
61
62
    /**
63
     * Check the latest timelog if user has no time in.
64
     *
65
     * @param Employee $employee
66
     *
67
     * @return \Illuminate\Database\Eloquent\Model
68
     *
69
     * @author Harlequin Doyon
70
     */
71 2
    public function hasNoLatestTimein(Employee $employee)
72
    {
73 2
        $timelog = $this->latest($employee);
74
75 2
        if (!isset($timelog) || (!empty($timelog->in) && !empty($timelog->out))) {
76 2
            return true;
77
        }
78
79
        return false;
80
    }
81
82
    /**
83
     * Fetch the latest record of the table.
84
     *
85
     * @param Employee $employee
86
     *
87
     * @return \Illuminate\Database\Eloquent\Model
88
     *
89
     * @author Harlequin Doyon
90
     */
91 2
    public function latest(Employee $employee)
92
    {
93 2
        return $this->model->where('employee_id', $employee->id)->orderBy('created_at', 'desc')->first();
94
    }
95
96
    /**
97
     * Check the latest timelog if user has no time out.
98
     *
99
     * @param Employee $employee
100
     *
101
     * @return bool
102
     *
103
     * @author Harlequin Doyon
104
     */
105
    public function hasNoLatestTimeout(Employee $employee)
106
    {
107
        $timelog = $this->latest($employee);
108
109
        if (isset($timelog) && is_null($timelog->out)) {
110
            return true;
111
        }
112
113
        return false;
114
    }
115
116
    /**
117
     * Returns timelog in a given range.
118
     *
119
     * @param $start
120
     * @param $end
121
     * @param $employee_id
122
     * @param $rows_per_page
123
     *
124
     * @return mixed
125
     */
126
    public function range($start, $end, $employee_id, $rows_per_page)
127
    {
128
        return $this->model
129
            ->whereBetween('in', [$start, $end])
130
            ->where('employee_id', $employee_id)
131
            ->select('id', 'employee_id', 'in', 'out', 'rendered_hours', 'created_at')
132
            ->orderBy('id', 'desc')
133
            ->paginate($rows_per_page);
134
    }
135
}
136