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