1: <?php
2: namespace Ctct\Services;
3:
4: use Ctct\Util\RestClient;
5: use Ctct\Util\RestClientInterface;
6:
7: /**
8: * Super class for all services
9: *
10: * @package Services
11: * @author Constant Contact
12: */
13: abstract class BaseService
14: {
15: /**
16: * RestClient Implementation to use for HTTP requests
17: * @var $restClient - RestClient
18: */
19: protected $restClient;
20:
21: /**
22: * ApiKey for the application
23: * @var string
24: */
25: protected $apiKey;
26:
27: /**
28: * Constructor with the option to to supply an alternative rest client to be used
29: * @param RestClientInterface - RestClientInterface implementation to be used in the service
30: */
31: public function __construct($apiKey, $restClient = null)
32: {
33: $this->apiKey = $apiKey;
34:
35: if (is_null($restClient)) {
36: $this->restClient = new RestClient();
37: } else {
38: $this->restClient = $restClient;
39: }
40: }
41:
42: /**
43: * Build a url from the base url and query parameters array
44: * @return string
45: */
46: public function buildUrl($url, $queryParams = null)
47: {
48: $keyArr = array('api_key' => $this->apiKey);
49: if ($queryParams) {
50: $params = array_merge($keyArr, $queryParams);
51: } else {
52: $params = $keyArr;
53: }
54:
55: return $url . '?' . http_build_query($params);
56: }
57:
58: /**
59: * Get the rest client being used by the service
60: * @return RestClientInterface - RestClientInterface implementation being used
61: */
62: public function getRestClient()
63: {
64: return $this->restClient;
65: }
66:
67: public function setRestClient(RestClientInterface $restClient)
68: {
69: $this->restClient = $restClient;
70: }
71:
72:
73: /**
74: * Helper function to return required headers for making an http request with constant contact
75: * @param $accessToken - OAuth2 access token to be placed into the Authorization header
76: * @return array - authorization headers
77: */
78: protected static function getHeaders($accessToken)
79: {
80: return array(
81: 'Content-Type: application/json',
82: 'Accept: application/json',
83: 'Authorization: Bearer ' . $accessToken
84: );
85: }
86: }
87: