Skip to content

Commit

Permalink
🐛 misc bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasDoebertin committed Mar 22, 2018
1 parent 36e8dbc commit fd5c3e7
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/tinker.php

# Created by https://www.gitignore.io/api/linux,macos,windows,composer,phpstorm

Expand Down
38 changes: 13 additions & 25 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,20 @@ class Client
*/
const API_TIMEZONE = 'Europe/Berlin';

/**
* The production api base uri.
*/
const API_HOST_PRODUCTION = 'https://api.geofox.de/gti/public/';

/**
* The test api base uri.
*/
const API_HOST_TEST = 'https://api-test.geofox.de/gti/public/';

/**
* @var string
*/
protected $applicationId;
protected $username;

/**
* @var string
*/
protected $password;

/**
* @var bool
* @var string
*/
protected $useTestApi;
protected $host;

/**
* @var \JdPowered\Geofox\Enum\Platform
Expand All @@ -78,20 +68,20 @@ class Client
/**
* Client constructor.
*
* @param string $applicationId
* @param string $username
* @param string $password
* @param bool $useTestApi
* @param string $host
* @param string $platform
*/
public function __construct(
string $applicationId,
string $username,
string $password,
bool $useTestApi = false,
string $host,
string $platform = Platform::WEB
) {
$this->applicationId = $applicationId;
$this->username = $username;
$this->password = $password;
$this->useTestApi = $useTestApi;
$this->host = $host;
$this->platform = Platform::get($platform);
}

Expand All @@ -114,9 +104,7 @@ public function listStations()
*/
public function fetch(BaseRequest $request): BaseResponse
{
$client = $this->client();

$result = $client->send($request->httpRequest(), $this->requestOptions());
$result = $this->client()->send($request->httpRequest(), $this->requestOptions());

return new $this->requestResponseMap[get_class($request)](
$result->getStatusCode(),
Expand All @@ -137,13 +125,13 @@ protected function client(): Guzzle
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
return $request->withHeader(
'geofox-auth-signature',
$this->generateRequestSignature($request->getBody())
$this->generateRequestSignature($request)
);
}));

$this->httpClient = new Guzzle([
'handler' => $stack,
'base_uri' => $this->useTestApi ? self::API_HOST_TEST : self::API_HOST_PRODUCTION,
'base_uri' => $this->host,
]);
}

