Skip to content

Commit

Permalink
Added default environment specification to ForteClient
Browse files Browse the repository at this point in the history
  • Loading branch information
oojacoboo committed Sep 13, 2020
1 parent 034a946 commit 419cbab
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 46 deletions.
15 changes: 8 additions & 7 deletions src/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ class Factory
/**
* Builds out the ForteClient
*
* @param array $settings Nested associative array, see `settings.php.dist`
* @param LoggerInterface $logger
* @param array $settings Nested associative array, see `settings.php.dist`
*/
public static function make(array $settings, LoggerInterface $logger): ForteClient
public static function make(
array $settings,
LoggerInterface $logger,
string $defaultEnvironmentKey
): ForteClient
{
if (empty($settings['environments'])) {
throw new LibraryGenericException('"environments" key missing from settings array');
Expand Down Expand Up @@ -67,14 +70,12 @@ public static function make(array $settings, LoggerInterface $logger): ForteClie
$envSettings['sandbox'],
$baseUri,
$logger,
$envSettings['debug']
$envSettings['debug'],
);
}

$overrideSubResourceEnvironments = $settings['override_sub_resource_environments'] ?? [];

$defaultEnvironment = array_shift($environments);

return new ForteClient($defaultEnvironment, $environments, $overrideSubResourceEnvironments);
return new ForteClient($environments, $defaultEnvironmentKey, $overrideSubResourceEnvironments);
}
}
64 changes: 39 additions & 25 deletions src/Client/ForteClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,34 @@
class ForteClient
{

protected const DEFAULT_ENVIRONMENT_NAME = '_default_';

/** @var AbstractSubResource[] */
protected $subResources;
protected array $subResources;

/** @var ForteEnvironment[] */
protected $environments;
protected array $environments;

protected ?ForteEnvironment $currentEnvironment = null;


/**
* Constructor
*
* @param ForteEnvironment $defaultEnvironment
* @param array $alternativeEnvironments
* @param array $overrideSubResourceEnvironments
*/
public function __construct(
ForteEnvironment $defaultEnvironment,
array $alternativeEnvironments = [],
array $environments,
string $defaultEnvironment,
array $overrideSubResourceEnvironments = []
) {
$this->addForteEnvironment(self::DEFAULT_ENVIRONMENT_NAME, $defaultEnvironment);

foreach ($alternativeEnvironments as $name => $environment) {
foreach ($environments as $name => $environment) {
$this->addForteEnvironment($name, $environment);
}

$this->setCurrentForteEnvironment($defaultEnvironment);
$this->initSubResources($overrideSubResourceEnvironments);
}


/**
* Adds an environment to support
*
* @param string $name
* @param ForteEnvironment $environment
*/
private function addForteEnvironment(string $name, ForteEnvironment $environment): void
{
Expand All @@ -63,9 +55,7 @@ private function addForteEnvironment(string $name, ForteEnvironment $environment


/**
* Gets a given environment by name
*
* @param string $name
* Gets a particular ForteEnvironment based on the key identifier
*/
protected function getForteEnvironment(string $name): ForteEnvironment
{
Expand All @@ -77,6 +67,34 @@ protected function getForteEnvironment(string $name): ForteEnvironment
}


/**
* Sets the current environment to be used
*/
protected function setCurrentForteEnvironment(string $name): self
{
if (!isset($this->environments[$name])) {
throw new LibraryGenericException('Cannot find forte environment with name `' . $name . '`');
}

$this->currentEnvironment = $this->environments[$name];

return $this;
}


/**
* Gets a given environment by name
*/
protected function getCurrentForteEnvironment(): ForteEnvironment
{
if ($this->currentEnvironment === null) {
throw new LibraryGenericException('Current Forte environemnt has not been set');
}

return $this->currentEnvironment;
}


/**
* Generates the risk session id
*
Expand All @@ -90,8 +108,6 @@ public function generateRiskSessionId(): string

/**
* Gets the URL for the risk session id
*
* @param string $riskSessionId
*/
public function getRiskSessionJavascriptUrl(string $riskSessionId): string
{
Expand All @@ -102,7 +118,7 @@ public function getRiskSessionJavascriptUrl(string $riskSessionId): string
/**
* Initializes all the necessary sub resources
*
* @param array $overrideSubResourceEnvironments
* @param string[] $overrideSubResourceEnvironments
*/
protected function initSubResources(array $overrideSubResourceEnvironments): void
{
Expand Down Expand Up @@ -133,7 +149,7 @@ protected function initSubResources(array $overrideSubResourceEnvironments): voi
if (!empty($overrideSubResourceEnvironments[$subResource])) {
$environment = $this->getForteEnvironment($overrideSubResourceEnvironments[$subResource]);
} else {
$environment = $this->getForteEnvironment(self::DEFAULT_ENVIRONMENT_NAME);
$environment = $this->getCurrentForteEnvironment();
}

$this->subResources[$subResource] = new $subResourceFqns($environment);
Expand All @@ -143,8 +159,6 @@ protected function initSubResources(array $overrideSubResourceEnvironments): voi

/**
* Determines the sub resource FQCN
*
* @param string $subResource
*/
protected function inferSubResourceClassName(string $subResource): string
{
Expand Down
6 changes: 2 additions & 4 deletions src/Client/SubResource/AbstractSubResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@
* Abstract class for SubResource(s)
*
* @author Sam Anthony <[email protected]>
* @author Jacob Thomason <[email protected]>
*/
abstract class AbstractSubResource
{

/** @var ForteEnvironment */
private $forteEnvironment;
private ForteEnvironment $forteEnvironment;


/**
* Constructor
*
* @param ForteEnvironment $forteEnvironment
*/
public function __construct(ForteEnvironment $forteEnvironment)
{
Expand Down
24 changes: 14 additions & 10 deletions test/Integration/AbstractIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

namespace Rentpost\ForteApi\Test\Integration;

use Rentpost\ForteApi\Attribute as Attribute;
use Rentpost\ForteApi\Attribute;
use Rentpost\ForteApi\Client\ForteClient;
use Rentpost\ForteApi\Exception\LibraryGenericException;
use Rentpost\ForteApi\File\Logger\Factory as FileLoggerFactory;
use Rentpost\ForteApi\HttpClient\HttpClient;
use Rentpost\ForteApi\Test\AbstractTestCase;
use Rentpost\ForteApi\Void\Logger as VoidLogger;
use Rentpost\ForteApi\File\Logger\Factory as FileLoggerFactory;
use Symfony\Component\Yaml\Yaml;
use Rentpost\ForteApi\Test\UserSettings;
use Rentpost\Sprocket\Environment\Detective;
use Rentpost\ForteApi\Void\Logger as VoidLogger;

/**
* Base Integration Test Class
*
* @author Sam Anthony <[email protected]>
* @author Jacob Thomason <[email protected]>
*/
abstract class AbstractIntegrationTest extends AbstractTestCase
{

Expand All @@ -24,16 +27,17 @@ protected function getAllSettingsFromFile(): array
}


/**
* Gets the ForteClient
*/
protected function getForteClient(): ForteClient
{
$logDir = UserSettings::getLogLocation();
$logger = (new FileLoggerFactory($logDir))->make('forte');

$allSettings = $this->getAllSettingsFromFile();

$forteClient = (new \Rentpost\ForteApi\Client\Factory())->make($allSettings, $logger);

return $forteClient;
return (new \Rentpost\ForteApi\Client\Factory())->make($allSettings, $logger, 'sandbox');
}


Expand All @@ -53,7 +57,7 @@ protected function getDirectHttpClient(string $baseUrl): HttpClient
new Attribute\Id\OrganizationId($settings['authenticating_organization_id']),
$baseUrl,
new VoidLogger(),
$settings['debug']
$settings['debug'],
);
}
}

0 comments on commit 419cbab

Please sign in to comment.