1 <?php
2 /**
3 * Copyright © 2013 Native5
4 *
5 * All Rights Reserved.
6 * Licensed under the Native5 License, Version 1.0 (the "License");
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.native5.com/legal/npl-v1.html
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * PHP version 5.3+
18 *
19 * @category <category>
20 * @package Native5\Core\<package>
21 * @author Barada Sahu <barry@native5.com>
22 * @copyright 2012 Native5. All Rights Reserved
23 * @license See attached LICENSE for details
24 * @version GIT: $gitid$
25 * @link http://www.docs.native5.com
26 */
27
28 namespace Native5\Services\Rules;
29
30 use Native5\Services\Common\ApiClient;
31
32 /**
33 * SchemeRemoteDAO
34 *
35 * @category Schemes
36 * @package Akzo\Schemes
37 * @author Barada Sahu <barry@native5.com>
38 * @copyright 2012 Native5. All Rights Reserved
39 * @license See attached NOTICE.md for details
40 * @version Release: 1.0
41 * @link http://www.docs.native5.com
42 * Created : 27-11-2012
43 * Last Modified : Fri Dec 21 09:11:53 2012
44 */
45 class RemoteRulesService extends ApiClient
46 {
47
48
49
50 /**
51 * Find the rules by pattern
52 *
53 * @param mixed $rulePattern
54 * @access public
55 * @return void
56 */
57 public function findByPattern($rulePattern) {
58 $logger = $GLOBALS['logger'];
59 $path = 'rules/';
60 $request = $this->_remoteServer->get($path);
61 $query = $request->getQuery();
62 $query->set('pattern',$rulePattern);
63 $logger->info(print_r($query,1));
64 //$request = $this->_remoteServer->get($path, array(), array('pattern'=>urlencode($rulePattern)));
65 try {
66 $response = $request->send();
67 if ($response->getStatusCode() !== 200) {
68 throw new \Exception('Unable to handle request');
69 }
70 $rawResponse = $response->getBody('true');
71 return $rawResponse;
72 } catch (\Exception $e) {
73 throw $e;
74 }
75
76 }//end findByPattern()
77
78
79 /**
80 * Save Rule in Database
81 *
82 * @param mixed $rule
83 *
84 * @access public
85 * @return void
86 */
87 public function saveRule($rule) {
88 $logger = $GLOBALS['logger'];
89 $path = 'rules/create';
90 $request = $this->_remoteServer->post(
91 $path,
92 array('Content-Type'=>'application/json'),
93 $rule
94 );
95 try {
96 $response = $request->send();
97 if ($response->getStatusCode() !== 200) {
98 throw new \Exception('Unable to handle request');
99 }
100 $rawResponse = $response->getBody('true');
101 return $rawResponse;
102 } catch (\Exception $e) {
103 throw $e;
104 }
105
106 }//end saveRule()
107
108
109 /**
110 * Update Rule in Database
111 *
112 * @param mixed $rule
113 *
114 * @access public
115 * @return void
116 */
117 public function updateRule($rule) {
118 $logger = $GLOBALS['logger'];
119 $path = 'rules/update';
120 $request = $this->_remoteServer->put(
121 $path,
122 array('Content-Type'=>'application/json'),
123 $rule
124 );
125 try {
126 $response = $request->send();
127 if ($response->getStatusCode() !== 200) {
128 throw new \Exception('Unable to handle request');
129 }
130 $rawResponse = $response->getBody('true');
131 return $rawResponse;
132 } catch (\Exception $e) {
133 throw $e;
134 }
135
136 }//end saveRule()
137
138
139 /**
140 * Evaluate incoming data against available rules.
141 *
142 * @param mixed $data
143 *
144 * @access public
145 * @return void
146 */
147 public function evaluateRules($ruleData)
148 {
149 $logger = $GLOBALS['logger'];
150 $path = 'rules/execute';
151 $request = $this->_remoteServer->post(
152 $path,
153 array('Content-Type'=>'application/json'),
154 $ruleData
155 );
156 try {
157 $response = $request->send();
158 if ($response->getStatusCode() !== 200) {
159 throw new \Exception('Unable to handle request');
160 }
161 $rawResponse = $response->getBody('true');
162 $logger->debug('Rule Evaluation Outcome = '.print_r($rawResponse,1));
163 return $rawResponse;
164 } catch (\Exception $e) {
165 throw $e;
166 }
167 }
168 }
169 ?>
170