Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
Wheniwork
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 14
600
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 setToken
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setEndpoint
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getEndpoint
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setHeaders
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getHeaders
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 post
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 delete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeRequest
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
72
 login
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Client library for the When I Work scheduling and attendance platform.
5 *
6 * Uses curl to request JSON responses from the When I Work API.
7 * This probably has more comments than code.
8 *
9 * Contributors:
10 * Daniel Olfelt <daniel@thisclicks.com>
11 *
12 * @author Daniel Olfelt <daniel@thisclicks.com>
13 * @version 0.1
14 */
15class Wheniwork {
16
17    const METHOD_GET = 'get';
18    const METHOD_POST = 'post';
19    const METHOD_PUT = 'put';
20    const METHOD_PATCH = 'patch';
21    const METHOD_DELETE = 'delete';
22
23    private $api_key;
24    private $api_token;
25    private $api_endpoint = 'https://api.wheniwork.com/2';
26    private $api_headers = [];
27    private $verify_ssl   = false;
28
29    /**
30     * Create a new instance
31     * @param string $api_token The user WhenIWork API token
32     * @param array $options Allows you to set the `headers` and the `endpoint`
33     */
34    function __construct($api_token = null, $options = [])
35    {
36        $this->api_token = $api_token;
37
38        if (!empty($options['endpoint'])) {
39          $this->setEndpoint($options['endpoint']);
40        }
41        if (!empty($options['headers'])) {
42          $this->setHeaders($options['headers'], true);
43        }
44    }
45
46    /**
47     * Set the user token for all requests
48     * @param string $api_token The user WhenIWork API token
49     * @return Wheniwork
50     */
51    public function setToken($api_token) {
52        $this->api_token = $api_token;
53        return $this;
54    }
55
56    /**
57     * Get the user token to save for future requests
58     * @return string The user WhenIWork API token
59     */
60    public function getToken() {
61        return $this->api_token;
62    }
63
64    /**
65    * Set the endpoint for all requests
66    * @param string $endpoint The WhenIWork API endpoint to use
67    * @return Wheniwork
68    */
69    public function setEndpoint($endpoint) {
70      $this->api_endpoint = $endpoint;
71      return $this;
72    }
73
74    /**
75    * Get the endpoint to use for future requests
76    * @return string The WhenIWork API endpoint
77    */
78    public function getEndpoint() {
79      return $this->api_endpoint;
80    }
81
82    /**
83    * Set the host for all requests
84    * @param array $headers Global headers for all future requests
85    * @return Wheniwork
86    */
87    public function setHeaders(array $headers, $reset = false) {
88      if ($reset === true) {
89        $this->api_headers = $headers;
90      }
91      else {
92        $this->api_headers += $headers;
93      }
94
95      return $this;
96    }
97
98    /**
99    * Get the host to use for future requests
100    * @return array Global headers array
101    */
102    public function getHeaders() {
103      return $this->api_headers;
104    }
105
106    /**
107     * Get an object or list.
108     * @param  string $method  The API method to call, e.g. '/users/'
109     * @param  array  $params  An array of arguments to pass to the method.
110     * @param  array  $headers Array of custom headers to be passed
111     * @return array           Object of json decoded API response.
112     */
113    public function get($method, $params = array(), $headers = array()) {
114        return $this->makeRequest($method, self::METHOD_GET, $params, $headers);
115    }
116
117    /**
118     * Post to an endpoint.
119     * @param  string $method  The API method to call, e.g. '/shifts/publish/'
120     * @param  array  $params  An array of data used to create the object.
121     * @param  array  $headers Array of custom headers to be passed
122     * @return array           Object of json decoded API response.
123     */
124    public function post($method, $params = array(), $headers = array()) {
125        return $this->makeRequest($method, self::METHOD_POST, $params, $headers);
126    }
127
128    /**
129     * Create an object. Helper method for post.
130     * @param  string $method  The API method to call, e.g. '/users/'
131     * @param  array  $params  An array of data used to create the object.
132     * @param  array  $headers Array of custom headers to be passed
133     * @return array           Object of json decoded API response.
134     */
135    public function create($method, $params = array(), $headers = array()) {
136        return $this->post($method, $params, $headers);
137    }
138
139    /**
140     * Update an object. Must include the ID.
141     * @param  string $method  The API method to call, e.g. '/users/1'
142     * @param  array  $params  An array of data to update the object. Only changed fields needed.
143     * @param  array  $headers Array of custom headers to be passed
144     * @return array           Object of json decoded API response.
145     */
146    public function update($method, $params = array(), $headers = array()) {
147        return $this->makeRequest($method, self::METHOD_PUT, $params, $headers);
148    }
149
150    /**
151     * Delete an object. Must include the ID.
152     * @param  string $method  The API method to call, e.g. '/users/1'
153     * @param  array  $params  An array of arguments to pass to the method.
154     * @param  array  $headers Array of custom headers to be passed
155     * @return array           Object of json decoded API response.
156     */
157    public function delete($method, $params = array(), $headers = array()) {
158        return $this->makeRequest($method, self::METHOD_DELETE, $params, $headers);
159    }
160
161
162    /**
163     * Performs the underlying HTTP request. Exciting stuff happening here. Not really.
164     * @param  string $method  The API method to be called
165     * @param  string $request The type of request
166     * @param  array  $params  Assoc array of parameters to be passed
167     * @param  array  $headers Assoc array of custom headers to be passed
168     * @return array           Assoc array of decoded result
169     */
170    private function makeRequest($method, $request, $params = array(), $headers = array()) {
171
172        $url = $this->getEndpoint().'/'.$method;
173
174        if ($params && ($request == self::METHOD_GET || $request == self::METHOD_DELETE)) {
175            $url .= '?'.http_build_query($params);
176        }
177
178        $ch = curl_init();
179        curl_setopt($ch, CURLOPT_URL, $url);
180        curl_setopt($ch, CURLOPT_USERAGENT, 'WhenIWork-PHP/0.1');
181
182        $headers += $this->getHeaders();
183
184        $headers['Content-Type'] = 'application/json';
185        if ($this->api_token) {
186            $headers['W-Token'] = $this->api_token;
187        }
188
189        $header_data = array();
190        foreach ($headers as $k=>$v) {
191            $headers_data[] = $k.': '.$v;
192        }
193        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_data);
194
195        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($request));
196        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
197        curl_setopt($ch, CURLOPT_HEADER, false);
198        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
199        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
200        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
201
202        if (in_array($request, array(self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH))) {
203            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
204        }
205
206        $result = curl_exec($ch);
207        curl_close($ch);
208
209        return $result ? json_decode($result) : false;
210    }
211
212
213
214    /**
215     * Login helper using developer key and credentials to get back a login response
216     * @param  $key      Developer API key
217     * @param  $email    Email of the user logging in
218     * @param  $password Password of the user
219     * @return
220     */
221    public static function login($key, $email, $password) {
222        $params = array(
223            "username" => $email,
224            "password" => $password
225        );
226        $headers = array(
227            'W-Key' => $key
228        );
229
230        $login = new static();
231        $response = $login->makeRequest("login", self::METHOD_POST, $params, $headers);
232
233        return $response;
234    }
235
236}