Expand Down Expand Up @@ -184,7 +172,7 @@ protected function requestHeaders(): array
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip, deflate',
'Accept' => 'application/json',
'geofox-auth-user' => $this->applicationId,
'geofox-auth-user' => $this->username,
'geofox-auth-type' => 'HmacSHA1',
'X-Platform' => $this->platform->getValue(),
'X-TraceId' => Uuid::uuid4(),
Expand Down
10 changes: 5 additions & 5 deletions src/Enum/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class Platform extends Enum
{
const IOS = 'ios';
const ANDROID = 'android';
const WINPHONE = 'winphone';
const WEB = 'web';
const MOBILE = 'mobile';
const IOS = 'IOS';
const ANDROID = 'ANDROID';
const WINPHONE = 'WINPHONE';
const WEB = 'WEB';
const MOBILE = 'MOBILE';
}
18 changes: 10 additions & 8 deletions src/Objects/StationListEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function __construct(Data $data = null)
* @param array $shortcuts
* @return StationListEntry
*/
public function setShortcuts(array $shortcuts): self
public function setShortcuts(?array $shortcuts): self
{
$this->shortcuts = $shortcuts;
$this->shortcuts = $shortcuts ?? [];

return $this;
}
Expand All @@ -76,9 +76,9 @@ public function getShortcuts(): array
* @param array $aliasses
* @return StationListEntry
*/
public function setAliasses(array $aliasses): self
public function setAliasses(?array $aliasses): self
{
$this->aliasses = $aliasses;
$this->aliasses = $aliasses ?? [];

return $this;
}
Expand All @@ -99,12 +99,14 @@ public function getAliasses(): array
* @param array $vehicleTypes
* @return StationListEntry
*/
public function setVehicleTypes(array $vehicleTypes): self
public function setVehicleTypes(?array $vehicleTypes): self
{
$this->vehicleTypes = new Set(VehicleType::class);

foreach ($vehicleTypes as $vehicleType) {
$this->vehicleTypes->attach(VehicleType::get($vehicleType));
if (is_array($vehicleTypes)) {
foreach ($vehicleTypes as $vehicleType) {
$this->vehicleTypes->attach(VehicleType::get($vehicleType));
}
}

return $this;
Expand All @@ -126,7 +128,7 @@ public function getVehicleTypes(): Set
* @param bool $exists
* @return StationListEntry
*/
public function setExists(bool $exists): self
public function setExists(?bool $exists = true): self
{
$this->exists = $exists;

Expand Down
4 changes: 2 additions & 2 deletions src/Request/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function get()
*/
public function httpRequest(): GuzzleRequest
{
return new GuzzleRequest('POST', $this->uri(), [], $this->httpBody());
return new GuzzleRequest('POST', $this->uri(), [], json_encode($this->httpBody()));
}

/**
Expand All @@ -129,7 +129,7 @@ public function httpRequest(): GuzzleRequest
protected function httpBody(): array
{
return [
'language' => $this->language,
'language' => $this->language->getValue(),
'version' => $this->version,
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Request/DepartureList.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,6 @@ protected function httpBody(): array
*/
protected function uri(): string
{
return 'departureList';
return '/gti/public/departureList';
}
}
2 changes: 1 addition & 1 deletion src/Request/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class Init extends Base
*/
protected function uri(): string
{
return 'init';
return '/gti/public/init';
}
}
10 changes: 5 additions & 5 deletions src/Request/ListStations.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ protected function setDefaults()
protected function httpBody(): array
{
return array_merge(parent::httpBody(), [
'dataReleaseId' => $this->getDataReleaseId(),
'modificationsTypes' => $this->getModificationTypes()->getValues(),
'coordinateType' => $this->getCoordinateType(),
'filterEquivalent' => $this->getFilterEquivalent(),
'dataReleaseID' => $this->getDataReleaseId(),
'modificationTypes' => $this->getModificationTypes()->getValues(),
'coordinateType' => $this->getCoordinateType()->getValue(),
'filterEquivalent' => $this->getFilterEquivalent(),
]);
}

Expand All @@ -160,6 +160,6 @@ protected function httpBody(): array
*/
protected function uri(): string
{
return 'listStations';
return '/gti/public/listStations';
}
}
37 changes: 33 additions & 4 deletions src/Response/ListStations.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

class ListStations extends Base
{
/**
* @var string
*/
protected $dataReleaseId;

/**
* @var \JdPowered\Geofox\Objects\StationListEntry[]
*/
Expand All @@ -22,7 +27,31 @@ public function __construct(int $statusCode, Data $data)
{
parent::__construct($statusCode, $data);

$this->setStations($data->stations);
$this->setDataReleaseId($data->dataReleaseID)
->setStations($data->stations);
}

/**
* Get data release id.
*
* @return string
*/
public function getDataReleaseId(): string
{
return $this->dataReleaseId;
}

/**
* Set data release id.
*
* @param string $dataReleaseId
* @return \JdPowered\Geofox\Response\ListStations
*/
protected function setDataReleaseId(string $dataReleaseId): self
{
$this->dataReleaseId = $dataReleaseId;

return $this;
}

/**
Expand All @@ -38,14 +67,14 @@ public function getStations(): array
/**
* Set stations.
*
* @param array $stations
* @param array|null $stations
* @return \JdPowered\Geofox\Response\ListStations
*/
protected function setStations(array $stations = []): self
protected function setStations(?array $stations): self
{
$this->stations = array_map(function (Data $station) {
return new StationListEntry($station);
}, $stations);
}, $stations ?? []);

return $this;
}
Expand Down

0 comments on commit fd5c3e7

Please sign in to comment.