1 | <?php |
||
30 | class Service |
||
31 | { |
||
32 | /** |
||
33 | * Drest Manager |
||
34 | * @var Manager $dm |
||
35 | */ |
||
36 | protected $dm; |
||
37 | |||
38 | /** |
||
39 | * Service Action Registry |
||
40 | * @var Service\Action\Registry |
||
41 | */ |
||
42 | protected $service_action_registry; |
||
43 | |||
44 | /** |
||
45 | * When a route object is matched, it's injected into the service class |
||
46 | * @var RouteMetaData $route |
||
47 | */ |
||
48 | protected $matched_route; |
||
49 | |||
50 | /** |
||
51 | * Service action instance determined |
||
52 | * @var AbstractAction $service_action |
||
53 | */ |
||
54 | private $service_action; |
||
55 | |||
56 | /** |
||
57 | * A representation instance determined either from configuration or client media accept type - can be null if none matched |
||
58 | * Note this will be present on both a fetch (GET) request and on a push (POST / PUT) request if using the drest client |
||
59 | * @var Representation\AbstractRepresentation $representation |
||
60 | */ |
||
61 | protected $representation; |
||
62 | |||
63 | /** |
||
64 | * The error handler instance |
||
65 | * @var AbstractHandler $error_handler |
||
66 | */ |
||
67 | protected $error_handler; |
||
68 | |||
69 | /** |
||
70 | * Initialise a new instance of a Drest service |
||
71 | * @param Manager $dm - The Drest Manager object |
||
72 | * @param Service\Action\Registry $service_action_registry - registry use to look up service actions |
||
73 | */ |
||
74 | 31 | public function __construct(Manager $dm, Service\Action\Registry $service_action_registry) |
|
75 | { |
||
76 | 31 | $this->dm = $dm; |
|
77 | 31 | $this->service_action_registry = $service_action_registry; |
|
78 | 31 | } |
|
79 | |||
80 | |||
81 | /** |
||
82 | * Set up and run the required call method |
||
83 | */ |
||
84 | 25 | public function setUpAndRunRequest() |
|
90 | |||
91 | /** |
||
92 | * Called on successful routing of a service call |
||
93 | * Prepares the service to a request to be rendered |
||
94 | * |
||
95 | * @return boolean $result - if false then fail fast no call to runCallMethod() should be made. |
||
96 | */ |
||
97 | 25 | protected function setupRequest() |
|
123 | |||
124 | /** |
||
125 | * Get an instance of the action class to be used |
||
126 | * @throws DrestException |
||
127 | * @return AbstractAction $action |
||
128 | */ |
||
129 | 25 | protected function getActionInstance() |
|
149 | |||
150 | /** |
||
151 | * Run the call method required on this service object |
||
152 | */ |
||
153 | 25 | final public function runCallMethod() |
|
167 | |||
168 | /** |
||
169 | * Gets an instance of the "default" action based of request information |
||
170 | * @throws DrestException |
||
171 | * @return AbstractAction $action |
||
172 | */ |
||
173 | 25 | protected function getDefaultAction() |
|
198 | |||
199 | /** |
||
200 | * Get the service action registry |
||
201 | * @return Service\Action\Registry $service_action_registry |
||
202 | */ |
||
203 | public function getServiceActionRegistry() |
||
207 | |||
208 | /** |
||
209 | * Set the matched route object |
||
210 | * @param RouteMetaData $matched_route |
||
211 | */ |
||
212 | 25 | public function setMatchedRoute(RouteMetaData $matched_route) |
|
216 | |||
217 | /** |
||
218 | * Get the route object that was matched |
||
219 | * @return RouteMetaData $matched_route |
||
220 | */ |
||
221 | 25 | public function getMatchedRoute() |
|
225 | |||
226 | /** |
||
227 | * Set any predetermined representation instance |
||
228 | * @param Representation\AbstractRepresentation $representation |
||
229 | */ |
||
230 | 25 | public function setRepresentation(Representation\AbstractRepresentation $representation) |
|
234 | |||
235 | /** |
||
236 | * Get the predetermined representation |
||
237 | * @return Representation\AbstractRepresentation |
||
238 | */ |
||
239 | 3 | public function getRepresentation() |
|
243 | |||
244 | /** |
||
245 | * Set the error handler object |
||
246 | * @param AbstractHandler $error_handler |
||
247 | */ |
||
248 | 25 | public function setErrorHandler(AbstractHandler $error_handler) |
|
252 | |||
253 | /** |
||
254 | * Get the entity manager |
||
255 | * This will return the default manager, to choose a specific one use getEntityManagerRegistry() |
||
256 | * @return EntityManager $em |
||
257 | */ |
||
258 | public function getEntityManager() |
||
262 | |||
263 | /** |
||
264 | * Get the entity manager registry |
||
265 | * @return EntityManagerRegistry |
||
266 | */ |
||
267 | 25 | public function getEntityManagerRegistry() |
|
271 | |||
272 | /** |
||
273 | * Get the Drest Manager |
||
274 | * @return Manager $dm |
||
275 | */ |
||
276 | public function getDrestManager() |
||
280 | |||
281 | /** |
||
282 | * Get the response object |
||
283 | * @return Response $response |
||
284 | */ |
||
285 | 25 | public function getResponse() |
|
289 | |||
290 | /** |
||
291 | * Get the request object |
||
292 | * @return Request $request |
||
293 | */ |
||
294 | 25 | public function getRequest() |
|
298 | |||
299 | /** |
||
300 | * Handle an error - set the resulting error document to the response object |
||
301 | * @param \Exception $e |
||
302 | * @param integer $defaultResponseCode the default response code to use if no match on exception type occurs |
||
303 | * @param ResponseInterface $errorDocument |
||
304 | * @return ResultSet the error result set |
||
305 | */ |
||
306 | 5 | public function handleError(\Exception $e, $defaultResponseCode = 500, ResponseInterface $errorDocument = null) |
|
318 | |||
319 | |||
320 | /** |
||
321 | * Write out as result set on the representation object that was determined - if no representation has been determined - defaults to text |
||
322 | * @param ResultSet $resultSet |
||
323 | */ |
||
324 | 20 | public function renderDeterminedRepresentation(ResultSet $resultSet) |
|
329 | } |
||
330 |