1: <?php
2: namespace NGS\Client;
3:
4: require_once(__DIR__.'/../Utils.php');
5: require_once(__DIR__.'/../Name.php');
6: require_once(__DIR__.'/RestHttp.php');
7: require_once(__DIR__.'/../Converter/PrimitiveConverter.php');
8: require_once(__DIR__.'/../Patterns/Repository.php');
9:
10:
11: use NGS\Converter\PrimitiveConverter;
12: use NGS\Name;
13: use NGS\Patterns\AggregateDomainEvent;
14: use NGS\Patterns\DomainEvent;
15: use NGS\Patterns\IDomainObject;
16: use NGS\Patterns\Repository;
17: use NGS\Patterns\Specification;
18:
19: 20: 21: 22: 23:
24: class DomainProxy
25: {
26: const DOMAIN_URI = 'Domain.svc';
27:
28: 29: 30:
31: protected $http;
32:
33: 34: 35:
36: protected static $instance;
37:
38: 39: 40: 41: 42: 43:
44: public function __construct(RestHttp $http = null)
45: {
46: $this->http = $http !== null ? $http : RestHttp::instance();
47: }
48:
49: 50: 51: 52: 53:
54: public static function instance()
55: {
56: if(self::$instance === null)
57: self::$instance = new DomainProxy();
58: return self::$instance;
59: }
60:
61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74:
75: public function find($class, array $uris)
76: {
77: $name = Name::full($class);
78: $body = array('Name' => $name, 'Uri' => PrimitiveConverter::toStringArray($uris));
79: $response =
80: $this->http->sendRequest(
81: ApplicationProxy::APPLICATION_URI.'/GetDomainObject',
82: 'POST',
83: json_encode($body),
84: array(200));
85: return RestHttp::parseResult($response, $class);
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104:
105: public function search(
106: $class,
107: $limit = null,
108: $offset = null,
109: array $order = null)
110: {
111: $name = Name::full($class);
112: $lo = QueryString::formatLimitAndOffsetAndOrder($limit, $offset, $order);
113: $response =
114: $this->http->sendRequest(
115: self::DOMAIN_URI.'/search/'.rawurlencode($name).($lo ? '?'.$lo : ''),
116: 'GET',
117: null,
118: array(200));
119: return RestHttp::parseResult($response, $class);
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132:
133: public function searchWithSpecification(
134: $class,
135: Specification $specification,
136: $limit = null,
137: $offset = null,
138: array $order = null)
139: {
140: $objectName = Name::full($class);
141: $specName = Name::parent($specification).'+'.Name::base($specification);
142: $lo = QueryString::formatLimitAndOffsetAndOrder($limit, $offset, $order);
143: $response =
144: $this->http->sendRequest(
145: self::DOMAIN_URI.'/search/'
146: .rawurlencode($objectName)
147: .'?specification='.rawurlencode($specName)
148: .($lo ? '&'.$lo : ''),
149: 'PUT',
150: $specification->toJson(),
151: array(200));
152: return RestHttp::parseResult($response, Name::toClass($class));
153: }
154:
155: 156: 157: 158: 159: 160: 161: 162: 163: 164:
165: public function searchGeneric(
166: $class,
167: array $filters = null,
168: $limit = null,
169: $offset = null,
170: array $order = null)
171: {
172: $object = Name::full($class);
173: $lo = QueryString::formatLimitAndOffsetAndOrder($limit, $offset, $order);
174: $response =
175: $this->http->sendRequest(
176: self::DOMAIN_URI.'/search-generic/'.rawurlencode($object).($lo ? '?'.$lo : ''),
177: 'PUT',
178: json_encode($filters),
179: array(200));
180: return RestHttp::parseResult($response, Name::toClass($object));
181: }
182:
183: 184: 185: 186: 187: 188: 189:
190: public function countGeneric(
191: $class,
192: array $filters = null)
193: {
194: $object = Name::full($class);
195: $response =
196: $this->http->sendRequest(
197: self::DOMAIN_URI.'/count-generic/'.rawurlencode($object),
198: 'PUT',
199: json_encode($filters),
200: array(200));
201: return RestHttp::parseResult($response, Name::toClass($object));
202: }
203:
204: 205: 206: 207: 208: 209:
210: public function count($class)
211: {
212: $name = Name::full($class);
213: $response = $this->http->sendRequest(
214: self::DOMAIN_URI.'/count/'.rawurlencode($name),
215: 'GET',
216: null,
217: array(200));
218: $count = RestHttp::parseResult($response);
219: return PrimitiveConverter::toInteger($count);
220: }
221:
222: 223: 224: 225: 226: 227:
228: public function countWithSpecification(Specification $specification)
229: {
230: $object = Name::parent($specification);
231: $name = Name::base($specification);
232: $response =
233: $this->http->sendRequest(
234: self::DOMAIN_URI.'/count/'.rawurlencode($object).'?specification='.rawurlencode($name),
235: 'PUT',
236: $specification->toJson(),
237: array(200));
238: $count = RestHttp::parseResult($response);
239: return PrimitiveConverter::toInteger($count);
240: }
241:
242: 243: 244: 245: 246: 247: 248:
249: public function submitEvent(DomainEvent $event)
250: {
251: $name = Name::full($event);
252: $response =
253: $this->http->sendRequest(
254: self::DOMAIN_URI.'/submit/'.rawurlencode($name),
255: 'POST',
256: $event->toJson(),
257: array(201));
258: $uri = RestHttp::parseResult($response);
259: return PrimitiveConverter::toString($uri);
260: }
261:
262: 263: 264: 265: 266: 267: 268: 269:
270: public function submitAggregateEvent(AggregateDomainEvent $event, $uri)
271: {
272: $object = Name::parent($event);
273: $name = Name::base($event);
274: $response =
275: $this->http->sendRequest(
276: self::DOMAIN_URI.'/submit/'.rawurlencode($object).'/'.rawurlencode($name).'?uri='.rawurlencode($uri),
277: 'POST',
278: $event->toJson(),
279: array(201));
280: Repository::instance()->invalidate($object, $uri);
281: return RestHttp::parseResult($response, Name::toClass($object));
282: }
283:
284: }
285: