|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenapply\PeopleMatter; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use Zenapply\PeopleMatter\Exceptions\PeopleMatterException; |
|
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
|
6 |
|
protected function buildUrl($resource) |
|
77
|
|
|
{ |
|
78
|
6 |
|
return "https://{$this->host}/api/{$resource}"; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
View Code Duplication |
public function getBusinessUnits() |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
3 |
|
$this->login(); |
|
84
|
3 |
|
$response = $this->request('GET', $this->buildUrl("businessunit?businessalias={$this->alias}")); |
|
85
|
|
|
|
|
86
|
3 |
|
$units = []; |
|
87
|
3 |
|
foreach ($response["Records"] as $unit) { |
|
88
|
3 |
|
$units[] = new BusinessUnit($unit); |
|
89
|
3 |
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
return $units; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
View Code Duplication |
public function getJobs() |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
3 |
|
$this->login(); |
|
97
|
3 |
|
$response = $this->request('GET', $this->buildUrl("job?businessalias={$this->alias}")); |
|
98
|
|
|
|
|
99
|
3 |
|
$jobs = []; |
|
100
|
3 |
|
foreach ($response["Jobs"] as $unit) { |
|
101
|
3 |
|
$jobs[] = new Job($unit); |
|
102
|
3 |
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
return $jobs; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
9 |
|
protected function login() |
|
108
|
|
|
{ |
|
109
|
9 |
|
if ($this->authenticated !== true) { |
|
110
|
9 |
|
$url = "https://{$this->host}/api/account/login"; |
|
111
|
9 |
|
$this->request('POST', $url, [ |
|
112
|
|
|
'form_params' => [ |
|
113
|
9 |
|
'email' => $this->username, |
|
114
|
9 |
|
'password' => $this->password, |
|
115
|
|
|
] |
|
116
|
9 |
|
]); |
|
117
|
9 |
|
$this->authenticated = true; |
|
118
|
9 |
|
} |
|
119
|
|
|
|
|
120
|
9 |
|
return $this->authenticated; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns the Client instance |
|
125
|
|
|
* @return Client |
|
126
|
|
|
*/ |
|
127
|
9 |
|
public function getClient() |
|
128
|
2 |
|
{ |
|
129
|
9 |
|
if (!$this->client instanceof Client) { |
|
130
|
|
|
$this->client = new Client([ |
|
131
|
|
|
'cookies' => true |
|
132
|
|
|
]); |
|
133
|
|
|
} |
|
134
|
9 |
|
return $this->client; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Executes a request to the PeopleMatter API |
|
139
|
|
|
* @param string $method The request type |
|
140
|
|
|
* @param string $url The url to request |
|
141
|
|
|
* @param array $options An array of options for the request |
|
142
|
|
|
* @return array The response as an array |
|
143
|
|
|
*/ |
|
144
|
9 |
|
protected function request($method, $url, $options = []) |
|
145
|
|
|
{ |
|
146
|
9 |
|
$client = $this->getClient(); |
|
147
|
|
|
try { |
|
148
|
9 |
|
$response = $client->request($method, $url, $options); |
|
149
|
9 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
|
150
|
3 |
|
$response = $e->getResponse(); |
|
151
|
3 |
|
throw new PeopleMatterException($response->getStatusCode().": ".$response->getReasonPhrase(), 1); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
9 |
|
$json = json_decode($response->getBody(), true); |
|
155
|
|
|
|
|
156
|
9 |
|
if (!empty($json["ErrorMessage"])) { |
|
157
|
|
|
throw new PeopleMatterException($json["ErrorMessage"], $json["ErrorCode"]); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
9 |
|
return $json; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
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: