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,33 @@ 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 = self ::createClient ();
6462 $ this ->setResource (new Hal \Resource ($ state , $ links ));
6563 }
6664
65+ private static function createClient ()
66+ {
67+ if (Settings::$ username == null || Settings::$ password == null ) {
68+ $ client = new Hal \Client (Settings::$ url_root , '/ ' );
69+ }
70+ else {
71+ $ client = new Hal \Client (
72+ Settings::$ url_root ,
73+ '/ ' ,
74+ null ,
75+ new BasicAuthentication (Settings::$ username , Settings::$ password ));
76+ }
77+ return $ client ;
78+ }
79+
6780 public function __get ($ name )
6881 {
69- if ($ this ->state ->has_key ($ name ))
70- {
82+ if ($ this ->state ->has_key ($ name )) {
7183 return $ this ->state [$ name ];
7284 }
7385
@@ -92,8 +104,8 @@ public function __set($name, $value)
92104 public function __isset ($ name )
93105 {
94106 if (array_key_exists ($ name , $ this ->resource ->getAllLinks ()) ||
95- array_key_exists ($ name , $ this ->resource ->getState ()))
96- {
107+ array_key_exists ($ name , $ this ->resource ->getState ())
108+ ) {
97109 return true ;
98110 }
99111
@@ -111,11 +123,21 @@ public function __isset($name)
111123 public static function retrieve ($ id )
112124 {
113125 $ uri = self ::getHrefSpec ()->collection_uri . '/ ' . $ id ;
114- $ resource = self ::getClient ()->sendRequest (new Request ($ uri ));
126+ $ resource = self ::createClient ()->sendRequest (new Request ($ uri ));
115127 $ class = get_called_class ();
116128 return new $ class ($ resource ->getState (), $ resource ->getAllLinks ());
117129 }
118130
131+ public function refresh () {
132+ $ request = new Request (
133+ $ this ->resource ->getLink ("self " )->getHref (),
134+ 'GET '
135+ );
136+ $ resource = $ this ->getClient ()->sendRequest ($ request );
137+ $ this ->setResource ($ resource );
138+ return $ this ;
139+ }
140+
119141 /**
120142 * @return \Finix\Resource
121143 * @throws Hal\Exception\HalClientErrorException
@@ -128,12 +150,11 @@ public static function retrieve($id)
128150 public function save ()
129151 {
130152 if (empty ($ this ->state ["tags " ])) {
131- $ this ->state ["tags " ] = new stdClass ();
153+ $ this ->state ["tags " ] = new stdClass ();
132154 }
133155
134156 $ payload = new JsonBody (iterator_to_array ($ this ->state ));
135- if ($ this ->isUpdate ())
136- {
157+ if ($ this ->isUpdate ()) {
137158 $ request = new Request (
138159 $ this ->resource ->getLink ("self " )->getHref (),
139160 'PUT ' ,
@@ -199,5 +220,9 @@ private function setResource($resource)
199220 $ this ->state = new ArrayProxy ($ resource ->getState ());
200221 }
201222
202-
203- }
223+ public function verifyOn (Verification $ verification )
224+ {
225+ $ verifyLink = $ this ->resource ->getLink ("verifications " )->getHref ();
226+ return $ verification ->create ($ verifyLink );
227+ }
228+ }
0 commit comments