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: * @var $rest_client - RestClient
17: */
18: public static $rest_client;
19:
20: /**
21: * ApiKey for the application
22: * @var string
23: */
24: protected $apiKey;
25:
26: /**
27: * Constructor with the option to to supply an alternative rest client to be used
28: * @param RestClientInterface - RestClientInterface implementation to be used in the service
29: */
30: public function __construct($apiKey, $rest_client = null)
31: {
32: $this->apiKey = $apiKey;
33:
34: if (is_null($rest_client)) {
35: self::$rest_client = new RestClient();
36: } else {
37: self::$rest_client = $rest_client;
38: }
39: }
40:
41: /**
42: * Build a url from the base url and query parameters array
43: * @return string
44: */
45: public function buildUrl($url, $queryParams = null)
46: {
47: $keyArr = array('api_key' => $this->apiKey);
48: if ($queryParams) {
49: $params = array_merge($keyArr, $queryParams);
50: } else {
51: $params = $keyArr;
52: }
53:
54: return $url . '?' . http_build_query($params);
55: }
56:
57: /**
58: * Get the rest client being used by the service
59: * @return RestClientInterface - RestClientInterface implementation being used
60: */
61: public static function getRestClient()
62: {
63: if (is_null(self::$rest_client)) {
64: return new RestClient();
65: } else {
66: return self::$rest_client;
67: }
68: }
69:
70:
71: /**
72: * Helper function to return required headers for making an http request with constant contact
73: * @param $accessToken - OAuth2 access token to be placed into the Authorization header
74: * @return array - authorization headers
75: */
76: protected static function getHeaders($accessToken)
77: {
78: return array(
79: 'Content-Type: application/json',
80: 'Accept: application/json',
81: 'Authorization: Bearer ' . $accessToken
82: );
83: }
84: }
85: