|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenapply\PeopleMatter; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Zenapply\PeopleMatter\Exceptions\PeopleMatterException; |
|
7
|
|
|
use DateTime; |
|
8
|
|
|
|
|
9
|
|
|
class PeopleMatter |
|
10
|
|
|
{ |
|
11
|
|
|
const V3 = 'v3'; |
|
12
|
|
|
|
|
13
|
|
|
protected $host; |
|
14
|
|
|
protected $version; |
|
15
|
|
|
protected $client; |
|
16
|
|
|
protected $token; |
|
17
|
|
|
protected $authenticated = false; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Creates a PeopleMatter instance that can register and unregister webhooks with the API |
|
21
|
|
|
* @param string $username The Username |
|
22
|
|
|
* @param string $password The Password |
|
23
|
|
|
* @param string $alias The business alias |
|
24
|
|
|
* @param string $host The host to connect to |
|
25
|
|
|
* @param Client|null $client The Guzzle client (used for testing) |
|
26
|
|
|
*/ |
|
27
|
12 |
|
public function __construct($username, $password, $alias, $host = "api.peoplematter.com", Client $client = null) |
|
28
|
|
|
{ |
|
29
|
12 |
|
$this->alias = $alias; |
|
|
|
|
|
|
30
|
12 |
|
$this->username = $username; |
|
|
|
|
|
|
31
|
12 |
|
$this->password = $password; |
|
|
|
|
|
|
32
|
12 |
|
$this->host = $host; |
|
33
|
12 |
|
$this->client = $client; |
|
34
|
12 |
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
public function hire(Person $person, Job $job, BusinessUnit $businessUnit, DateTime $hired_at = null, $timeStatus = "FullTime") |
|
37
|
|
|
{ |
|
38
|
3 |
|
$this->login(); |
|
39
|
|
|
|
|
40
|
3 |
|
if ($hired_at === null) { |
|
41
|
|
|
$hired_at = new DateTime('now'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
$url = "https://{$this->host}/api/services/platform/hireemployee"; |
|
45
|
|
|
|
|
46
|
3 |
|
return $this->request('POST', $url, [ |
|
47
|
3 |
|
'debug' => true, |
|
48
|
|
|
'json' => [ |
|
49
|
3 |
|
"HireDate" => $hired_at->format("m/d/Y"), |
|
50
|
|
|
"Business" => [ |
|
51
|
|
|
"Alias" => "cafezupassandbox" |
|
52
|
3 |
|
], |
|
53
|
|
|
"BusinessUnit" => [ |
|
54
|
3 |
|
"UnitNumber" => $businessUnit->UnitNumber |
|
|
|
|
|
|
55
|
3 |
|
], |
|
56
|
3 |
|
"Person" => $person->toArray(), |
|
57
|
|
|
"JobPositions" => [ |
|
58
|
|
|
[ |
|
59
|
|
|
"Business" => [ |
|
60
|
|
|
"Alias" => "cafezupassandbox" |
|
61
|
3 |
|
], |
|
62
|
|
|
"BusinessUnit" => [ |
|
63
|
3 |
|
"UnitNumber" => $businessUnit->UnitNumber |
|
64
|
3 |
|
], |
|
65
|
|
|
"Job" => [ |
|
66
|
3 |
|
"Code" => $job->Code, |
|
|
|
|
|
|
67
|
3 |
|
], |
|
68
|
3 |
|
"TimeStatus" => $timeStatus, |
|
69
|
3 |
|
"Person" => $person->toArray(), |
|
70
|
|
|
] |
|
71
|
3 |
|
] |
|
72
|
3 |
|
] |
|
73
|
3 |
|
]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
View Code Duplication |
public function getBusinessUnits() |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
3 |
|
$this->login(); |
|
79
|
3 |
|
$units = []; |
|
80
|
3 |
|
$url = "https://{$this->host}/api/businessunit?businessalias={$this->alias}"; |
|
81
|
3 |
|
$response = $this->request('GET', $url); |
|
82
|
|
|
|
|
83
|
3 |
|
foreach ($response["Records"] as $unit) { |
|
84
|
3 |
|
$units[] = new BusinessUnit($unit); |
|
85
|
3 |
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
return $units; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// public function getPerson($email) |
|
|
|
|
|
|
91
|
|
|
// { |
|
92
|
|
|
// if (empty($email)) { |
|
|
|
|
|
|
93
|
|
|
// throw new \Exception("Email is invalid!"); |
|
|
|
|
|
|
94
|
|
|
// } |
|
95
|
|
|
// $this->login(); |
|
|
|
|
|
|
96
|
|
|
// $units = []; |
|
|
|
|
|
|
97
|
|
|
// $url = "https://{$this->host}/api/employees/list"; |
|
|
|
|
|
|
98
|
|
|
// $response = $this->request('GET', $url); |
|
|
|
|
|
|
99
|
|
|
// var_dump($response); |
|
|
|
|
|
|
100
|
|
|
// foreach ($response["Records"] as $unit) { |
|
|
|
|
|
|
101
|
|
|
// $units[] = new BusinessUnit($unit); |
|
|
|
|
|
|
102
|
|
|
// } |
|
103
|
|
|
|
|
104
|
|
|
// return $units; |
|
105
|
|
|
// } |
|
106
|
|
|
|
|
107
|
3 |
View Code Duplication |
public function getJobs() |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
3 |
|
$this->login(); |
|
110
|
3 |
|
$jobs = []; |
|
111
|
3 |
|
$url = "https://{$this->host}/api/job?businessalias={$this->alias}"; |
|
112
|
3 |
|
$response = $this->request('GET', $url); |
|
113
|
|
|
|
|
114
|
3 |
|
foreach ($response["Jobs"] as $unit) { |
|
115
|
3 |
|
$jobs[] = new Job($unit); |
|
116
|
3 |
|
} |
|
117
|
|
|
|
|
118
|
3 |
|
return $jobs; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
9 |
|
protected function login() |
|
122
|
|
|
{ |
|
123
|
9 |
|
if ($this->authenticated !== true) { |
|
124
|
9 |
|
$url = "https://{$this->host}/api/account/login"; |
|
125
|
9 |
|
$this->request('POST', $url, [ |
|
126
|
1 |
|
'form_params' => [ |
|
127
|
9 |
|
'email' => $this->username, |
|
128
|
9 |
|
'password' => $this->password, |
|
129
|
|
|
] |
|
130
|
9 |
|
]); |
|
131
|
9 |
|
$this->authenticated = true; |
|
132
|
9 |
|
} |
|
133
|
|
|
|
|
134
|
9 |
|
return $this->authenticated; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns the Client instance |
|
139
|
|
|
* @return Client |
|
140
|
|
|
*/ |
|
141
|
9 |
|
public function getClient() |
|
142
|
|
|
{ |
|
143
|
9 |
|
if (!$this->client instanceof Client) { |
|
144
|
|
|
$this->client = new Client([ |
|
145
|
|
|
'cookies' => true |
|
146
|
|
|
]); |
|
147
|
|
|
} |
|
148
|
9 |
|
return $this->client; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Executes a request to the PeopleMatter API |
|
153
|
|
|
* @param string $method The request type |
|
154
|
|
|
* @param string $url The url to request |
|
155
|
|
|
* @param array $options An array of options for the request |
|
156
|
|
|
* @return array The response as an array |
|
157
|
|
|
*/ |
|
158
|
9 |
|
protected function request($method, $url, $options = []) |
|
159
|
|
|
{ |
|
160
|
9 |
|
$client = $this->getClient(); |
|
161
|
|
|
try { |
|
162
|
9 |
|
$response = $client->request($method, $url, $options); |
|
163
|
9 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
|
164
|
3 |
|
$response = $e->getResponse(); |
|
165
|
3 |
|
throw new PeopleMatterException($response->getStatusCode().": ".$response->getReasonPhrase(), 1); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
9 |
|
$json = json_decode($response->getBody(), true); |
|
169
|
|
|
|
|
170
|
9 |
|
if (!empty($json["ErrorMessage"])) { |
|
171
|
|
|
throw new PeopleMatterException($json["ErrorMessage"], $json["ErrorCode"]); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
9 |
|
return $json; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: