Skip to content

Commit

Permalink
fix: review corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Oct 27, 2023
1 parent 5a45508 commit 5cfaf38
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 13 deletions.
77 changes: 64 additions & 13 deletions src/Loader/SiteKitLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,49 @@

/**
* ResourceLoader that loads resources created with SiteKit aggregators.
* @phpstan-type InitData array{id: int, name: string, objectType: string}
* @phpstan-type ResourceData array{init: InitData}
*/
class SiteKitLoader implements ResourceLoader
{
public function __construct(
private readonly string $base
private readonly string $basePath
) {
}

/**
* @throws InvalidResource
* @throws ResourceNotFound
*/
public function load(string $location): Resource
{
$data = $this->loadRaw($location);

$this->validateData($location, $data);

$init = (array)$data['init'];
$init = $data['init'];

return new Resource(
$location,
(string)$init['id'], // @phpstan-ignore-line
$init['name'], // @phpstan-ignore-line
$init['objectType'], // @phpstan-ignore-line
(string)$init['id'],
$init['name'],
$init['objectType'],
$data
);
}

/**
* @return array<string, mixed>
* @return array<string, mixed> $data
*/
private function loadRaw(string $location): array
{
$file = $this->base . '/' . $location;
$file = $this->basePath . '/' . $location;

/**
* $context and $lifecycle must be defined here, because for the SiteKit
* resource PHP files these variables must be provided for the require
* call.
*/
$context = new ContextStub();
$lifecycle = new LifecylceStub();

Expand All @@ -67,19 +78,59 @@ private function loadRaw(string $location): array

/**
* @param array<string, mixed> $data
* @return ($data is ResourceData ? void : never)
*/
private function validateData(string $location, array $data): void
{

/*
* Cannot be passed because this case cannot occur here. This would
* already lead to an error in ResourceStub. But is still included,
* that so no phpstan errors arise.
*/
// @codeCoverageIgnoreStart
if (!isset($data['init']) || !is_array($data['init'])) {
throw new InvalidResource($location, 'init field missing');
}
// @codeCoverageIgnoreEnd

$init = $data['init'];

if (!isset($init['id'])) { // @phpstan-ignore-line
throw new InvalidResource($location, 'id field missing');
if (!isset($init['id'])) {
throw new InvalidResource(
$location,
'id field missing'
);
}
if (!is_int($init['id'])) {
throw new InvalidResource(
$location,
'id field not an int'
);
}
if (!isset($init['name'])) {
throw new InvalidResource(
$location,
'name field missing'
);
}
if (!is_string($init['name'])) {
throw new InvalidResource(
$location,
'name field not a string'
);
}
if (!isset($init['name'])) { // @phpstan-ignore-line
throw new InvalidResource($location, 'name field missing');
if (!isset($init['objectType'])) {
throw new InvalidResource(
$location,
'objectType field missing'
);
}
if (!isset($init['objectType'])) { // @phpstan-ignore-line
throw new InvalidResource($location, 'objectType field missing');
if (!is_string($init['objectType'])) {
throw new InvalidResource(
$location,
'objectType field not a string'
);
}
}
}
17 changes: 17 additions & 0 deletions src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public function getObjectType(): string
return $this->objectType;
}

/**
* @param string $name Name of the value that can be returned from the
* data object. The name can contain dots (`.`) in case of a nested
* structure and define the corresponding levels.
*
* Example:
* [
* 'foo' : [
* 'bar' : 'value'
* ]
* ]
*
* For the above structure, `value` can be retrieved using the following
* `$name`: `boo.bar`
*
* @return mixed
*/
public function getData(string $name): mixed
{
return $this->findData($this->data, $name);
Expand Down
18 changes: 18 additions & 0 deletions test/Loader/SiteKitLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public function testLoadWithInitNotAnArray(): void
$this->loader->load('initNotAnArrayResource.php');
}

public function testLoadWithNonIntId(): void
{
$this->expectException(InvalidResource::class);
$this->loader->load('nonIntIdResource.php');
}

public function testLoadWithMissingId(): void
{
$this->expectException(InvalidResource::class);
Expand All @@ -72,9 +78,21 @@ public function testLoadWithMissingName(): void
$this->loader->load('missingNameResource.php');
}

public function testLoadWithNonStringName(): void
{
$this->expectException(InvalidResource::class);
$this->loader->load('nonStringNameResource.php');
}

public function testLoadWithMissingObjectType(): void
{
$this->expectException(InvalidResource::class);
$this->loader->load('missingObjectTypeResource.php');
}

public function testLoadWithNonStringObjectType(): void
{
$this->expectException(InvalidResource::class);
$this->loader->load('nonStringObjectTypeResource.php');
}
}
26 changes: 26 additions & 0 deletions test/resources/Loader/SiteKitLoader/nonIntIdResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/* Bootstrap */
if (!isset($context)) {
$context = include(
__DIR__ .
'/./WEB-IES/sitekit-module/php/bootstrapper.php'
);
}
if (!isset($lifecycle)) {
$lifecycle = $context->getAttribute('lifecycle');
}

$resource = $context->redirectToTranslation($lifecycle, '/index.php');
if ($resource !== null) {
return $resource;
}

/* Lifecylce-Process */
$resource = $lifecycle->init([
"id" => "123",
"objectType" => "home",
"name" => "Startseite",
]);

return $lifecycle->service($resource);
26 changes: 26 additions & 0 deletions test/resources/Loader/SiteKitLoader/nonStringNameResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/* Bootstrap */
if (!isset($context)) {
$context = include(
__DIR__ .
'/./WEB-IES/sitekit-module/php/bootstrapper.php'
);
}
if (!isset($lifecycle)) {
$lifecycle = $context->getAttribute('lifecycle');
}

$resource = $context->redirectToTranslation($lifecycle, '/index.php');
if ($resource !== null) {
return $resource;
}

/* Lifecylce-Process */
$resource = $lifecycle->init([
"name" => 123,
"id" => 1118,
"objectType" => "home",
]);

return $lifecycle->service($resource);
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/* Bootstrap */
if (!isset($context)) {
$context = include(
__DIR__ .
'/./WEB-IES/sitekit-module/php/bootstrapper.php'
);
}
if (!isset($lifecycle)) {
$lifecycle = $context->getAttribute('lifecycle');
}

$resource = $context->redirectToTranslation($lifecycle, '/index.php');
if ($resource !== null) {
return $resource;
}

/* Lifecylce-Process */
$resource = $lifecycle->init([
"id" => 1118,
"name" => "Startseite",
"objectType" => 123
]);

return $lifecycle->service($resource);

0 comments on commit 5cfaf38

Please sign in to comment.