Skip to content

Commit

Permalink
cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kurowski committed Apr 13, 2023
1 parent 0d7fd51 commit c03326d
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use HnutiBrontosaurus\BisClient\AdministrationUnit\Category;
use HnutiBrontosaurus\BisClient\LimitParameter;
use HnutiBrontosaurus\BisClient\QueryParameters;
use function count;
use function implode;


final class AdministrationUnitParameters implements QueryParameters
Expand Down Expand Up @@ -42,8 +44,8 @@ public function toArray(): array
{
$array = [];

if (\count($this->categories) > 0) {
$array['category'] = \implode(',', $this->categories);
if (count($this->categories) > 0) {
$array['category'] = implode(',', $this->categories);
}

return $array;
Expand Down
13 changes: 8 additions & 5 deletions src/BisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use HnutiBrontosaurus\BisClient\Event\Response\Event;
use HnutiBrontosaurus\BisClient\Opportunity\Request\OpportunityParameters as OpportunityParameters;
use HnutiBrontosaurus\BisClient\Opportunity\Response\Opportunity;
use function array_map;
use function array_slice;
use function count;


final class BisClient
Expand All @@ -28,7 +31,7 @@ public function getEvents(?EventParameters $params = null): array
{
$params = $params !== null ? $params : new EventParameters();
$events = $this->retrieve(Endpoint::EVENTS(), $params, $params->getLimit());
return \array_map(static fn($result) => Event::fromResponseData($result), $events);
return array_map(static fn($result) => Event::fromResponseData($result), $events);
}


Expand Down Expand Up @@ -62,7 +65,7 @@ public function getAdministrationUnits(?AdministrationUnitParameters $params = n
{
$params = $params !== null ? $params : new AdministrationUnitParameters();
$administrationUnits = $this->retrieve(Endpoint::ADMINISTRATION_UNITS(), $params, $params->getLimit());
return \array_map(static fn($result) => AdministrationUnit::fromResponseData($result), $administrationUnits);
return array_map(static fn($result) => AdministrationUnit::fromResponseData($result), $administrationUnits);
}


Expand All @@ -76,7 +79,7 @@ public function getOpportunities(?OpportunityParameters $params = null): array
{
$params = $params !== null ? $params : new OpportunityParameters();
$opportunities = $this->retrieve(Endpoint::OPPORTUNITIES(), $params, $params->getLimit());
return \array_map(static fn($result) => Opportunity::fromResponseData($result), $opportunities);
return array_map(static fn($result) => Opportunity::fromResponseData($result), $opportunities);
}


Expand Down Expand Up @@ -123,15 +126,15 @@ private function retrieve(string $endpoint, ?QueryParameters $params, ?int $limi
endpoint: $data['next'],
params: null, // params are already included in next URL
limit: $limit,
currentCount: $currentCount + \count($results),
currentCount: $currentCount + count($results),
topLevel: false,
);
$results = [...$results, ...$moreResults];
}

// keep only given count of data
if ($topLevel && $limit !== null) {
$results = \array_slice($results, 0, $limit);
$results = array_slice($results, 0, $limit);
}

return $results;
Expand Down
3 changes: 2 additions & 1 deletion src/BisClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\HandlerStack;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use function rtrim;


/*
Expand All @@ -24,7 +25,7 @@ public function __construct(string $apiUrl)
$stack = HandlerStack::create();
$stack->push(self::lastRequestUrlIntoHeaderMiddleware());
$this->underlyingHttpClient = new Client([
'base_uri' => \rtrim($apiUrl, '/') . '/',
'base_uri' => rtrim($apiUrl, '/') . '/',
'handler' => $stack,
]);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace HnutiBrontosaurus\BisClient;

use function sprintf;


final class Endpoint
{

public static function EVENT(int $id): string
{
return \sprintf('web/events/%d/', $id);
return sprintf('web/events/%d/', $id);
}

public static function EVENTS(): string
Expand All @@ -28,7 +30,7 @@ public static function OPPORTUNITIES(): string

public static function OPPORTUNITY(int $id): string
{
return \sprintf('web/opportunities/%d/', $id);
return sprintf('web/opportunities/%d/', $id);
}

}
38 changes: 21 additions & 17 deletions src/Event/Request/EventParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
use HnutiBrontosaurus\BisClient\Event\Group;
use HnutiBrontosaurus\BisClient\Event\IntendedFor;
use HnutiBrontosaurus\BisClient\Event\Program;
use HnutiBrontosaurus\BisClient\LimitParameter;
use HnutiBrontosaurus\BisClient\QueryParameters;
use function count;
use function implode;


final class EventParameters implements \HnutiBrontosaurus\BisClient\QueryParameters
final class EventParameters implements QueryParameters
{
use \HnutiBrontosaurus\BisClient\LimitParameter;
use LimitParameter;

private Ordering $ordering;
private TimeZone $timeZone;
Expand Down Expand Up @@ -51,7 +55,7 @@ public function setAdministrationUnits(array $ids): self

// groups

/** @var \HnutiBrontosaurus\BisClient\Event\Group[] */
/** @var Group[] */
private array $groups = [];

public function setGroup(Group $group): self
Expand All @@ -61,7 +65,7 @@ public function setGroup(Group $group): self
}

/**
* @param \HnutiBrontosaurus\BisClient\Event\Group[] $groups
* @param Group[] $groups
*/
public function setGroups(array $groups): self
{
Expand All @@ -72,7 +76,7 @@ public function setGroups(array $groups): self

// category

/** @var \HnutiBrontosaurus\BisClient\Event\Category[] */
/** @var Category[] */
private array $categories = [];

public function setCategory(Category $category): self
Expand All @@ -93,7 +97,7 @@ public function setCategories(array $categories): self

// program

/** @var \HnutiBrontosaurus\BisClient\Event\Program[] */
/** @var Program[] */
private array $programs = [];

public function setProgram(Program $program): self
Expand Down Expand Up @@ -124,7 +128,7 @@ public function setIntendedFor(IntendedFor $intendedFor): self
}

/**
* @param \HnutiBrontosaurus\BisClient\Event\IntendedFor[] $intendedFor
* @param IntendedFor[] $intendedFor
*/
public function setMultipleIntendedFor(array $intendedFor): self
{
Expand Down Expand Up @@ -229,20 +233,20 @@ public function toArray(): array
'ordering' => $this->ordering->toScalar(),
];

if (\count($this->administrationUnits) > 0) {
$array['administration_unit'] = \implode(',', $this->administrationUnits);
if (count($this->administrationUnits) > 0) {
$array['administration_unit'] = implode(',', $this->administrationUnits);
}
if (\count($this->groups) > 0) {
$array['group'] = \implode(',', $this->groups);
if (count($this->groups) > 0) {
$array['group'] = implode(',', $this->groups);
}
if (\count($this->categories) > 0) {
$array['category'] = \implode(',', $this->categories);
if (count($this->categories) > 0) {
$array['category'] = implode(',', $this->categories);
}
if (\count($this->programs) > 0) {
$array['program'] = \implode(',', $this->programs);
if (count($this->programs) > 0) {
$array['program'] = implode(',', $this->programs);
}
if (\count($this->intendedFor) > 0) {
$array['intended_for'] = \implode(',', $this->intendedFor);
if (count($this->intendedFor) > 0) {
$array['intended_for'] = implode(',', $this->intendedFor);
}

if ($this->dateStartLessThanOrEqualTo !== null) {
Expand Down
12 changes: 7 additions & 5 deletions src/Event/Response/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use HnutiBrontosaurus\BisClient\Response\ContactPerson;
use HnutiBrontosaurus\BisClient\Response\Coordinates;
use HnutiBrontosaurus\BisClient\Response\Location;
use function array_map;
use function array_shift;


final class Event
Expand Down Expand Up @@ -117,11 +119,11 @@ private function __construct(
*/
public static function fromResponseData(array $data): self
{
$photos = \array_map(
$photos = array_map(
static fn($photo) => Photo::from($photo['image']),
$data['propagation']['images'],
);
$mainPhoto = \array_shift($photos);
$mainPhoto = array_shift($photos);

return new self(
$data['id'],
Expand Down Expand Up @@ -149,7 +151,7 @@ public static function fromResponseData(array $data): self
$data['propagation']['accommodation'] !== '' ? $data['propagation']['accommodation'] : null,
$data['propagation']['working_days'],
$data['propagation']['working_hours'],
\array_map(static fn($diet) => Diet::fromScalar($diet['slug']), $data['propagation']['diets']),
array_map(static fn($diet) => Diet::fromScalar($diet['slug']), $data['propagation']['diets']),
$data['propagation']['organizers'] !== '' ? $data['propagation']['organizers'] : null,
$data['propagation']['web_url'] !== '' ? $data['propagation']['web_url'] : null,
$data['propagation']['invitation_text_introduction'],
Expand All @@ -161,7 +163,7 @@ public static function fromResponseData(array $data): self
$data['propagation']['contact_email'] !== null ? $data['propagation']['contact_email'] : '', // todo temp unless BIS returns nulls for some old events
$data['propagation']['contact_phone'] !== null && $data['propagation']['contact_phone'] !== '' ? $data['propagation']['contact_phone'] : null,
),
\array_map(
array_map(
static fn($photo) => Image::from($photo['image']),
$data['propagation']['images'],
),
Expand Down Expand Up @@ -358,7 +360,7 @@ public function getAccommodation(): ?string
*/
public function getFood(): array
{
return \array_map(
return array_map(
static fn(Diet $diet): Food => Food::fromScalar($diet->toScalar()),
$this->getPropagation()->getDiets(),
);
Expand Down
10 changes: 7 additions & 3 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use GuzzleHttp\Exception\TooManyRedirectsException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Client\NetworkExceptionInterface;
use function assert;
use function http_build_query;
use function is_array;
use function json_decode;


final class HttpClient
Expand All @@ -32,7 +36,7 @@ public function send(
): array
{
$queryString = $queryParameters !== null
? '?' . \http_build_query($queryParameters->toArray())
? '?' . http_build_query($queryParameters->toArray())
: '';

// see Guzzle exceptions docs: https://docs.guzzlephp.org/en/stable/quickstart.html#exceptions
Expand Down Expand Up @@ -69,8 +73,8 @@ public function send(

$content = $response->getBody()->getContents();
$this->lastResponseEncoded = $content;
$decoded = \json_decode($content, flags: JSON_OBJECT_AS_ARRAY);
\assert(\is_array($decoded));
$decoded = json_decode($content, flags: JSON_OBJECT_AS_ARRAY);
assert(is_array($decoded));
$this->lastResponseDecoded = $decoded;
return $decoded;
}
Expand Down
4 changes: 3 additions & 1 deletion src/LimitParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace HnutiBrontosaurus\BisClient;

use function sprintf;


/**
* Limit parameter is not appended to request as API doesn't support it.
Expand All @@ -19,7 +21,7 @@ public function setLimit(?int $limit): self
throw new UsageException("Limit must be at least 1. If you want to remove limit, use 'removeLimit()' method");

} elseif ($limit < 1) {
throw new UsageException(\sprintf("Limit must be at least 1, '%d' given", $limit));
throw new UsageException(sprintf("Limit must be at least 1, '%d' given", $limit));
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/Opportunity/Request/OpportunityParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use HnutiBrontosaurus\BisClient\LimitParameter;
use HnutiBrontosaurus\BisClient\Opportunity\Category;
use HnutiBrontosaurus\BisClient\QueryParameters;
use function count;
use function implode;


final class OpportunityParameters implements \HnutiBrontosaurus\BisClient\QueryParameters
final class OpportunityParameters implements QueryParameters
{
use LimitParameter;

Expand Down Expand Up @@ -41,8 +44,8 @@ public function toArray(): array
{
$array = [];

if (\count($this->categories) > 0) {
$array['category'] = \implode(',', $this->categories);
if (count($this->categories) > 0) {
$array['category'] = implode(',', $this->categories);
}

return $array;
Expand Down
4 changes: 3 additions & 1 deletion src/QueryParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace HnutiBrontosaurus\BisClient;

use Stringable;


interface QueryParameters
{

public function getLimit(): ?int;

/** @return array<scalar|\Stringable> */
/** @return array<scalar|Stringable> */
public function toArray(): array;

}
5 changes: 4 additions & 1 deletion src/Response/Coordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace HnutiBrontosaurus\BisClient\Response;


use function sprintf;


final class Coordinates
{

Expand Down Expand Up @@ -32,7 +35,7 @@ public function getLongitude(): float

public function __toString(): string
{
return \sprintf('%s,%s', $this->latitude, $this->longitude);
return sprintf('%s,%s', $this->latitude, $this->longitude);
}

}
Loading

0 comments on commit c03326d

Please sign in to comment.