44use Finix \Http \Auth \BasicAuthentication ;
55use Finix \Http \JsonBody ;
66use Finix \Http \Request ;
7+ use Finix \Resources \Verification ;
78use Finix \Utils \ArrayProxy ;
89use \stdClass ;
910
@@ -15,7 +16,8 @@ abstract class Resource
1516 protected $ state ;
1617
1718 protected static $ href ;
18- protected static $ client ;
19+ protected $ client ;
20+ // protected static $client;
1921 protected static $ registry ;
2022
2123 /**
@@ -24,10 +26,10 @@ abstract class Resource
2426 */
2527 public static function getHrefSpec ($ resource = null )
2628 {
27- if (is_null ($ resource )){
29+ if (is_null ($ resource )) {
2830 $ resource = get_called_class ();
2931 }
30- if (!is_string ($ resource )) {
32+ if (!is_string ($ resource )) {
3133 $ resource = get_class ($ resource );
3234 }
3335 return self ::getRegistry ()->getHrefSpecForResource ($ resource );
@@ -36,9 +38,9 @@ public static function getHrefSpec($resource = null)
3638 /**
3739 * @return Hal\Client
3840 */
39- public static function getClient ()
41+ public function getClient ()
4042 {
41- return self :: $ client ;
43+ return $ this -> client ;
4244 }
4345
4446 /**
@@ -51,23 +53,18 @@ public static function getRegistry()
5153
5254 public static function init ()
5355 {
54- self ::$ client = new Hal \Client (
55- Settings::$ url_root ,
56- '/ ' ,
57- null ,
58- new BasicAuthentication (Settings::$ username , Settings::$ password ));
5956 self ::$ registry = new Registry ();
6057 }
6158
6259 public function __construct (array $ state = null , array $ links = null )
6360 {
61+ $ this ->client = Bootstrap::createClient ();
6462 $ this ->setResource (new Hal \Resource ($ state , $ links ));
6563 }
6664
6765 public function __get ($ name )
6866 {
69- if ($ this ->state ->has_key ($ name ))
70- {
67+ if ($ this ->state ->has_key ($ name )) {
7168 return $ this ->state [$ name ];
7269 }
7370
@@ -92,8 +89,8 @@ public function __set($name, $value)
9289 public function __isset ($ name )
9390 {
9491 if (array_key_exists ($ name , $ this ->resource ->getAllLinks ()) ||
95- array_key_exists ($ name , $ this ->resource ->getState ()))
96- {
92+ array_key_exists ($ name , $ this ->resource ->getState ())
93+ ) {
9794 return true ;
9895 }
9996
@@ -111,11 +108,28 @@ public function __isset($name)
111108 public static function retrieve ($ id )
112109 {
113110 $ uri = self ::getHrefSpec ()->collection_uri . '/ ' . $ id ;
114- $ resource = self :: getClient ()->sendRequest (new Request ($ uri ));
111+ $ resource = Bootstrap:: createClient ()->sendRequest (new Request ($ uri ));
115112 $ class = get_called_class ();
116113 return new $ class ($ resource ->getState (), $ resource ->getAllLinks ());
117114 }
118115
116+ public static function getPagination ($ href )
117+ {
118+ $ resource = Bootstrap::createClient ()->sendRequest (new Request ($ href ));
119+ return new Pagination ($ resource , get_called_class ());
120+ }
121+
122+ public function refresh ()
123+ {
124+ $ request = new Request (
125+ $ this ->resource ->getLink ("self " )->getHref (),
126+ 'GET '
127+ );
128+ $ resource = $ this ->getClient ()->sendRequest ($ request );
129+ $ this ->setResource ($ resource );
130+ return $ this ;
131+ }
132+
119133 /**
120134 * @return \Finix\Resource
121135 * @throws Hal\Exception\HalClientErrorException
@@ -128,12 +142,11 @@ public static function retrieve($id)
128142 public function save ()
129143 {
130144 if (empty ($ this ->state ["tags " ])) {
131- $ this ->state ["tags " ] = new stdClass ();
145+ $ this ->state ["tags " ] = new stdClass ();
132146 }
133147
134148 $ payload = new JsonBody (iterator_to_array ($ this ->state ));
135- if ($ this ->isUpdate ())
136- {
149+ if ($ this ->isUpdate ()) {
137150 $ request = new Request (
138151 $ this ->resource ->getLink ("self " )->getHref (),
139152 'PUT ' ,
@@ -183,11 +196,12 @@ private function isUpdate()
183196
184197
185198 /**
199+ * @param string $rel
186200 * @return string
187201 */
188- public function getHref ()
202+ public function getHref ($ rel = " self " )
189203 {
190- return $ this ->resource ->getLink (" self " )->getHref ();
204+ return $ this ->resource ->getLink ($ rel )->getHref ();
191205 }
192206
193207 /**
@@ -199,5 +213,9 @@ private function setResource($resource)
199213 $ this ->state = new ArrayProxy ($ resource ->getState ());
200214 }
201215
202-
203- }
216+ public function verifyOn (Verification $ verification )
217+ {
218+ $ verifyLink = $ this ->resource ->getLink ("verifications " )->getHref ();
219+ return $ verification ->create ($ verifyLink );
220+ }
221+ }
0 commit comments