Skip to content

Commit

Permalink
[FEATURE] Introduce connection information, resolves #33
Browse files Browse the repository at this point in the history
  • Loading branch information
fsuter committed Oct 31, 2024
1 parent 70d4618 commit 0c0118c
Show file tree
Hide file tree
Showing 20 changed files with 714 additions and 78 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2024-10-31 Francois Suter (Idéative) <[email protected]>

* Introduce connection information, resolves #33

2024-07-20 Francois Suter (Idéative) <[email protected]>

* Introduce call context , resolves #30
Expand Down
9 changes: 5 additions & 4 deletions Classes/Controller/TestingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function defaultAction(): ResponseInterface
'format' => $arguments['format'],
'testResult' => $this->performTest(
$arguments['service'],
$arguments['parameters'],
$parameters,
(int)$arguments['format']
),
]
Expand Down Expand Up @@ -179,11 +179,12 @@ protected function performTest(string $type, string $parameters = '', int $forma
try {
$service->getCallContext()->add('svconnector', ['function' => 'testing module']);
$parsedParameters = json_decode($parameters, true, 512, JSON_THROW_ON_ERROR);
$service->setParameters($parsedParameters);
// Call the right "fetcher" depending on chosen format
$result = match ($format) {
1 => $service->fetchArray($parsedParameters),
2 => $service->fetchXML($parsedParameters),
default => $service->fetchRaw($parsedParameters),
1 => $service->fetchArray(),
2 => $service->fetchXML(),
default => $service->fetchRaw(),
};
// If the result is empty, issue an information message
if (empty($result)) {
Expand Down
62 changes: 62 additions & 0 deletions Classes/Domain/Model/Dto/AbstractDataObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace Cobweb\Svconnector\Domain\Model\Dto;

use Cobweb\Svconnector\Exception\NoSuchDataException;

/**
* DTO class for managing a simple data storage.
*/
abstract class AbstractDataObject
{
protected array $data = [];

public function get(): array
{
return $this->data;
}

/**
* @throws NoSuchDataException
*/
public function getForKey(string $key): ?array
{
if (array_key_exists($key, $this->data)) {
return $this->data[$key];
}
throw new NoSuchDataException(
sprintf('No value found in context for key "%s".', $key),
1721494427
);
}

public function set(array $data): void
{
$this->data = $data;
}

public function add(string $key, mixed $value): void
{
$this->data[$key] = $value;
}

public function reset(): void
{
$this->data = [];
}
}
41 changes: 1 addition & 40 deletions Classes/Domain/Model/Dto/CallContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,10 @@

namespace Cobweb\Svconnector\Domain\Model\Dto;

use Cobweb\Svconnector\Exception\NoSuchContextException;

/**
* DTO class for managing a call context.
*
* A call context can be made up of a collection of information, each piece of information being stored
* with a specific associative key in the context array.
*/
class CallContext
{
protected array $context = [];

public function get(): array
{
return $this->context;
}

/**
* @throws NoSuchContextException
*/
public function getForKey(string $key): ?array
{
if (array_key_exists($key, $this->context)) {
return $this->context[$key];
}
throw new NoSuchContextException(
sprintf('No value found in context for key "%s".', $key),
1721494427
);
}

public function set(array $context): void
{
$this->context = $context;
}

public function add(string $key, array $value): void
{
$this->context[$key] = $value;
}

public function reset(): void
{
$this->context = [];
}
}
class CallContext extends AbstractDataObject {}
27 changes: 27 additions & 0 deletions Classes/Domain/Model/Dto/ConnectionInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace Cobweb\Svconnector\Domain\Model\Dto;

/**
* DTO class for managing connection information.
*
* Connection to third-party sources may require some form of authentication, for example
* returning a token. This kind of information can be stored in the connection information,
* which can later be used in the connection parameters.
*/
class ConnectionInformation extends AbstractDataObject {}
35 changes: 35 additions & 0 deletions Classes/Event/InitializeConnectorEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace Cobweb\Svconnector\Event;

use Cobweb\Svconnector\Service\ConnectorBase;

final class InitializeConnectorEvent
{
protected ConnectorBase $connector;

public function __construct(ConnectorBase $connector)
{
$this->connector = $connector;
}

public function getConnector(): ConnectorBase
{
return $this->connector;
}
}
39 changes: 39 additions & 0 deletions Classes/Event/ProcessParametersEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace Cobweb\Svconnector\Event;

final class ProcessParametersEvent
{
protected array $parameters;

public function __construct(array $parameters)
{
$this->parameters = $parameters;
}

public function getParameters(): array
{
return $this->parameters;
}

public function setParameters(array $parameters): void
{
$this->parameters = $parameters;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
*/

/**
* Exception to throw when trying to access an element which does not exist in the current call context
* Exception to throw when trying to access an element which does not exist in a data object
*/
class NoSuchContextException extends ConnectorException {}
class NoSuchDataException extends ConnectorException {}
13 changes: 9 additions & 4 deletions Classes/Registry/ConnectorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

declare(strict_types=1);

namespace Cobweb\Svconnector\Registry;

/*
* This file is part of the TYPO3 CMS project.
*
Expand All @@ -17,6 +15,8 @@
* The TYPO3 project - inspiring people to share!
*/

namespace Cobweb\Svconnector\Registry;

use Cobweb\Svconnector\Exception\UnknownServiceException;
use Cobweb\Svconnector\Service\ConnectorBase;

Expand Down Expand Up @@ -52,6 +52,7 @@ public function __construct(iterable $connectors)
1671361286
);
}
$connector->initialize();
$this->connectors[$type] = $connector;
}
}
Expand All @@ -70,13 +71,17 @@ public function getAllServices(): iterable
* Returns a connector service object for the requested type
*
* @param string $type Type of connector service
* @param array $parameters Parameters for the connector
* @return ConnectorBase
* @throws UnknownServiceException
*/
public function getServiceForType(string $type): ConnectorBase
public function getServiceForType(string $type, array $parameters = []): ConnectorBase
{
if (isset($this->connectors[$type])) {
return $this->connectors[$type];
/** @var ConnectorBase $connector */
$connector = $this->connectors[$type];
$connector->setParameters($parameters);
return $connector;
}
throw new UnknownServiceException(
sprintf(
Expand Down
Loading

0 comments on commit 0c0118c

Please sign in to comment.