|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenapply\PeopleMatter; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
|
|
7
|
|
|
class PeopleMatter |
|
8
|
|
|
{ |
|
9
|
|
|
const V3 = 'v3'; |
|
10
|
|
|
|
|
11
|
|
|
protected $host; |
|
12
|
|
|
protected $version; |
|
13
|
|
|
protected $client; |
|
14
|
|
|
protected $token; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Creates a PeopleMatter instance that can register and unregister webhooks with the API |
|
18
|
|
|
* @param string $username The Username |
|
19
|
|
|
* @param string $password The Password |
|
20
|
|
|
* @param string $alias The business alias |
|
21
|
|
|
* @param string $host The host to connect to |
|
22
|
|
|
* @param Client|null $client The Guzzle client (used for testing) |
|
23
|
|
|
*/ |
|
24
|
3 |
|
public function __construct($username, $password, $alias, $host = "api.peoplematter.com", Client $client = null) |
|
25
|
|
|
{ |
|
26
|
3 |
|
$this->alias = $alias; |
|
|
|
|
|
|
27
|
3 |
|
$this->username = $username; |
|
|
|
|
|
|
28
|
3 |
|
$this->password = $password; |
|
|
|
|
|
|
29
|
3 |
|
$this->host = $host; |
|
30
|
3 |
|
$this->client = $client; |
|
31
|
3 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function hire() |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function login() |
|
39
|
|
|
{ |
|
40
|
|
|
$url = "https://{$this->host}/api/account/login"; |
|
41
|
|
|
return $this->request('POST', $url, [ |
|
42
|
|
|
'username' => $this->username, |
|
43
|
|
|
'password' => $this->password, |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns the Client instance |
|
49
|
|
|
* @return Client |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function getClient() |
|
52
|
|
|
{ |
|
53
|
|
|
$client = $this->client; |
|
54
|
|
|
if (!$client instanceof Client) { |
|
55
|
|
|
$client = new Client(); |
|
56
|
|
|
} |
|
57
|
|
|
return $client; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Executes a request to the PeopleMatter API |
|
62
|
|
|
* @param string $url The URL to send to |
|
63
|
|
|
* @return mixed The response data |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function request($method, $url, $data) |
|
66
|
|
|
{ |
|
67
|
|
|
$client = $this->getClient(); |
|
68
|
|
|
$response = $client->request($method, $url, ["data" => $data]); |
|
69
|
|
|
return $response->getBody(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
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: