diff --git a/CHANGELOG.md b/CHANGELOG.md index fdbef21e..4f3916c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Pagination Page-Size Option ([PR163](https://github.com/5pm-HDH/churchtools-api/pull/163)) - DateTime-Getter ([PR167](https://github.com/5pm-HDH/churchtools-api/pull/167)) - Song-Tags ([PR168](https://github.com/5pm-HDH/churchtools-api/pull/168)) +- CTSession ([PR170](https://github.com/5pm-HDH/churchtools-api/pull/170)) ### Changed - Refactor Imports ([PR165](https://github.com/5pm-HDH/churchtools-api/pull/165)) diff --git a/docs/out/CTConfig.md b/docs/out/CTConfig.md index 5c2e80c6..c01ea9f9 100644 --- a/docs/out/CTConfig.md +++ b/docs/out/CTConfig.md @@ -98,7 +98,31 @@ $success = CTConfig::authWithUserIdAndLoginToken("29", ""); ``` -## 3. Cache Requests +## 3. CT-Session + +To manage various ChurchTools connections with different logins within a single application, you can utilize the CTSession feature to switch between different configurations. + +To create and switch to a new session, use the following code: + +```php +use CTApi\CTSession; + +CTSession::switchSession("person_a_session"); + +``` + +When switching to a new session, you need to reinitialize the ChurchTools API and authenticate the client. + +By default, if no session configuration is specified, the "default" session is used. To switch back to the default session, you can use the following code: + +```php +use CTApi\CTSession; + +CTSession::switchSession(); + +``` + +## 4. Cache Requests To increase performance enable the caching-mechanism with: @@ -137,7 +161,7 @@ $response = $client->get( ``` -## 4. Pagination +## 5. Pagination Set Page-Size of Pagination-Requests. @@ -148,7 +172,7 @@ CTConfig::setPaginationPageSize(400); ``` -## 5. CSRF-Token +## 6. CSRF-Token ```php use CTApi\Requests\CSRFTokenRequest; diff --git a/docs/src/ressources/CTConfig.md b/docs/src/ressources/CTConfig.md index 802ae547..d5eeadc1 100644 --- a/docs/src/ressources/CTConfig.md +++ b/docs/src/ressources/CTConfig.md @@ -91,7 +91,29 @@ use CTApi\CTConfig; $success = CTConfig::authWithUserIdAndLoginToken("29", ""); ``` -## 3. Cache Requests +## 3. CT-Session + +To manage various ChurchTools connections with different logins within a single application, you can utilize the CTSession feature to switch between different configurations. + +To create and switch to a new session, use the following code: + +```php +use CTApi\CTSession; + +CTSession::switchSession("person_a_session"); +``` + +When switching to a new session, you need to reinitialize the ChurchTools API and authenticate the client. + +By default, if no session configuration is specified, the "default" session is used. To switch back to the default session, you can use the following code: + +```php +use CTApi\CTSession; + +CTSession::switchSession(); +``` + +## 4. Cache Requests To increase performance enable the caching-mechanism with: @@ -128,7 +150,7 @@ $response = $client->get( ); ``` -## 4. Pagination +## 5. Pagination Set Page-Size of Pagination-Requests. @@ -138,6 +160,6 @@ use CTApi\CTConfig; CTConfig::setPaginationPageSize(400); ``` -## 5. CSRF-Token +## 6. CSRF-Token {{ \CTApi\Test\Unit\Docs\CSRFTokenRequestTest.testGetCSRFToken }} diff --git a/src/CTClient.php b/src/CTClient.php index 9f4994e9..d30dfc3d 100644 --- a/src/CTClient.php +++ b/src/CTClient.php @@ -14,9 +14,6 @@ use GuzzleHttp\HandlerStack; use Psr\Http\Message\ResponseInterface; -/** - * @psalm-suppress InvalidExtendClass - */ class CTClient { private Client $guzzleClient; @@ -109,7 +106,7 @@ private function handleException(Exception $exception): ResponseInterface public static function getClient(): CTClient { if (is_null(self::$client)) { - self::createClient(); + self::$client = self::createClient(); } if (isset(self::$client)) { return self::$client; @@ -122,12 +119,12 @@ public static function setClient(CTClient $client): void self::$client = $client; } - public static function createClient(?HandlerStack $handlerStack = null): void + public static function createClient(?HandlerStack $handlerStack = null): CTClient { if (is_null($handlerStack)) { - self::$client = new CTClient(); + return new CTClient(); } else { - self::$client = new CTClient(['handler' => $handlerStack]); + return new CTClient(['handler' => $handlerStack]); } } } \ No newline at end of file diff --git a/src/CTConfig.php b/src/CTConfig.php index f0b9852a..eba33e71 100644 --- a/src/CTConfig.php +++ b/src/CTConfig.php @@ -51,7 +51,12 @@ private function __construct() public static function clearConfig(): void { - self::$config = new CTConfig(); + self::$config = CTConfig::createConfig(); + } + + public static function createConfig(): CTConfig + { + return new CTConfig(); } public static function clearCookies(): void @@ -65,17 +70,41 @@ public static function clearCookies(): void public static function getConfig(): CTConfig { if (is_null(self::$config)) { - self::$config = new CTConfig(); + self::$config = CTConfig::createConfig(); } return self::$config; } + public static function setConfig(CTConfig $config) + { + self::$config = $config; + } + + private static function setRequestOption(string $path, $value): void + { + CTUtil::arrayPathSet(self::getConfig()->requestOptions, $path, $value); + } + public static function getRequestConfig(): array { self::validateConfig(); return self::getConfig()->requestOptions; } + public static function validateConfig(): void + { + $apiUrl = self::getRequestOption('base_uri'); + if ($apiUrl == null) { + throw new CTConfigException("CTConfig invalid: ApiUrl cannot be null. Set it with: CTConfig::setApiUrl('https://example.com')"); + } + } + + private static function getRequestOption(string $path) + { + $array = self::getConfig()->requestOptions; + return CTUtil::arrayPathGet($array, $path); + } + public static function setApiUrl(string $apiUrl): void { self::setRequestOption("base_uri", $apiUrl); @@ -157,14 +186,6 @@ public static function getPaginationPageSize(): ?int return $size; } - public static function validateConfig(): void - { - $apiUrl = self::getRequestOption('base_uri'); - if ($apiUrl == null) { - throw new CTConfigException("CTConfig invalid: ApiUrl cannot be null. Set it with: CTConfig::setApiUrl('https://example.com')"); - } - } - public static function validateAuthentication(): bool { try { @@ -179,17 +200,6 @@ public static function validateAuthentication(): bool } } - private static function setRequestOption(string $path, $value): void - { - CTUtil::arrayPathSet(self::getConfig()->requestOptions, $path, $value); - } - - private static function getRequestOption(string $path) - { - $array = self::getConfig()->requestOptions; - return CTUtil::arrayPathGet($array, $path); - } - public static function enableDebugging(): void { CTLog::setConsoleLogLevelDebug(); @@ -224,13 +234,15 @@ public static function enableCache(?int $timeToLive = null): void $stack->push(CTCacheMiddleware::create()); CTLog::getLog()->info("CTConfig: Create cache-middleware and recreate CTClient"); - CTClient::createClient($stack); + $client = CTClient::createClient($stack); + CTClient::setClient($client); } public static function disableCache(): void { CTLog::getLog()->info("CTConfig: Recreate CTClient without cache-middleware"); - CTClient::createClient(); + $client = CTClient::createClient(); + CTClient::setClient($client); } public static function clearCache(): void diff --git a/src/CTSession.php b/src/CTSession.php new file mode 100644 index 00000000..f23b6e2d --- /dev/null +++ b/src/CTSession.php @@ -0,0 +1,67 @@ +configPersonA(); + $personA = PersonRequest::whoami(); + + // Custom Session + CTSession::switchSession("custom_person_session"); + $this->configPersonB(); + $personB = PersonRequest::whoami(); + + // Default Session + CTSession::switchSession(); + $personA_2 = PersonRequest::whoami(); + + // Custom Session + CTSession::switchSession("custom_person_session"); + $personB_2 = PersonRequest::whoami(); + + $this->assertEquals($personA, $personA_2); + $this->assertEquals($personB, $personB_2); + $this->assertNotEquals($personA, $personB); + $this->assertNotEquals($personA_2, $personB_2); + $this->assertNotEquals($personA, $personB_2); + $this->assertNotEquals($personA_2, $personB); + } + + private function configPersonA() + { + CTConfig::setApiUrl(IntegrationTestData::get()->getApiUrl()); + IntegrationTestData::get()->authenticateUser(); + } + + private function configPersonB() + { + CTConfig::setApiUrl(IntegrationTestData::get()->getApiUrl()); + IntegrationTestData::get()->authenticateUser("jona"); + } + +} \ No newline at end of file diff --git a/tests/Unit/CTSessionUnitTest.php b/tests/Unit/CTSessionUnitTest.php new file mode 100644 index 00000000..359d1155 --- /dev/null +++ b/tests/Unit/CTSessionUnitTest.php @@ -0,0 +1,59 @@ +assertNotEquals($configDefault, $configPerson); + $this->assertNotEquals($clientDefault, $clientPerson); + $this->assertEquals($configDefault, $configDefaultB); + $this->assertEquals($clientDefault, $clientDefaultB); + $this->assertEquals($configDefault, $configDefaultC); + $this->assertEquals($clientDefault, $clientDefaultC); + + $sessionIds = CTSession::getSessionIds(); + + $this->assertTrue(in_array("default", $sessionIds)); + $this->assertTrue(in_array("person_b", $sessionIds)); + } + + public function testSessionDefault() + { + $sessionIds = CTSession::getSessionIds(); + + $this->assertEquals(1, sizeof($sessionIds)); + $this->assertTrue(in_array("default", $sessionIds)); + } +} \ No newline at end of file diff --git a/tests/Unit/TestCaseHttpMocked.php b/tests/Unit/TestCaseHttpMocked.php index d9e4cf60..4d187b54 100644 --- a/tests/Unit/TestCaseHttpMocked.php +++ b/tests/Unit/TestCaseHttpMocked.php @@ -4,9 +4,8 @@ use CTApi\CTClient; use CTApi\CTConfig; -use CTApi\Utils\CTUtil; -use PHPUnit\Framework\TestCase; use CTApi\Test\Unit\HttpMock\CTClientMock; +use PHPUnit\Framework\TestCase; class TestCaseHttpMocked extends TestCase { @@ -49,6 +48,7 @@ protected function tearDown(): void parent::tearDown(); // TODO: Change the autogenerated stub // overwrite custom CTClientMock with default HTTP-Configuration - CTClient::createClient(); + $client = CTClient::createClient(); + CTClient::setClient($client); } } \ No newline at end of file