Total Complexity | 101 |
Total Lines | 778 |
Duplicated Lines | 0 % |
Changes | 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 |
||
57 | abstract class BaseService implements IRequestHandler, IService |
||
58 | { |
||
59 | /** |
||
60 | * The wrapper over IQueryProvider and IMetadataProvider implementations. |
||
61 | * |
||
62 | * @var ProvidersWrapper |
||
63 | */ |
||
64 | private $providersWrapper; |
||
65 | |||
66 | /** |
||
67 | * The wrapper over IStreamProvider implementation. |
||
68 | * |
||
69 | * @var StreamProviderWrapper |
||
70 | */ |
||
71 | protected $streamProvider; |
||
72 | |||
73 | /** |
||
74 | * Hold reference to the ServiceHost instance created by dispatcher, |
||
75 | * using this library can access headers and body of Http Request |
||
76 | * dispatcher received and the Http Response Dispatcher is going to send. |
||
77 | * |
||
78 | * @var ServiceHost |
||
79 | */ |
||
80 | private $serviceHost; |
||
81 | |||
82 | /** |
||
83 | * To hold reference to ServiceConfiguration instance where the |
||
84 | * service specific rules (page limit, resource set access rights |
||
85 | * etc...) are defined. |
||
86 | * |
||
87 | * @var IServiceConfiguration |
||
88 | */ |
||
89 | protected $config; |
||
90 | |||
91 | /** |
||
92 | * Hold reference to object serialiser - bit wot turns PHP objects |
||
93 | * into message traffic on wire. |
||
94 | * |
||
95 | * @var IObjectSerialiser |
||
96 | */ |
||
97 | protected $objectSerialiser; |
||
98 | |||
99 | /** |
||
100 | * Get reference to object serialiser - bit wot turns PHP objects |
||
101 | * into message traffic on wire. |
||
102 | * |
||
103 | * @return IObjectSerialiser |
||
104 | */ |
||
105 | public function getObjectSerialiser() |
||
106 | { |
||
107 | assert(null != $this->objectSerialiser); |
||
|
|||
108 | |||
109 | return $this->objectSerialiser; |
||
110 | } |
||
111 | |||
112 | protected function __construct(IObjectSerialiser $serialiser = null) |
||
113 | { |
||
114 | if (null != $serialiser) { |
||
115 | $serialiser->setService($this); |
||
116 | } else { |
||
117 | $serialiser = new ObjectModelSerializer($this, null); |
||
118 | } |
||
119 | $this->objectSerialiser = $serialiser; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Gets reference to ServiceConfiguration instance so that |
||
124 | * service specific rules defined by the developer can be |
||
125 | * accessed. |
||
126 | * |
||
127 | * @return IServiceConfiguration |
||
128 | */ |
||
129 | public function getConfiguration() |
||
130 | { |
||
131 | assert(null != $this->config); |
||
132 | |||
133 | return $this->config; |
||
134 | } |
||
135 | |||
136 | //TODO: shouldn't we hide this from the interface..if we need it at all. |
||
137 | |||
138 | /** |
||
139 | * Get the wrapper over developer's IQueryProvider and IMetadataProvider implementation. |
||
140 | * |
||
141 | * @return ProvidersWrapper |
||
142 | */ |
||
143 | public function getProvidersWrapper() |
||
144 | { |
||
145 | return $this->providersWrapper; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Gets reference to wrapper class instance over IDSSP implementation. |
||
150 | * |
||
151 | * @return StreamProviderWrapper |
||
152 | */ |
||
153 | public function getStreamProviderWrapper() |
||
154 | { |
||
155 | return $this->streamProvider; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Get reference to the data service host instance. |
||
160 | * |
||
161 | * @return ServiceHost |
||
162 | */ |
||
163 | public function getHost() |
||
164 | { |
||
165 | assert(null != $this->serviceHost); |
||
166 | |||
167 | return $this->serviceHost; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Sets the data service host instance. |
||
172 | * |
||
173 | * @param ServiceHost $serviceHost The data service host instance |
||
174 | */ |
||
175 | public function setHost(ServiceHost $serviceHost) |
||
176 | { |
||
177 | $this->serviceHost = $serviceHost; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * To get reference to operation context where we have direct access to |
||
182 | * headers and body of Http Request, we have received and the Http Response |
||
183 | * We are going to send. |
||
184 | * |
||
185 | * @return IOperationContext |
||
186 | */ |
||
187 | public function getOperationContext() |
||
188 | { |
||
189 | return $this->getHost()->getOperationContext(); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Get reference to the wrapper over IStreamProvider or |
||
194 | * IStreamProvider2 implementations. |
||
195 | * |
||
196 | * @return StreamProviderWrapper |
||
197 | */ |
||
198 | public function getStreamProvider() |
||
199 | { |
||
200 | if (null === $this->streamProvider) { |
||
201 | $this->streamProvider = new StreamProviderWrapper(); |
||
202 | $this->streamProvider->setService($this); |
||
203 | } |
||
204 | |||
205 | return $this->streamProvider; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Top-level handler invoked by Dispatcher against any request to this |
||
210 | * service. This method will hand over request processing task to other |
||
211 | * functions which process the request, set required headers and Response |
||
212 | * stream (if any in Atom/Json format) in |
||
213 | * WebOperationContext::Current()::OutgoingWebResponseContext. |
||
214 | * Once this function returns, dispatcher uses global WebOperationContext |
||
215 | * to write out the request response to client. |
||
216 | * This function will perform the following operations: |
||
217 | * (1) Check whether the top level service class implements |
||
218 | * IServiceProvider which means the service is a custom service, in |
||
219 | * this case make sure the top level service class implements |
||
220 | * IMetaDataProvider and IQueryProvider. |
||
221 | * These are the minimal interfaces that a custom service to be |
||
222 | * implemented in order to expose its data as OData. Save reference to |
||
223 | * These interface implementations. |
||
224 | * NOTE: Here we will ensure only providers for IDSQP and IDSMP. The |
||
225 | * IDSSP will be ensured only when there is an GET request on MLE/Named |
||
226 | * stream. |
||
227 | * |
||
228 | * (2). Invoke 'Initialize' method of top level service for |
||
229 | * collecting the configuration rules set by the developer for this |
||
230 | * service. |
||
231 | * |
||
232 | * (3). Invoke the Uri processor to process the request URI. The uri |
||
233 | * processor will do the following: |
||
234 | * (a). Validate the request uri syntax using OData uri rules |
||
235 | * (b). Validate the request using metadata of this service |
||
236 | * (c). Parse the request uri and using, IQueryProvider |
||
237 | * implementation, fetches the resources pointed by the uri |
||
238 | * if required |
||
239 | * (d). Build a RequestDescription which encapsulate everything |
||
240 | * related to request uri (e.g. type of resource, result |
||
241 | * etc...) |
||
242 | * (3). Invoke handleRequest2 for further processing |
||
243 | */ |
||
244 | public function handleRequest() |
||
245 | { |
||
246 | try { |
||
247 | $this->createProviders(); |
||
248 | $this->getHost()->validateQueryParameters(); |
||
249 | $uriProcessor = UriProcessorNew::process($this); |
||
250 | $request = $uriProcessor->getRequest(); |
||
251 | if (TargetKind::BATCH() == $request->getTargetKind()) { |
||
252 | //dd($request); |
||
253 | $this->handleBatchRequest($request); |
||
254 | } else { |
||
255 | $this->serializeResult($request, $uriProcessor); |
||
256 | } |
||
257 | } catch (\Exception $exception) { |
||
258 | ErrorHandler::handleException($exception, $this); |
||
259 | // Return to dispatcher for writing serialized exception |
||
260 | return; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | private function handleBatchRequest($request) |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @return IQueryProvider |
||
281 | */ |
||
282 | abstract public function getQueryProvider(); |
||
283 | |||
284 | /** |
||
285 | * @return IMetadataProvider |
||
286 | */ |
||
287 | abstract public function getMetadataProvider(); |
||
288 | |||
289 | /** |
||
290 | * @return \POData\Providers\Stream\IStreamProvider2 |
||
291 | */ |
||
292 | abstract public function getStreamProviderX(); |
||
293 | |||
294 | /** @var ODataWriterRegistry */ |
||
295 | protected $writerRegistry; |
||
296 | |||
297 | /** |
||
298 | * Returns the ODataWriterRegistry to use when writing the response to a service document or resource request. |
||
299 | * |
||
300 | * @return ODataWriterRegistry |
||
301 | */ |
||
302 | public function getODataWriterRegistry() |
||
303 | { |
||
304 | assert(null != $this->writerRegistry); |
||
305 | |||
306 | return $this->writerRegistry; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * This method will query and validates for IMetadataProvider and IQueryProvider implementations, invokes |
||
311 | * BaseService::Initialize to initialize service specific policies. |
||
312 | * |
||
313 | * @throws ODataException |
||
314 | */ |
||
315 | protected function createProviders() |
||
348 | } |
||
349 | |||
350 | //TODO: i don't want this to be public..but it's the only way to test it right now... |
||
351 | public function registerWriters() |
||
352 | { |
||
353 | $registry = $this->getODataWriterRegistry(); |
||
354 | $serviceVersion = $this->getConfiguration()->getMaxDataServiceVersion(); |
||
355 | $serviceURI = $this->getHost()->getAbsoluteServiceUri()->getUrlAsString(); |
||
356 | |||
357 | //We always register the v1 stuff |
||
358 | $registry->register(new JsonODataV1Writer()); |
||
359 | $registry->register(new AtomODataWriter($serviceURI)); |
||
360 | |||
361 | if (-1 < $serviceVersion->compare(Version::v2())) { |
||
362 | $registry->register(new JsonODataV2Writer()); |
||
363 | } |
||
364 | |||
365 | if (-1 < $serviceVersion->compare(Version::v3())) { |
||
366 | $registry->register(new JsonLightODataWriter(JsonLightMetadataLevel::NONE(), $serviceURI)); |
||
367 | $registry->register(new JsonLightODataWriter(JsonLightMetadataLevel::MINIMAL(), $serviceURI)); |
||
368 | $registry->register(new JsonLightODataWriter(JsonLightMetadataLevel::FULL(), $serviceURI)); |
||
369 | } |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Serialize the requested resource. |
||
374 | * |
||
375 | * @param RequestDescription $request The description of the request submitted by the client |
||
376 | * @param IUriProcessor $uriProcessor Reference to the uri processor |
||
377 | * |
||
378 | * @throws ODataException |
||
379 | */ |
||
380 | protected function serializeResult(RequestDescription $request, IUriProcessor $uriProcessor) |
||
545 | } |
||
546 | } |
||
547 | |||
548 | /** |
||
549 | * Gets the response format for the requested resource. |
||
550 | * |
||
551 | * @param RequestDescription $request The request submitted by client and it's execution result |
||
552 | * @param IUriProcessor $uriProcessor The reference to the IUriProcessor |
||
553 | * |
||
554 | * @throws ODataException, HttpHeaderFailure |
||
555 | * |
||
556 | * @return string|null the response content-type, a null value means the requested resource |
||
557 | * is named stream and IDSSP2::getStreamContentType returned null |
||
558 | */ |
||
559 | public function getResponseContentType( |
||
560 | RequestDescription $request, |
||
561 | IUriProcessor $uriProcessor |
||
562 | ) { |
||
563 | $baseMimeTypes = [ |
||
564 | MimeTypes::MIME_APPLICATION_JSON, |
||
565 | MimeTypes::MIME_APPLICATION_JSON_FULL_META, |
||
566 | MimeTypes::MIME_APPLICATION_JSON_NO_META, |
||
567 | MimeTypes::MIME_APPLICATION_JSON_MINIMAL_META, |
||
568 | MimeTypes::MIME_APPLICATION_JSON_VERBOSE, ]; |
||
569 | |||
570 | // The Accept request-header field specifies media types which are acceptable for the response |
||
571 | |||
572 | $host = $this->getHost(); |
||
573 | $requestAcceptText = $host->getRequestAccept(); |
||
574 | $requestVersion = $request->getResponseVersion(); |
||
575 | |||
576 | //if the $format header is present it overrides the accepts header |
||
577 | $format = $host->getQueryStringItem(ODataConstants::HTTPQUERY_STRING_FORMAT); |
||
578 | if (null !== $format) { |
||
579 | //There's a strange edge case..if application/json is supplied and it's V3 |
||
580 | if (MimeTypes::MIME_APPLICATION_JSON == $format && Version::v3() == $requestVersion) { |
||
581 | //then it's actual minimalmetadata |
||
582 | //TODO: should this be done with the header text too? |
||
583 | $format = MimeTypes::MIME_APPLICATION_JSON_MINIMAL_META; |
||
584 | } |
||
585 | |||
586 | $requestAcceptText = ServiceHost::translateFormatToMime($requestVersion, $format); |
||
587 | } |
||
588 | |||
589 | //The response format can be dictated by the target resource kind. IE a $value will be different then expected |
||
590 | //getTargetKind doesn't deal with link resources directly and this can change things |
||
591 | $targetKind = $request->isLinkUri() ? TargetKind::LINK() : $request->getTargetKind(); |
||
592 | assert(is_string($requestAcceptText) || !isset($requestAcceptText)); |
||
593 | |||
594 | switch ($targetKind) { |
||
595 | case TargetKind::METADATA(): |
||
596 | return HttpProcessUtility::selectMimeType( |
||
597 | $requestAcceptText, |
||
598 | [MimeTypes::MIME_APPLICATION_XML] |
||
599 | ); |
||
600 | |||
601 | case TargetKind::SERVICE_DIRECTORY(): |
||
602 | return HttpProcessUtility::selectMimeType( |
||
603 | $requestAcceptText, |
||
604 | array_merge( |
||
605 | [MimeTypes::MIME_APPLICATION_ATOMSERVICE], |
||
606 | $baseMimeTypes |
||
607 | ) |
||
608 | ); |
||
609 | |||
610 | case TargetKind::PRIMITIVE_VALUE(): |
||
611 | $supportedResponseMimeTypes = [MimeTypes::MIME_TEXTPLAIN]; |
||
612 | |||
613 | if ('$count' != $request->getIdentifier()) { |
||
614 | $projectedProperty = $request->getProjectedProperty(); |
||
615 | assert(null !== $projectedProperty, 'is_null($projectedProperty)'); |
||
616 | $type = $projectedProperty->getInstanceType(); |
||
617 | assert($type instanceof IType, '!$type instanceof IType'); |
||
618 | if ($type instanceof Binary) { |
||
619 | $supportedResponseMimeTypes = [MimeTypes::MIME_APPLICATION_OCTETSTREAM]; |
||
620 | } |
||
621 | } |
||
622 | |||
623 | return HttpProcessUtility::selectMimeType( |
||
624 | $requestAcceptText, |
||
625 | $supportedResponseMimeTypes |
||
626 | ); |
||
627 | |||
628 | case TargetKind::PRIMITIVE(): |
||
629 | case TargetKind::COMPLEX_OBJECT(): |
||
630 | case TargetKind::BAG(): |
||
631 | case TargetKind::LINK(): |
||
632 | return HttpProcessUtility::selectMimeType( |
||
633 | $requestAcceptText, |
||
634 | array_merge( |
||
635 | [MimeTypes::MIME_APPLICATION_XML, |
||
636 | MimeTypes::MIME_TEXTXML, ], |
||
637 | $baseMimeTypes |
||
638 | ) |
||
639 | ); |
||
640 | |||
641 | case TargetKind::SINGLETON(): |
||
642 | case TargetKind::RESOURCE(): |
||
643 | return HttpProcessUtility::selectMimeType( |
||
644 | $requestAcceptText, |
||
645 | array_merge( |
||
646 | [MimeTypes::MIME_APPLICATION_ATOM], |
||
647 | $baseMimeTypes |
||
648 | ) |
||
649 | ); |
||
650 | |||
651 | case TargetKind::MEDIA_RESOURCE(): |
||
652 | if (!$request->isNamedStream() && !$request->getTargetResourceType()->isMediaLinkEntry()) { |
||
653 | throw ODataException::createBadRequestError( |
||
654 | Messages::badRequestInvalidUriForMediaResource( |
||
655 | $host->getAbsoluteRequestUri()->getUrlAsString() |
||
656 | ) |
||
657 | ); |
||
658 | } |
||
659 | |||
660 | $uriProcessor->execute(); |
||
661 | $request->setExecuted(); |
||
662 | // DSSW::getStreamContentType can throw error in 2 cases |
||
663 | // 1. If the required stream implementation not found |
||
664 | // 2. If IDSSP::getStreamContentType returns NULL for MLE |
||
665 | $responseContentType = $this->getStreamProviderWrapper() |
||
666 | ->getStreamContentType( |
||
667 | $request->getTargetResult(), |
||
668 | $request->getResourceStreamInfo() |
||
669 | ); |
||
670 | |||
671 | // Note StreamWrapper::getStreamContentType can return NULL if the requested named stream has not |
||
672 | // yet been uploaded. But for an MLE if IDSSP::getStreamContentType returns NULL |
||
673 | // then StreamWrapper will throw error |
||
674 | if (null !== $responseContentType) { |
||
675 | $responseContentType = HttpProcessUtility::selectMimeType( |
||
676 | $requestAcceptText, |
||
677 | [$responseContentType] |
||
678 | ); |
||
679 | } |
||
680 | |||
681 | return $responseContentType; |
||
682 | } |
||
683 | |||
684 | //If we got here, we just don't know what it is... |
||
685 | throw new ODataException(Messages::unsupportedMediaType(), 415); |
||
686 | } |
||
687 | |||
688 | /** |
||
689 | * For the given entry object compare its eTag (if it has eTag properties) |
||
690 | * with current eTag request headers (if present). |
||
691 | * |
||
692 | * @param mixed &$entryObject entity resource for which etag |
||
693 | * needs to be checked |
||
694 | * @param ResourceType &$resourceType Resource type of the entry |
||
695 | * object |
||
696 | * @param bool &$needToSerializeResponse On return, this will contain |
||
697 | * True if response needs to be |
||
698 | * serialized, False otherwise |
||
699 | * @param bool $needToSerializeResponse |
||
700 | * |
||
701 | * @throws ODataException |
||
702 | * @return string|null The ETag for the entry object if it has eTag properties |
||
703 | * NULL otherwise |
||
704 | */ |
||
705 | protected function compareETag( |
||
785 | } |
||
786 | |||
787 | /** |
||
788 | * Returns the etag for the given resource. |
||
789 | * Note: This function will not add W\" prefix and " suffix, that is caller's |
||
790 | * responsibility. |
||
791 | * |
||
792 | * @param mixed &$entryObject Resource for which etag value needs to |
||
793 | * be returned |
||
794 | * @param ResourceType &$resourceType Resource type of the $entryObject |
||
795 | * |
||
796 | * @throws ODataException |
||
797 | * @return string|null ETag value for the given resource (with values encoded |
||
798 | * for use in a URI) there are etag properties, NULL if |
||
799 | * there is no etag property |
||
800 | */ |
||
801 | protected function getETagForEntry(&$entryObject, ResourceType &$resourceType) |
||
835 | } |
||
836 | } |
||
837 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.