Skip to content

Commit

Permalink
Migrate to PHP 8+
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kurowski committed Feb 4, 2021
1 parent 7485f4c commit 9f341c6
Show file tree
Hide file tree
Showing 24 changed files with 160 additions and 331 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"minimum-stability": "stable",
"require": {
"php": ">=5.6",
"php": ">=8.0",
"ext-dom": "*",
"guzzlehttp/guzzle": "^6.3"
},
Expand Down
70 changes: 22 additions & 48 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);

namespace HnutiBrontosaurus\BisApiClient;

Expand Down Expand Up @@ -28,27 +28,16 @@
final class Client
{

/** @var HttpClient */
private $httpClient;

/** @var string */
private $url;

/** @var string */
private $username;

/** @var string */
private $password;
private HttpClient $httpClient;
private string $url;
private string $username;
private string $password;


/**
* @param string $url
* @param string $username
* @param string $password
* @param HttpClient $httpClient
* @throws InvalidArgumentException
*/
public function __construct($url, $username, $password, HttpClient $httpClient)
public function __construct(string $url, string $username, string $password, HttpClient $httpClient)
{
if ($url === '') {
throw new InvalidArgumentException('You need to pass an URL with BIS API.');
Expand All @@ -70,14 +59,11 @@ public function __construct($url, $username, $password, HttpClient $httpClient)
// events

/**
* @param int $id
* @param EventParameters $params
* @return Event
* @throws NotFoundException
* @throws TransferErrorException
* @throws ResponseErrorException
*/
public function getEvent($id, EventParameters $params = null)
public function getEvent(int $id, EventParameters $params = null): Event
{
$params = ($params !== null ? $params : new EventParameters());
$params->setId($id);
Expand All @@ -92,14 +78,14 @@ public function getEvent($id, EventParameters $params = null)
return Event::fromResponseData(\reset($data));
}


/**
* @param EventParameters $params
* @return Event[]
* @throws NotFoundException
* @throws TransferErrorException
* @throws ResponseErrorException
*/
public function getEvents(EventParameters $params = null)
public function getEvents(EventParameters $params = null): array
{
$response = $this->processRequest($params !== null ? $params : new EventParameters());
$data = $response->getData();
Expand All @@ -111,11 +97,11 @@ public function getEvents(EventParameters $params = null)
return \array_map(Event::class . '::fromResponseData', $data);
}


/**
* @param EventAttendee $eventAttendee
* @throws ResponseErrorException
*/
public function addAttendeeToEvent(EventAttendee $eventAttendee)
public function addAttendeeToEvent(EventAttendee $eventAttendee): void
{
$eventAttendee->setCredentials($this->username, $this->password);
$response = $this->httpClient->send($this->createRequest($eventAttendee));
Expand All @@ -131,13 +117,12 @@ public function addAttendeeToEvent(EventAttendee $eventAttendee)
// organizational units

/**
* @param OrganizationalUnitParameters $params
* @return OrganizationalUnit[]
* @throws NotFoundException
* @throws TransferErrorException
* @throws ResponseErrorException
*/
public function getOrganizationalUnits(OrganizationalUnitParameters $params = null)
public function getOrganizationalUnits(OrganizationalUnitParameters $params = null): array
{
$response = $this->processRequest($params !== null ? $params : new OrganizationalUnitParameters());

Expand All @@ -159,10 +144,9 @@ public function getOrganizationalUnits(OrganizationalUnitParameters $params = nu
// adoption

/**
* @param Adoption $adoption
* @throws ResponseErrorException
*/
public function saveRequestForAdoption(Adoption $adoption)
public function saveRequestForAdoption(Adoption $adoption): void
{
$adoption->setCredentials($this->username, $this->password);

Expand All @@ -177,13 +161,11 @@ public function saveRequestForAdoption(Adoption $adoption)


/**
* @param Parameters $requestParameters
* @return Response
* @throws NotFoundException
* @throws TransferErrorException
* @throws ResponseErrorException
*/
private function processRequest(Parameters $requestParameters)
private function processRequest(Parameters $requestParameters): Response
{
$requestParameters->setCredentials($this->username, $this->password);

Expand All @@ -207,26 +189,24 @@ private function processRequest(Parameters $requestParameters)


/**
* @param ResponseInterface $response
* @throws InvalidContentTypeException
*/
private function checkForResponseContentType(ResponseInterface $response)
private function checkForResponseContentType(ResponseInterface $response): void
{
if (\strncmp($response->getHeaderLine('Content-Type'), 'text/xml', \strlen('text/xml')) !== 0) {
throw new InvalidContentTypeException('Unable to process response: the response Content-Type is invalid or missing.');
}
}


/**
* @param ResponseInterface $response
* @return \DOMDocument
* @throws InvalidXMLStructureException
*/
private function generateDOM(ResponseInterface $response)
private function generateDOM(ResponseInterface $response): \DOMDocument
{
try {
$domDocument = new \DOMDocument();
$domDocument->loadXML($response->getBody());
$domDocument->loadXML($response->getBody()->getContents());

} catch (\Exception $e) {
throw new InvalidXMLStructureException('Unable to process response: response body contains invalid XML.', 0, $e);
Expand All @@ -235,11 +215,11 @@ private function generateDOM(ResponseInterface $response)
return $domDocument;
}


/**
* @param \DOMDocument $domDocument
* @throws ResponseErrorException
*/
private function checkForResponseErrors(\DOMDocument $domDocument)
private function checkForResponseErrors(\DOMDocument $domDocument): void
{
$resultNode = $domDocument->getElementsByTagName(Response::TAG_RESULT)->item(0);
\assert($resultNode instanceof \DOMElement);
Expand All @@ -251,27 +231,21 @@ private function checkForResponseErrors(\DOMDocument $domDocument)

case 'user':
throw new InvalidUserInputException($resultNode);
break;

case 'forbidden':
throw new UnauthorizedAccessException();
break;

case 'params':
throw new InvalidParametersException();
break;

default:
throw new UnknownErrorException($resultNode->getAttribute(Response::TAG_RESULT_ATTRIBUTE_ERROR));
break;
}
}
}

/**
* @return Request
*/
private function createRequest(Parameters $parameters)

private function createRequest(Parameters $parameters): Request
{
return new Request(
'POST',
Expand Down
2 changes: 1 addition & 1 deletion src/Request/Adoption.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);

namespace HnutiBrontosaurus\BisApiClient\Request;

Expand Down
2 changes: 1 addition & 1 deletion src/Request/EventAttendee.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);

namespace HnutiBrontosaurus\BisApiClient\Request;

Expand Down
2 changes: 1 addition & 1 deletion src/Request/EventParameters.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);

namespace HnutiBrontosaurus\BisApiClient\Request;

Expand Down
2 changes: 1 addition & 1 deletion src/Request/OrganizationalUnitParameters.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);

namespace HnutiBrontosaurus\BisApiClient\Request;

Expand Down
2 changes: 1 addition & 1 deletion src/Request/Parameters.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);

/**
* See https://bis.brontosaurus.cz/myr.php for more information on values.
Expand Down
Loading

0 comments on commit 9f341c6

Please sign in to comment.