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