Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 54 | abstract class BaseService implements IRequestHandler, IService |
||
| 55 | { |
||
| 56 | /** |
||
| 57 | * The wrapper over IQueryProvider and IMetadataProvider implementations. |
||
| 58 | * |
||
| 59 | * @var ProvidersWrapper |
||
| 60 | */ |
||
| 61 | private $providersWrapper; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The wrapper over IStreamProvider implementation |
||
| 65 | * |
||
| 66 | * @var StreamProviderWrapper |
||
| 67 | */ |
||
| 68 | private $_streamProvider; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Hold reference to the ServiceHost instance created by dispatcher, |
||
| 72 | * using this library can access headers and body of Http Request |
||
| 73 | * dispatcher received and the Http Response Dispatcher is going to send. |
||
| 74 | * |
||
| 75 | * @var ServiceHost |
||
| 76 | */ |
||
| 77 | private $_serviceHost; |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * To hold reference to ServiceConfiguration instance where the |
||
| 82 | * service specific rules (page limit, resource set access rights |
||
| 83 | * etc...) are defined. |
||
| 84 | * |
||
| 85 | * @var ServiceConfiguration |
||
| 86 | */ |
||
| 87 | private $config; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Gets reference to ServiceConfiguration instance so that |
||
| 91 | * service specific rules defined by the developer can be |
||
| 92 | * accessed. |
||
| 93 | * |
||
| 94 | * @return ServiceConfiguration |
||
| 95 | */ |
||
| 96 | public function getConfiguration() |
||
| 100 | |||
| 101 | |||
| 102 | //TODO: shouldn't we hide this from the interface..if we need it at all. |
||
| 103 | /** |
||
| 104 | * Get the wrapper over developer's IQueryProvider and IMetadataProvider implementation. |
||
| 105 | * |
||
| 106 | * @return ProvidersWrapper |
||
| 107 | */ |
||
| 108 | public function getProvidersWrapper() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Gets reference to wrapper class instance over IDSSP implementation |
||
| 115 | * |
||
| 116 | * @return StreamProviderWrapper |
||
| 117 | */ |
||
| 118 | public function getStreamProviderWrapper() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get reference to the data service host instance. |
||
| 125 | * |
||
| 126 | * @return ServiceHost |
||
| 127 | */ |
||
| 128 | public function getHost() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Sets the data service host instance. |
||
| 135 | * |
||
| 136 | * @param ServiceHost $serviceHost The data service host instance. |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function setHost(ServiceHost $serviceHost) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * To get reference to operation context where we have direct access to |
||
| 147 | * headers and body of Http Request we have received and the Http Response |
||
| 148 | * We are going to send. |
||
| 149 | * |
||
| 150 | * @return IOperationContext |
||
| 151 | */ |
||
| 152 | public function getOperationContext() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get reference to the wrapper over IStreamProvider or |
||
| 159 | * IStreamProvider2 implementations. |
||
| 160 | * |
||
| 161 | * @return StreamProviderWrapper |
||
| 162 | */ |
||
| 163 | public function getStreamProvider() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Top-level handler invoked by Dispatcher against any request to this |
||
| 175 | * service. This method will hand over request processing task to other |
||
| 176 | * functions which process the request, set required headers and Response |
||
| 177 | * stream (if any in Atom/Json format) in |
||
| 178 | * WebOperationContext::Current()::OutgoingWebResponseContext. |
||
| 179 | * Once this function returns, dispatcher uses global WebOperationContext |
||
| 180 | * to write out the request response to client. |
||
| 181 | * This function will perform the following operations: |
||
| 182 | * (1) Check whether the top level service class implements |
||
| 183 | * IServiceProvider which means the service is a custom service, in |
||
| 184 | * this case make sure the top level service class implements |
||
| 185 | * IMetaDataProvider and IQueryProvider. |
||
| 186 | * These are the minimal interfaces that a custom service to be |
||
| 187 | * implemented in order to expose its data as OData. Save reference to |
||
| 188 | * These interface implementations. |
||
| 189 | * NOTE: Here we will ensure only providers for IDSQP and IDSMP. The |
||
| 190 | * IDSSP will be ensured only when there is an GET request on MLE/Named |
||
| 191 | * stream. |
||
| 192 | * |
||
| 193 | * (2). Invoke 'Initialize' method of top level service for |
||
| 194 | * collecting the configuration rules set by the developer for this |
||
| 195 | * service. |
||
| 196 | * |
||
| 197 | * (3). Invoke the Uri processor to process the request URI. The uri |
||
| 198 | * processor will do the following: |
||
| 199 | * (a). Validate the request uri syntax using OData uri rules |
||
| 200 | * (b). Validate the request using metadata of this service |
||
| 201 | * (c). Parse the request uri and using, IQueryProvider |
||
| 202 | * implementation, fetches the resources pointed by the uri |
||
| 203 | * if required |
||
| 204 | * (d). Build a RequestDescription which encapsulate everything |
||
| 205 | * related to request uri (e.g. type of resource, result |
||
| 206 | * etc...) |
||
| 207 | * (3). Invoke handleRequest2 for further processing |
||
| 208 | * |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | public function handleRequest() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return IQueryProvider |
||
| 233 | */ |
||
| 234 | public abstract function getQueryProvider(); |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return IMetadataProvider |
||
| 238 | */ |
||
| 239 | public abstract function getMetadataProvider(); |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return \POData\Providers\Stream\IStreamProvider |
||
| 243 | */ |
||
| 244 | public abstract function getStreamProviderX(); |
||
| 245 | |||
| 246 | |||
| 247 | /** @var ODataWriterRegistry */ |
||
| 248 | private $writerRegistry; |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns the ODataWriterRegistry to use when writing the response to a service document or resource request |
||
| 252 | * @return ODataWriterRegistry |
||
| 253 | */ |
||
| 254 | public function getODataWriterRegistry() |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * This method will query and validates for IMetadataProvider and IQueryProvider implementations, invokes |
||
| 262 | * BaseService::Initialize to initialize service specific policies. |
||
| 263 | * |
||
| 264 | * |
||
| 265 | * @throws ODataException |
||
| 266 | */ |
||
| 267 | protected function createProviders() |
||
| 307 | |||
| 308 | //TODO: i don't want this to be public..but it's the only way to test it right now... |
||
| 309 | public function registerWriters() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Serialize the requested resource. |
||
| 332 | * |
||
| 333 | * @param RequestDescription $request The description of the request submitted by the client. |
||
| 334 | * @param UriProcessor $uriProcessor Reference to the uri processor. |
||
| 335 | * |
||
| 336 | * @return void |
||
| 337 | */ |
||
| 338 | protected function serializeResult(RequestDescription $request, UriProcessor $uriProcessor) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Gets the response format for the requested resource. |
||
| 497 | * |
||
| 498 | * @param RequestDescription $request The request submitted by client and it's execution result. |
||
| 499 | * @param UriProcessor $uriProcessor The reference to the UriProcessor. |
||
| 500 | * @param IService $service Reference to the service implementation instance |
||
| 501 | * |
||
| 502 | * @return string the response content-type, a null value means the requested resource |
||
| 503 | * is named stream and IDSSP2::getStreamContentType returned null |
||
| 504 | * |
||
| 505 | * @throws ODataException, HttpHeaderFailure |
||
| 506 | */ |
||
| 507 | public static function getResponseContentType( |
||
| 654 | |||
| 655 | |||
| 656 | |||
| 657 | /** |
||
| 658 | * For the given entry object compare it's eTag (if it has eTag properties) |
||
| 659 | * with current eTag request headers (if it present). |
||
| 660 | * |
||
| 661 | * @param mixed &$entryObject entity resource for which etag |
||
| 662 | * needs to be checked. |
||
| 663 | * @param ResourceType &$resourceType Resource type of the entry |
||
| 664 | * object. |
||
| 665 | * @param boolean &$needToSerializeResponse On return, this will contain |
||
| 666 | * True if response needs to be |
||
| 667 | * serialized, False otherwise. |
||
| 668 | * |
||
| 669 | * @return string|null The ETag for the entry object if it has eTag properties |
||
| 670 | * NULL otherwise. |
||
| 671 | */ |
||
| 672 | protected function compareETag(&$entryObject, ResourceType &$resourceType, |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Returns the etag for the given resource. |
||
| 754 | * Note: This function will not add W\" prefix and " suffix, its callers |
||
| 755 | * repsonsability. |
||
| 756 | * |
||
| 757 | * @param mixed &$entryObject Resource for which etag value needs to |
||
| 758 | * be returned |
||
| 759 | * @param ResourceType &$resourceType Resource type of the $entryObject |
||
| 760 | * |
||
| 761 | * @return string|null ETag value for the given resource (with values encoded |
||
| 762 | * for use in a URI) there are etag properties, NULL if |
||
| 763 | * there is no etag property. |
||
| 764 | */ |
||
| 765 | protected function getETagForEntry(&$entryObject, ResourceType &$resourceType) |
||
| 807 | |||
| 808 | /** |
||
| 809 | * This function will perform the following operations: |
||
| 810 | * (1) Invoke delegateRequestProcessing method to process the request based |
||
| 811 | * on request method (GET, PUT/MERGE, POST, DELETE) |
||
| 812 | * (3) If the result of processing of request needs to be serialized as HTTP |
||
| 813 | * response body (e.g. GET request result in single resource or resource |
||
| 814 | * collection, successful POST operation for an entity need inserted |
||
| 815 | * entity to be serialized back etc..), Serialize the result by using |
||
| 816 | * 'serializeReultForResponseBody' method |
||
| 817 | * Set the serialized result to |
||
| 818 | * WebOperationContext::Current()::OutgoingWebResponseContext::Stream. |
||
| 819 | * |
||
| 820 | * @return void |
||
| 821 | */ |
||
| 822 | protected function handleRequest2() |
||
| 825 | |||
| 826 | /** |
||
| 827 | * This method will perform the following operations: |
||
| 828 | * (1) If request method is GET, then result is already there in the |
||
| 829 | * RequestDescription so simply return the RequestDescription |
||
| 830 | * (2). If request method is for CDU |
||
| 831 | * (Create/Delete/Update - POST/DELETE/PUT-MERGE) hand |
||
| 832 | * over the responsibility to respective handlers. The handler |
||
| 833 | * methods are: |
||
| 834 | * (a) handlePOSTOperation() => POST |
||
| 835 | * (b) handlePUTOperation() => PUT/MERGE |
||
| 836 | * (c) handleDELETEOperation() => DELETE |
||
| 837 | * (3). Check whether its required to write any result to the response |
||
| 838 | * body |
||
| 839 | * (a). Request method is GET |
||
| 840 | * (b). Request is a POST for adding NEW Entry |
||
| 841 | * (c). Request is a POST for adding Media Resource Stream |
||
| 842 | * (d). Request is a POST for adding a link |
||
| 843 | * (e). Request is a DELETE for deleting entry or relationship |
||
| 844 | * (f). Request is a PUT/MERGE for updating an entry |
||
| 845 | * (g). Request is a PUT for updating a link |
||
| 846 | * In case a, b and c we need to write the result to response body, |
||
| 847 | * for d, e, f and g no body content. |
||
| 848 | * |
||
| 849 | * @return RequestDescription|null Instance of RequestDescription with |
||
| 850 | * result to be write back Null if no result to write. |
||
| 851 | */ |
||
| 852 | protected function delegateRequestProcessing() |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Serialize the result in the current request description using |
||
| 858 | * appropriate odata writer (AtomODataWriter/JSONODataWriter) |
||
| 859 | * |
||
| 860 | * @return void |
||
| 861 | * |
||
| 862 | */ |
||
| 863 | protected function serializeReultForResponseBody() |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Handle POST request. |
||
| 869 | * |
||
| 870 | * @return void |
||
| 871 | * |
||
| 872 | * @throws NotImplementedException |
||
| 873 | */ |
||
| 874 | protected function handlePOSTOperation() |
||
| 877 | |||
| 878 | /** |
||
| 879 | * Handle PUT/MERGE request. |
||
| 880 | * |
||
| 881 | * @return void |
||
| 882 | * |
||
| 883 | * @throws NotImplementedException |
||
| 884 | */ |
||
| 885 | protected function handlePUTOperation() |
||
| 888 | |||
| 889 | /** |
||
| 890 | * Handle DELETE request. |
||
| 891 | * |
||
| 892 | * @return void |
||
| 893 | * |
||
| 894 | * @throws NotImplementedException |
||
| 895 | */ |
||
| 896 | protected function handleDELETEOperation() |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Assert that the given condition is true. |
||
| 902 | * |
||
| 903 | * @param boolean $condition The condtion to check. |
||
| 904 | * @param string $conditionAsString Message to show if assertion fails. |
||
| 905 | * |
||
| 906 | * @return void |
||
| 907 | * |
||
| 908 | * @throws InvalidOperationException |
||
| 909 | */ |
||
| 910 | protected static function assert($condition, $conditionAsString) |
||
| 918 | } |