77use Swader \Diffbot \Api \Image ;
88use Swader \Diffbot \Api \Analyze ;
99use Swader \Diffbot \Api \Article ;
10+ use GuzzleHttp \Client ;
11+ use Swader \Diffbot \Factory \Entity ;
12+ use Swader \Diffbot \Interfaces \EntityFactory ;
1013
1114/**
1215 * Class Diffbot
1821class Diffbot
1922{
2023 /** @var string The API access token */
21- private static $ token = null ;
24+ protected static $ token = null ;
2225
2326 /** @var string The instance token, settable once per new instance */
24- private $ instanceToken ;
27+ protected $ instanceToken ;
28+
29+ /** @var Client The HTTP clients to perform requests with */
30+ protected $ client ;
31+
32+ /** @var EntityFactory The Factory which created Entities from Responses */
33+ protected $ factory ;
2534
2635 /**
2736 * @param string|null $token The API access token, as obtained on diffbot.com/dev
@@ -72,6 +81,54 @@ public function getToken()
7281 return ($ this ->instanceToken ) ? $ this ->instanceToken : self ::$ token ;
7382 }
7483
84+ /**
85+ * Sets the client to be used for querying the API endpoints
86+ *
87+ * @param Client $client
88+ * @return $this
89+ */
90+ public function setHttpClient (Client $ client = null )
91+ {
92+ if ($ client === null ) {
93+ $ client = new Client ();
94+ }
95+ $ this ->client = $ client ;
96+ return $ this ;
97+ }
98+
99+ /**
100+ * Returns either the instance of the Guzzle client that has been defined, or null
101+ * @return Client|null
102+ */
103+ public function getHttpClient ()
104+ {
105+ return $ this ->client ;
106+ }
107+
108+ /**
109+ * Sets the Entity Factory which will create the Entities from Responses
110+ * @param EntityFactory $factory
111+ * @return $this
112+ */
113+ public function setEntityFactory (EntityFactory $ factory = null )
114+ {
115+ if ($ factory === null ) {
116+ $ factory = new Entity ();
117+ }
118+ $ this ->factory = $ factory ;
119+ return $ this ;
120+ }
121+
122+ /**
123+ * Returns the Factory responsible for creating Entities from Responses
124+ * @return EntityFactory
125+ */
126+ public function getEntityFactory ()
127+ {
128+ return $ this ->factory ;
129+ }
130+
131+
75132 /**
76133 * Creates a Product API interface
77134 *
@@ -80,40 +137,57 @@ public function getToken()
80137 */
81138 public function createProductAPI ($ url )
82139 {
83- return new Product ($ url );
140+ $ api = new Product ($ url );
141+ if (!$ this ->getHttpClient ()) {
142+ $ this ->setHttpClient ();
143+ }
144+ return $ api ->registerDiffbot ($ this );
84145 }
85146
86147 /**
87148 * Creates an Article API interface
88149 *
89150 * @param $url string Url to analyze
90- * @return Product
151+ * @return Article
91152 */
92153 public function createArticleAPI ($ url )
93154 {
94- return new Article ($ url );
155+ $ api = new Article ($ url );
156+ if (!$ this ->getHttpClient ()) {
157+ $ this ->setHttpClient ();
158+ }
159+ return $ api ->registerDiffbot ($ this );
95160 }
96161
97162 /**
98163 * Creates an Image API interface
99164 *
100165 * @param $url string Url to analyze
101- * @return Product
166+ * @return Image
102167 */
103168 public function createImageAPI ($ url )
104169 {
105- return new Image ($ url );
170+ $ api = new Image ($ url );
171+ if (!$ this ->getHttpClient ()) {
172+ $ this ->setHttpClient ();
173+ }
174+ return $ api ->registerDiffbot ($ this );
106175 }
107176
108177 /**
109178 * Creates an Analyze API interface
110179 *
111180 * @param $url string Url to analyze
112- * @return Product
181+ * @return Analyze
113182 */
114183 public function createAnalyzeAPI ($ url )
115184 {
116- return new Analyze ($ url );
185+ $ api = new Analyze ($ url );
186+ if (!$ this ->getHttpClient ()) {
187+ $ this ->setHttpClient ();
188+ }
189+ return $ api ->registerDiffbot ($ this );
117190 }
118191
192+
119193}
0 commit comments