11<?php
22/**
3- * API Client
3+ * API Client.
44 *
5- * @package DatingVIP
6- * @subpackage api
75 * @category lib
6+ *
87 * @author Boris Momčilović <[email protected] > 98 * @copyright All rights reserved
9+ *
1010 * @version 1.0
1111 */
1212
@@ -19,157 +19,168 @@ class Client
1919{
2020 const COMMAND = 'cmd ' ;
2121
22- /**
23- * API URL
24- *
25- * @var string
26- * @access protected
27- */
22+ /**
23+ * API URL.
24+ *
25+ * @var string
26+ */
2827 protected $ url ;
2928
30- /**
31- * API authorization user
32- *
33- * @var string
34- * @access protected
35- */
29+ /**
30+ * API authorization user.
31+ *
32+ * @var string
33+ */
3634 protected $ user ;
3735
38- /**
39- * API authorization pass
40- *
41- * @var string $pass
42- * @access protected
43- */
36+ /**
37+ * API authorization pass.
38+ *
39+ * @var string
40+ */
4441 protected $ pass ;
4542
46- /**
47- * API authorization pass
48- *
49- * @var int $timeout [= 5]
50- * @access protected
51- */
43+ /**
44+ * API authorization pass.
45+ *
46+ * @var int [= 5]
47+ */
5248 protected $ timeout = 5 ;
5349
54- /**
55- * Instance of DatingVIP\cURL\Request lib
56- *
57- * @var Request
58- * @access private
59- */
50+ /**
51+ * Instance of DatingVIP\cURL\Request lib.
52+ *
53+ * @var Request
54+ */
6055 private $ curl ;
6156
62- /**
63- * Set API url
64- *
65- * @param string $url
66- * @access public
67- * @return bool
68- */
57+ /**
58+ * Set API url.
59+ *
60+ * @param string $url
61+ *
62+ * @return bool
63+ */
6964 public function setUrl ($ url )
7065 {
7166 $ this ->url = (string ) $ url ;
7267
73- return !empty ($ this ->url );
68+ return !empty ($ this ->url );
7469 }
7570
76- /**
77- * Set auth data for API
78- *
79- * @param string $user
80- * @param string $pass
81- * @access public
82- * @return bool
83- */
71+ /**
72+ * Set auth data for API.
73+ *
74+ * @param string $user
75+ * @param string $pass
76+ *
77+ * @return bool
78+ */
8479 public function setAuth ($ user , $ pass )
8580 {
86- $ this ->user = (string ) $ user ;
87- $ this ->pass = (string ) $ pass ;
81+ $ this ->user = (string ) $ user ;
82+ $ this ->pass = (string ) $ pass ;
8883
89- return $ this ->hasAuth ();
84+ return $ this ->hasAuth ();
9085 }
9186
92- /**
93- * Set request timeout value (in seconds)
94- *
95- * @param int $timeout
96- * @access public
97- * @return int
98- */
87+ /**
88+ * Set request timeout value (in seconds).
89+ *
90+ * @param int $timeout
91+ *
92+ * @return int
93+ */
9994 public function setTimeout ($ timeout )
10095 {
101- $ timeout = is_scalar ($ timeout ) ? (int ) $ timeout : 0 ;
96+ $ timeout = is_scalar ($ timeout ) ? (int ) $ timeout : 0 ;
97+
10298 return $ timeout < 1
10399 ? $ this ->timeout
104100 : $ this ->timeout = $ timeout ;
105101 }
106102
107- /**
108- * Execute API command
109- *
110- * @param Command $command
111- * @access public
112- * @return \DatingVIP\API\Response
113- * @throws \Exception
114- */
103+ /**
104+ * Execute API command.
105+ *
106+ * @param Command $command
107+ *
108+ * @return \DatingVIP\API\Response
109+ *
110+ * @throws \Exception
111+ */
115112 public function execute (Command $ command )
116113 {
117- if (!$ command ->isValid ()) {
118- throw new Exception ('Invalid API command supplied ' );
114+ if (!$ command ->isValid ()) {
115+ throw new Exception ('Invalid API command supplied ' );
119116 }
120117
121- $ result = $ this ->curl ()->post ($ this ->getUrl ($ command ), $ command ->getData ());
118+ $ result = $ this ->curl ()->post ($ this ->getUrl ($ command ), $ command ->getData ());
119+ $ type = $ this ->getResponseType ();
122120
123- return new Response ($ result ->getData () );
121+ return new Response ($ result ->getData (), $ type );
124122 }
125123
126- /**
127- * Get browser for making API requests
128- * - create an instance
129- * - assign auth data if we have it
130- *
131- * @param void
132- * @access private
133- * @return cURL
134- */
124+ /**
125+ * Return expected response type based on URL.
126+ *
127+ * @param void
128+ *
129+ * @return string
130+ */
131+ private function getResponseType ()
132+ {
133+ list ($ path , $ type ) = explode ('. ' , basename ($ this ->url ), 2 );
134+
135+ return $ type ;
136+ }
137+
138+ /**
139+ * Get browser for making API requests
140+ * - create an instance
141+ * - assign auth data if we have it.
142+ *
143+ * @param void
144+ *
145+ * @return cURL
146+ */
135147 private function curl ()
136148 {
137- if (! ($ this ->curl instanceof cURL)) {
138- $ this ->curl = new cURL ();
149+ if (!($ this ->curl instanceof cURL)) {
150+ $ this ->curl = new cURL ();
139151 }
140152
141- if ($ this ->hasAuth ()) {
142- $ this ->curl ->setCredentials ($ this ->user , $ this ->pass );
153+ if ($ this ->hasAuth ()) {
154+ $ this ->curl ->setCredentials ($ this ->user , $ this ->pass );
143155 }
144156
145- return $ this ->curl ->setTimeout ($ this ->timeout );
157+ return $ this ->curl ->setTimeout ($ this ->timeout );
146158 }
147159
148- /**
149- * Get API URL for given command
150- *
151- * @param Command $command
152- * @access protected
153- * @return string
154- */
160+ /**
161+ * Get API URL for given command.
162+ *
163+ * @param Command $command
164+ *
165+ * @return string
166+ */
155167 protected function getUrl (Command $ command )
156168 {
157169 return $ this ->url
158- . (stripos ($ this ->url , '? ' ) !== false ? '& ' : '? ' )
159- . http_build_query ([self ::COMMAND => $ command ->getName ()]);
170+ .(stripos ($ this ->url , '? ' ) !== false ? '& ' : '? ' )
171+ .http_build_query ([self ::COMMAND => $ command ->getName ()]);
160172 }
161173
162- /**
163- * Check if API has auth data set
164- * - checks if user and pass aren't empty
165- *
166- * @param void
167- * @access private
168- * @return bool
169- */
174+ /**
175+ * Check if API has auth data set
176+ * - checks if user and pass aren't empty.
177+ *
178+ * @param void
179+ *
180+ * @return bool
181+ */
170182 private function hasAuth ()
171183 {
172- return !empty ($ this ->user ) && !empty ($ this ->pass );
184+ return !empty ($ this ->user ) && !empty ($ this ->pass );
173185 }
174-
175186}
0 commit comments