-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBambooHRAbsences.php
161 lines (142 loc) · 4.66 KB
/
BambooHRAbsences.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace BambooHRConnector;
/**
* Class BambooHRAbsences
*
* @package BambooHRConnector
* @author Georgi Damyanov <[email protected]>
*/
class BambooHRAbsences
{
/**
* Instance of SendMail
*
* @var $sendmail SendMail
*/
private $sendmail;
/**
* Instance of Logger
*
* @var $logger Logger
*/
private $logger;
/**
* The employee data
*
* @var $employeesXML \SimpleXMLElement
*/
private $employeesXML;
/**
* Instance of BambooAPI
*
* @var $bambooAPI BambooAPI
*/
private $bambooAPI;
/**
* Instance of Config
*
* @var $config Config
*/
private $config;
/**
* The employee absences
*
* @var $absencesArray array
*/
private $absencesArray = [];
/**
* The number of employees
*
* @var $employeeCounter int
*/
private $employeeCounter;
/**
* BambooHRAbsences constructor.
*
* @param Config $config Config Instance
* @param BambooAPI $bambooAPI BambooAPI Instance
* @param Logger $logger Logger Instance
* @param SendMail $sendmail SendMail Instance
*/
public function __construct(Config $config, BambooAPI $bambooAPI, Logger $logger, SendMail $sendmail)
{
$this->config = $config;
$this->bambooAPI = $bambooAPI;
$this->logger = $logger;
$this->sendmail = $sendmail;
}
/**
* Function getBambooHRAbsences()
*
* Fetch the absences from BambooHR
* - Go through all the employees
* - Get all the absences (with set date) for all the employees
*
* @return array Returns an array
*/
public function getBambooHRAbsences(): array
{
$this->bambooAPI->setSecretKey($this->config->getConfig('bamboohr', 'api_token'));
$employeeDirectoryRequestArray = $this->bambooAPI->getDirectory();
$httpcode = $employeeDirectoryRequestArray->statusCode;
if ($httpcode < 200 || $httpcode > 299) {
$this->logger->writeErrorToFile('Connection to BambooHR could not be established. Error Code: '.$httpcode);
}
$this->employeesXML = $employeeDirectoryRequestArray->getContentXML();
$this->employeeCounter = $this->getNumberOfEmployees($this->employeesXML);
while ($this->employeeCounter > -1) {
$employeeExternalID = $this->getEmployeeExternalID($this->employeesXML, $this->employeeCounter);
$timeOffRequestsArray = $this->bambooAPI
->getTimeOffRequests($this->config->getConfig('bamboohr', 'date'), $this->config
->getConfig('bamboohr', 'date'), $this->config
->getConfig('bamboohr', 'status'), '', $employeeExternalID);
$timeOffRequestsXML = $timeOffRequestsArray->getContentXML();
for ($i = 0; $i < sizeof($timeOffRequestsXML->request); $i++) {
$absence = [
'id' => $employeeExternalID,
'start' => $this->config->getConfig('bamboohr', 'date'),
'end' => $this->config->getConfig('bamboohr', 'date'),
'type' => (string) $timeOffRequestsXML->request[$i]->type,
'unit' => (string) $timeOffRequestsXML->request[$i]->amount->attributes()['unit'],
'amount' => (string) $timeOffRequestsXML->request[$i]->amount,
'full_day' => false
];
if ($this->config->getConfig('bamboohr', 'filter_enabled') === false ||
!in_array($absence['type'], $this->config->getConfig('bamboohr', 'filter_array'))
) {
$this->absencesArray[] = $absence;
}
}
$this->employeeCounter--;
}
return $this->absencesArray;
}
/**
* Function getNumberOfEmployees
*
* Get the number of employees
*
* @param \SimpleXMLElement $simplexml Array with the employees
*
* @return int returns (number of employees - 1) (because count starts from 0)
*/
private function getNumberOfEmployees($simplexml): int
{
return count($simplexml->employees->employee)-1;
}
/**
* Function getEmployeeExternalID
*
* Get the external ID of an employee
*
* @param \SimpleXMLElement $simplexml Array with the employees
* @param int $index employee counter
*
* @return int returns the externalID of an employee
*/
private function getEmployeeExternalID($simplexml, $index): int
{
return (int) $simplexml->employees->employee[$index]->attributes()['id'];
}
}