11<?php
22namespace SparkPost ;
3- use Ivory \HttpAdapter \Configuration ;
4- use Ivory \HttpAdapter \HttpAdapterInterface ;
53use Ivory \HttpAdapter \HttpAdapterException ;
4+ use SparkPost \SparkPost ;
65
76/**
87 * @desc SDK interface for managing SparkPost API endpoints
@@ -29,111 +28,14 @@ class APIResource {
2928 */
3029 protected static $ structure = [];
3130
32- /**
33- * @dec connection config for making requests.
34- */
35- private $ config ;
36-
37- /**
38- * @desc Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
39- */
40- protected $ httpAdapter ;
41-
42- /**
43- * @desc Default config values. Passed in values will override these.
44- */
45- private static $ apiDefaults = [
46- 'host ' =>'api.sparkpost.com ' ,
47- 'protocol ' =>'https ' ,
48- 'port ' =>443 ,
49- 'strictSSL ' =>true ,
50- 'key ' =>'' ,
51- 'version ' =>'v1 '
52- ];
53-
5431 /**
5532 * @desc Initializes config and httpAdapter for use later.
56- * @param $httpAdapter Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
57- * @param $config connection config for making requests.
33+ * @param $sparkpost SparkPost\SparkPost provides api configuration information
5834 */
59- public function __construct ($ httpAdapter , $ config ) {
60- //config needs to be setup before adapter because of default adapter settings
61- $ this ->setConfig ($ config );
62- $ this ->setHttpAdapter ($ httpAdapter );
35+ public function __construct (SparkPost $ sparkpost ) {
36+ $ this ->sparkpost = $ sparkpost ;
6337 }
6438
65- /**
66- * @desc Merges passed in headers with default headers for http requests
67- * @return Array - headers to be set on http requests
68- */
69- private function getHttpHeaders (Array $ headers = null ) {
70- $ defaultOptions = [
71- 'Authorization ' => $ this ->config ['key ' ],
72- 'Content-Type ' => 'application/json ' ,
73- ];
74-
75- // Merge passed in headers with defaults
76- if (!is_null ($ headers )) {
77- foreach ($ headers as $ header => $ value ) {
78- $ defaultOptions [$ header ] = $ value ;
79- }
80- }
81- return $ defaultOptions ;
82- }
83-
84-
85- /**
86- * @desc Helper function for getting the configuration for http requests
87- * @return \Ivory\HttpAdapter\Configuration
88- */
89- private function getHttpConfig ($ config ) {
90- // get composer.json to extract version number
91- $ composerFile = file_get_contents (dirname (__FILE__ ) . "/../../composer.json " );
92- $ composer = json_decode ($ composerFile , true );
93-
94- // create Configuration for http adapter
95- $ httpConfig = new Configuration ();
96- $ baseUrl = $ config ['protocol ' ] . ':// ' . $ config ['host ' ] . ($ config ['port ' ] ? ': ' . $ config ['port ' ] : '' ) . '/api/ ' . $ config ['version ' ];
97- $ httpConfig ->setBaseUri ($ baseUrl );
98- $ httpConfig ->setUserAgent ('php-sparkpost/ ' . $ composer ['version ' ]);
99- return $ httpConfig ;
100- }
101-
102- /**
103- * @desc Validates and sets up the httpAdapter
104- * @param $httpAdapter Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
105- * @throws \Exception
106- */
107- public function setHttpAdapter ($ httpAdapter ) {
108- if (!$ httpAdapter instanceOf HttpAdapterInterface) {
109- throw new \Exception ('$httpAdapter paramter must be a valid Ivory\HttpAdapter ' );
110- }
111-
112- $ this ->httpAdapter = $ httpAdapter ;
113- $ this ->httpAdapter ->setConfiguration ($ this ->getHttpConfig ($ this ->config ));
114- }
115-
116- /**
117- * Allows the user to pass in values to override the defaults and set their API key
118- * @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost
119- * @throws \Exception
120- */
121- public function setConfig (Array $ settingsConfig ) {
122- // Validate API key because its required
123- if (!isset ($ settingsConfig ['key ' ]) || empty (trim ($ settingsConfig ['key ' ]))){
124- throw new \Exception ('You must provide an API key ' );
125- }
126-
127- $ this ->config = self ::$ apiDefaults ;
128-
129- // set config, overriding defaults
130- foreach ($ settingsConfig as $ configOption => $ configValue ) {
131- if (key_exists ($ configOption , $ this ->config )) {
132- $ this ->config [$ configOption ] = $ configValue ;
133- }
134- }
135- }
136-
13739 /**
13840 * @desc Private Method helper to reference parameter mappings and set the right value for the right parameter
13941 *
@@ -282,7 +184,7 @@ private function callResource( $action, $resourcePath=null, $options=[] ) {
282184
283185 //make request
284186 try {
285- $ response = $ this ->httpAdapter ->send ($ url , $ action , $ this ->getHttpHeaders (), $ body );
187+ $ response = $ this ->sparkpost -> httpAdapter ->send ($ url , $ action , $ this -> sparkpost ->getHttpHeaders (), $ body );
286188 return json_decode ($ response ->getBody ()->getContents (), true );
287189 }
288190 /*
0 commit comments