| Total Complexity | 115 |
| Total Lines | 844 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 2 | Features | 0 |
Complex classes like BaseService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 62 | abstract class BaseService implements IRequestHandler, IService |
||
| 63 | { |
||
| 64 | /** |
||
| 65 | * The wrapper over IQueryProvider and IMetadataProvider implementations. |
||
| 66 | * |
||
| 67 | * @var ProvidersWrapper |
||
| 68 | */ |
||
| 69 | private $providersWrapper; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The wrapper over IStreamProvider implementation. |
||
| 73 | * |
||
| 74 | * @var StreamProviderWrapper |
||
| 75 | */ |
||
| 76 | protected $streamProvider; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Hold reference to the ServiceHost instance created by dispatcher, |
||
| 80 | * using this library can access headers and body of Http Request |
||
| 81 | * dispatcher received and the Http Response Dispatcher is going to send. |
||
| 82 | * |
||
| 83 | * @var ServiceHost |
||
| 84 | */ |
||
| 85 | private $serviceHost; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * To hold reference to ServiceConfiguration instance where the |
||
| 89 | * service specific rules (page limit, resource set access rights |
||
| 90 | * etc...) are defined. |
||
| 91 | * |
||
| 92 | * @var IServiceConfiguration |
||
| 93 | */ |
||
| 94 | protected $config; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Hold reference to object serialiser - bit wot turns PHP objects |
||
| 98 | * into message traffic on wire. |
||
| 99 | * |
||
| 100 | * @var IObjectSerialiser |
||
| 101 | */ |
||
| 102 | protected $objectSerialiser; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get reference to object serialiser - bit wot turns PHP objects |
||
| 106 | * into message traffic on wire. |
||
| 107 | * |
||
| 108 | * @return IObjectSerialiser |
||
| 109 | */ |
||
| 110 | public function getObjectSerialiser() |
||
| 111 | { |
||
| 112 | assert(null != $this->objectSerialiser); |
||
| 113 | |||
| 114 | return $this->objectSerialiser; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * BaseService constructor. |
||
| 119 | * @param IObjectSerialiser|null $serialiser |
||
| 120 | */ |
||
| 121 | protected function __construct(IObjectSerialiser $serialiser = null) |
||
| 122 | { |
||
| 123 | if (null != $serialiser) { |
||
| 124 | $serialiser->setService($this); |
||
| 125 | } else { |
||
| 126 | $serialiser = new ObjectModelSerializer($this, null); |
||
| 127 | } |
||
| 128 | $this->objectSerialiser = $serialiser; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Gets reference to ServiceConfiguration instance so that |
||
| 133 | * service specific rules defined by the developer can be |
||
| 134 | * accessed. |
||
| 135 | * |
||
| 136 | * @return IServiceConfiguration |
||
| 137 | */ |
||
| 138 | public function getConfiguration() |
||
| 139 | { |
||
| 140 | assert(null != $this->config); |
||
| 141 | |||
| 142 | return $this->config; |
||
| 143 | } |
||
| 144 | |||
| 145 | //TODO: shouldn't we hide this from the interface..if we need it at all. |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get the wrapper over developer's IQueryProvider and IMetadataProvider implementation. |
||
| 149 | * |
||
| 150 | * @return ProvidersWrapper |
||
| 151 | */ |
||
| 152 | public function getProvidersWrapper() |
||
| 153 | { |
||
| 154 | return $this->providersWrapper; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Gets reference to wrapper class instance over IDSSP implementation. |
||
| 159 | * |
||
| 160 | * @return StreamProviderWrapper |
||
| 161 | */ |
||
| 162 | public function getStreamProviderWrapper() |
||
| 163 | { |
||
| 164 | return $this->streamProvider; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get reference to the data service host instance. |
||
| 169 | * |
||
| 170 | * @return ServiceHost |
||
| 171 | */ |
||
| 172 | public function getHost() |
||
| 173 | { |
||
| 174 | assert(null != $this->serviceHost); |
||
| 175 | |||
| 176 | return $this->serviceHost; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Sets the data service host instance. |
||
| 181 | * |
||
| 182 | * @param ServiceHost $serviceHost The data service host instance |
||
| 183 | */ |
||
| 184 | public function setHost(ServiceHost $serviceHost) |
||
| 185 | { |
||
| 186 | $this->serviceHost = $serviceHost; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * To get reference to operation context where we have direct access to |
||
| 191 | * headers and body of Http Request, we have received and the Http Response |
||
| 192 | * We are going to send. |
||
| 193 | * |
||
| 194 | * @return IOperationContext |
||
| 195 | */ |
||
| 196 | public function getOperationContext() |
||
| 197 | { |
||
| 198 | return $this->getHost()->getOperationContext(); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get reference to the wrapper over IStreamProvider or |
||
| 203 | * IStreamProvider2 implementations. |
||
| 204 | * |
||
| 205 | * @return StreamProviderWrapper |
||
| 206 | */ |
||
| 207 | public function getStreamProvider() |
||
| 208 | { |
||
| 209 | if (null === $this->streamProvider) { |
||
| 210 | $this->streamProvider = new StreamProviderWrapper(); |
||
| 211 | $this->streamProvider->setService($this); |
||
| 212 | } |
||
| 213 | |||
| 214 | return $this->streamProvider; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Top-level handler invoked by Dispatcher against any request to this |
||
| 219 | * service. This method will hand over request processing task to other |
||
| 220 | * functions which process the request, set required headers and Response |
||
| 221 | * stream (if any in Atom/Json format) in |
||
| 222 | * WebOperationContext::Current()::OutgoingWebResponseContext. |
||
| 223 | * Once this function returns, dispatcher uses global WebOperationContext |
||
| 224 | * to write out the request response to client. |
||
| 225 | * This function will perform the following operations: |
||
| 226 | * (1) Check whether the top level service class implements |
||
| 227 | * IServiceProvider which means the service is a custom service, in |
||
| 228 | * this case make sure the top level service class implements |
||
| 229 | * IMetaDataProvider and IQueryProvider. |
||
| 230 | * These are the minimal interfaces that a custom service to be |
||
| 231 | * implemented in order to expose its data as OData. Save reference to |
||
| 232 | * These interface implementations. |
||
| 233 | * NOTE: Here we will ensure only providers for IDSQP and IDSMP. The |
||
| 234 | * IDSSP will be ensured only when there is an GET request on MLE/Named |
||
| 235 | * stream. |
||
| 236 | * |
||
| 237 | * (2). Invoke 'Initialize' method of top level service for |
||
| 238 | * collecting the configuration rules set by the developer for this |
||
| 239 | * service. |
||
| 240 | * |
||
| 241 | * (3). Invoke the Uri processor to process the request URI. The uri |
||
| 242 | * processor will do the following: |
||
| 243 | * (a). Validate the request uri syntax using OData uri rules |
||
| 244 | * (b). Validate the request using metadata of this service |
||
| 245 | * (c). Parse the request uri and using, IQueryProvider |
||
| 246 | * implementation, fetches the resources pointed by the uri |
||
| 247 | * if required |
||
| 248 | * (d). Build a RequestDescription which encapsulate everything |
||
| 249 | * related to request uri (e.g. type of resource, result |
||
| 250 | * etc...) |
||
| 251 | * (3). Invoke handleRequest2 for further processing |
||
| 252 | * @throws ODataException |
||
| 253 | */ |
||
| 254 | public function handleRequest() |
||
| 255 | { |
||
| 256 | try { |
||
| 257 | $this->createProviders(); |
||
| 258 | $this->getHost()->validateQueryParameters(); |
||
| 259 | $uriProcessor = UriProcessorNew::process($this); |
||
| 260 | $request = $uriProcessor->getRequest(); |
||
| 261 | if (TargetKind::BATCH() == $request->getTargetKind()) { |
||
| 262 | //dd($request); |
||
| 263 | $this->getProvidersWrapper()->startTransaction(true); |
||
| 264 | try { |
||
| 265 | $this->handleBatchRequest($request); |
||
| 266 | } catch (\Exception $ex) { |
||
| 267 | $this->getProvidersWrapper()->rollBackTransaction(); |
||
| 268 | throw $ex; |
||
| 269 | } |
||
| 270 | $this->getProvidersWrapper()->commitTransaction(); |
||
| 271 | } else { |
||
| 272 | $this->serializeResult($request, $uriProcessor); |
||
| 273 | } |
||
| 274 | } catch (\Exception $exception) { |
||
| 275 | ErrorHandler::handleException($exception, $this); |
||
| 276 | // Return to dispatcher for writing serialized exception |
||
| 277 | return; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param $request |
||
| 283 | * @throws ODataException |
||
| 284 | */ |
||
| 285 | private function handleBatchRequest($request) |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return IQueryProvider|null |
||
| 301 | */ |
||
| 302 | abstract public function getQueryProvider(): ?IQueryProvider; |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return IMetadataProvider |
||
| 306 | */ |
||
| 307 | abstract public function getMetadataProvider(); |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return \POData\Providers\Stream\IStreamProvider2 |
||
| 311 | */ |
||
| 312 | abstract public function getStreamProviderX(); |
||
| 313 | |||
| 314 | /** @var ODataWriterRegistry */ |
||
| 315 | protected $writerRegistry; |
||
| 316 | |||
| 317 | /** @var ODataReaderRegistry */ |
||
| 318 | protected $readerRegistry; |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns the ODataWriterRegistry to use when writing the response to a service document or resource request. |
||
| 322 | * |
||
| 323 | * @return ODataWriterRegistry |
||
| 324 | */ |
||
| 325 | public function getODataWriterRegistry() |
||
| 326 | { |
||
| 327 | assert(null != $this->writerRegistry); |
||
| 328 | |||
| 329 | return $this->writerRegistry; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns the ODataWriterRegistry to use when writing the response to a service document or resource request. |
||
| 334 | * |
||
| 335 | * @return ODataReaderRegistry |
||
| 336 | */ |
||
| 337 | public function getODataReaderRegistry() |
||
| 342 | } |
||
| 343 | /** |
||
| 344 | * This method will query and validates for IMetadataProvider and IQueryProvider implementations, invokes |
||
| 345 | * BaseService::Initialize to initialize service specific policies. |
||
| 346 | * |
||
| 347 | * @throws ODataException |
||
| 348 | * @throws \Exception |
||
| 349 | */ |
||
| 350 | protected function createProviders() |
||
| 381 | } |
||
| 382 | |||
| 383 | //TODO: i don't want this to be public..but it's the only way to test it right now... |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @throws \Exception |
||
| 387 | */ |
||
| 388 | public function registerWriters() |
||
| 389 | { |
||
| 390 | $registry = $this->getODataWriterRegistry(); |
||
| 391 | $serviceVersion = $this->getConfiguration()->getMaxDataServiceVersion(); |
||
| 392 | $serviceURI = $this->getHost()->getAbsoluteServiceUri()->getUrlAsString(); |
||
| 393 | |||
| 394 | //We always register the v1 stuff |
||
| 395 | $registry->register(new JsonODataV1Writer()); |
||
| 396 | $registry->register(new AtomODataWriter($serviceURI)); |
||
| 397 | |||
| 398 | if (-1 < $serviceVersion->compare(Version::v2())) { |
||
| 399 | $registry->register(new JsonODataV2Writer()); |
||
| 400 | } |
||
| 401 | |||
| 402 | if (-1 < $serviceVersion->compare(Version::v3())) { |
||
| 403 | $registry->register(new JsonLightODataWriter(JsonLightMetadataLevel::NONE(), $serviceURI)); |
||
| 404 | $registry->register(new JsonLightODataWriter(JsonLightMetadataLevel::MINIMAL(), $serviceURI)); |
||
| 405 | $registry->register(new JsonLightODataWriter(JsonLightMetadataLevel::FULL(), $serviceURI)); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | public function registerReaders() |
||
| 409 | { |
||
| 410 | $registry = $this->getODataReaderRegistry(); |
||
| 411 | //We always register the v1 stuff |
||
| 412 | $registry->register(new AtomODataReader()); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Serialize the requested resource. |
||
| 417 | * |
||
| 418 | * @param RequestDescription $request The description of the request submitted by the client |
||
| 419 | * @param IUriProcessor $uriProcessor Reference to the uri processor |
||
| 420 | * |
||
| 421 | * @throws Common\HttpHeaderFailure |
||
| 422 | * @throws Common\UrlFormatException |
||
| 423 | * @throws InvalidOperationException |
||
| 424 | * @throws ODataException |
||
| 425 | * @throws \ReflectionException |
||
| 426 | * @throws \Exception |
||
| 427 | */ |
||
| 428 | protected function serializeResult(RequestDescription $request, IUriProcessor $uriProcessor) |
||
| 601 | } |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Gets the response format for the requested resource. |
||
| 606 | * |
||
| 607 | * @param RequestDescription $request The request submitted by client and it's execution result |
||
| 608 | * @param IUriProcessor $uriProcessor The reference to the IUriProcessor |
||
| 609 | * |
||
| 610 | * @throws Common\HttpHeaderFailure |
||
| 611 | * @throws InvalidOperationException |
||
| 612 | * @throws ODataException , HttpHeaderFailure |
||
| 613 | * @throws \ReflectionException |
||
| 614 | * @throws Common\UrlFormatException |
||
| 615 | * @return string|null the response content-type, a null value means the requested resource |
||
| 616 | * is named stream and IDSSP2::getStreamContentType returned null |
||
| 617 | */ |
||
| 618 | public function getResponseContentType( |
||
| 752 | } |
||
| 753 | |||
| 754 | /** |
||
| 755 | * For the given entry object compare its eTag (if it has eTag properties) |
||
| 756 | * with current eTag request headers (if present). |
||
| 757 | * |
||
| 758 | * @param mixed &$entryObject entity resource for which etag |
||
| 759 | * needs to be checked |
||
| 760 | * @param ResourceType &$resourceType Resource type of the entry |
||
| 761 | * object |
||
| 762 | * @param bool &$needToSerializeResponse On return, this will contain |
||
| 763 | * True if response needs to be |
||
| 764 | * serialized, False otherwise |
||
| 765 | * |
||
| 766 | * @throws ODataException |
||
| 767 | * @throws InvalidOperationException |
||
| 768 | * @throws \ReflectionException |
||
| 769 | * @return string|null The ETag for the entry object if it has eTag properties |
||
| 770 | * NULL otherwise |
||
| 771 | */ |
||
| 772 | protected function compareETag( |
||
| 852 | } |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Returns the etag for the given resource. |
||
| 856 | * Note: This function will not add W\" prefix and " suffix, that is caller's |
||
| 857 | * responsibility. |
||
| 858 | * |
||
| 859 | * @param mixed &$entryObject Resource for which etag value needs to |
||
| 860 | * be returned |
||
| 861 | * @param ResourceType &$resourceType Resource type of the $entryObject |
||
| 862 | * |
||
| 863 | * @throws ODataException |
||
| 864 | * @throws InvalidOperationException |
||
| 865 | * @throws \ReflectionException |
||
| 866 | * @return string|null ETag value for the given resource (with values encoded |
||
| 867 | * for use in a URI) there are etag properties, NULL if |
||
| 868 | * there is no etag property |
||
| 869 | */ |
||
| 870 | protected function getETagForEntry(&$entryObject, ResourceType &$resourceType) |
||
| 906 | } |
||
| 907 | } |
||
| 908 |