/** * Creates and posts a service request * * @param string $serviceCode * @param float $lat * @param float $lng * @param string $addressString * @param string $addressId * @param string $email * @param string $deviceId * @param string $accountId * @param string $firstName * @param string $lastName * @param string $phone * @param string $description * @param string $mediaUrl * @param string $format * @return ServiceRequestResponse * @throws Open311Exception */ public function createServiceRequest($serviceCode, $lat, $lng, $addressString, $addressId = '', $email = '', $deviceId = '', $accountId = '', $firstName = '', $lastName = '', $phone = '', $description = '', $mediaUrl = '', $format = self::FORMAT_JSON) { $data = array(ServiceRequest::FIELD_SERVICE_CODE => $serviceCode, ServiceRequest::FIELD_LATITUDE => !is_null($lat) && is_numeric($lat) ? (double) $lat : null, ServiceRequest::FIELD_LONGITUDE => !is_null($lng) && is_numeric($lng) ? (double) $lng : null, ServiceRequest::FIELD_ADDRESS_STRING => $addressString, ServiceRequest::FIELD_ADDRESS_ID => $addressId, ServiceRequest::FIELD_EMAIL => $email, ServiceRequest::FIELD_DEVICE_ID => $deviceId, ServiceRequest::FIELD_ACCOUNT_ID => $accountId, ServiceRequest::FIELD_FIRST_NAME => $firstName, ServiceRequest::FIELD_LAST_NAME => $lastName, ServiceRequest::FIELD_PHONE => $phone, ServiceRequest::FIELD_DESCRIPTION => $description, ServiceRequest::FIELD_MEDIA_URL => $mediaUrl); try { return $this->postServiceRequest(ServiceRequest::fromArray($data, $this->serviceRequestRequiredFields), $format); } catch (Open311Exception $e) { throw $e; } }
/** * Returns a service request by id * * @param string|integer|null $serviceRequestId * @param string $format * @return ServiceRequest|null */ public function getServiceRequest($serviceRequestId = null, $format = self::FORMAT_JSON) { if (!empty($serviceRequestId)) { try { $uri = implode('/', array(self::COMMAND_SERVICE_REQUESTS, $serviceRequestId)); $response = $this->get($uri, $format); if (!is_null($response)) { $responseData = $this->responseToArray($response); if ($this->validResponseArray($responseData)) { return ServiceRequest::fromArray($responseData[0], $this->serviceRequestRequiredFields); } } } catch (GuzzleException $e) { return null; } catch (Open311Exception $e) { return null; } } return null; }
/** * @param mixed $serviceRequest * @param bool $expectSuccess * * @covers \GeoPal\Open311\BaseClient::postServiceRequest * @dataProvider providerTestPostServiceRequest */ public function testPostServiceRequest($serviceRequest, $expectSuccess) { $stubResponse = array(array(array(ServiceRequest::FIELD_SERVICE_REQUEST_ID => 'stub_id'))); /** * Create test client and do call */ $stubClient = new StubClient($this->getStubGuzzleClient($stubResponse)); if ($expectSuccess) { $response = $stubClient->postServiceRequest($serviceRequest); $this->assertInstanceOf('\\GeoPal\\Open311\\ServiceRequestResponse', $response); } else { $result = false; $exceptionThrown = false; try { // Throw an exception or change $result from false to null $result = $stubClient->postServiceRequest(ServiceRequest::fromArray($serviceRequest)); } catch (Open311Exception $e) { $exceptionThrown = true; } $this->assertTrue($exceptionThrown || is_null($result)); } }