Skip to content

Commit

Permalink
Format with pint
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmypuckett committed Mar 15, 2023
1 parent 9bc3178 commit ed0e186
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 97 deletions.
42 changes: 7 additions & 35 deletions src/Controllers/SsoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,26 @@
* Controller to process the Discourse SSO request. There is a good bit of logic in here that almost feels like too
* much for a controller, but given that this is the only thing that this controller is doing, I am not going to break
* it out into a service class.
*
* @package Spinen\Discourse\Controllers
*/
class SsoController extends Controller
{
/**
* Package configuration
*
* @var Collection
*/
protected $config;
protected Collection $config;

/**
* SSOHelper Instance
*
* @var SSOHelper
*/
protected $sso;
protected SSOHelper $sso;

/**
* Authenticated user
*
* @var User
*/
protected $user;
protected User $user;

/**
* SsoController constructor.
*
* @param Config $config
* @param SSOHelper $sso
*/
public function __construct(Config $config, SSOHelper $sso)
{
Expand All @@ -57,10 +46,8 @@ public function __construct(Config $config, SSOHelper $sso)

/**
* Build out the extra parameters to send to Discourse
*
* @return array
*/
protected function buildExtraParameters()
protected function buildExtraParameters(): array
{
return $this->config->get('user')
->except(['access', 'email', 'external_id'])
Expand All @@ -75,12 +62,8 @@ protected function buildExtraParameters()
*
* The Discourse SSO API does not accept 0 or 1 for false or true. You must send
* "false" or "true", so convert any boolean property to the string version.
*
* @param $property
*
* @return string
*/
public function castBooleansToString($property)
public function castBooleansToString(string|bool $property): string
{
if (! is_bool($property)) {
return $property;
Expand All @@ -93,10 +76,8 @@ public function castBooleansToString($property)
* Cache the configs on the object as a collection
*
* The 'user' property will be an array, so go ahead and convert it to a collection
*
* @param Config $config
*/
protected function loadConfigs(Config $config)
protected function loadConfigs(Config $config): void
{
$this->config = collect($config->get('services.discourse'));
$this->config->put('user', collect($this->config->get('user')));
Expand All @@ -105,9 +86,6 @@ protected function loadConfigs(Config $config)
/**
* Process the SSO login request from Discourse
*
* @param Request $request
*
* @return mixed
* @throws 403
*/
public function login(Request $request)
Expand Down Expand Up @@ -138,11 +116,8 @@ public function login(Request $request)

/**
* Check to see if property is null
*
* @param string $property
* @return bool
*/
public function nullProperty($property)
public function nullProperty(?string $property): bool
{
return is_null($property);
}
Expand All @@ -151,9 +126,6 @@ public function nullProperty($property)
* Get the property from the user
*
* If a string is passed in, then get it from the user object, otherwise, return what was given
*
* @param mixed $property
* @return mixed
*/
public function parseUserValue($property)
{
Expand Down
28 changes: 10 additions & 18 deletions src/Listeners/LogoutDiscourseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,33 @@
use GuzzleHttp\Exception\BadResponseException;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpKernel\Log\Logger;

/**
* Class LogoutDiscourseUser
*
* Send a Logout request to Discourse for the corresponding Laravel User.
*
* @package Spinen\Discourse\Listeners
*/
class LogoutDiscourseUser implements ShouldQueue
{
/**
* @var Client
* The client instance
*/
public $client;
public Client $client;

/**
* @var Repository
* @The repository instance
*/
public $config_repository;
public Repository $config_repository;

/**
* @var Logger
* The logger instance
*/
public $logger;
public Logger $logger;

/**
* Create the event listener.
*
* @param Client $client
* @param Repository $config_repository
* @param Logger $logger
*
* @return void
*/
public function __construct(Client $client, Repository $config_repository, Logger $logger)
Expand All @@ -52,20 +45,18 @@ public function __construct(Client $client, Repository $config_repository, Logge
/**
* Handle the event.
*
* @param mixed $event
*
* @return void
*/
public function handle($event)
{
if (!$event->user) {
if (! $event->user) {
return;
}

$configs = [
'base_uri' => $this->config_repository->get('services.discourse.url'),
'headers' => [
'Api-Key' => $this->config_repository->get('services.discourse.api.key'),
'headers' => [
'Api-Key' => $this->config_repository->get('services.discourse.api.key'),
'Api-Username' => $this->config_repository->get('services.discourse.api.user'),
],
];
Expand All @@ -82,6 +73,7 @@ public function handle($event)
"When getting user {$event->user->id} Discourse returned status code {$response->getStatusCode()}",
['reason' => $response->getReasonPhrase()]
);

return;
}

Expand Down
4 changes: 1 addition & 3 deletions src/SsoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

/**
* Class SsoServiceProvider
*
* @package Spinen\Discourse
*/
class SsoServiceProvider extends ServiceProvider
{
Expand All @@ -25,7 +23,7 @@ function (Router $router) {
$router->get(
$this->app['config']->get('services.discourse.route'),
[
'as' => 'sso.login',
'as' => 'sso.login',
'domain' => $this->app['config']->get('services.discourse.domain', null),
'uses' => 'Spinen\Discourse\Controllers\SsoController@login',
]
Expand Down
27 changes: 12 additions & 15 deletions tests/Controllers/SsoControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

/**
* Class SsoControllerTest
*
* @package Spinen\Discourse\Controllers
*/
class SsoControllerTest extends TestCase
{
Expand Down Expand Up @@ -89,7 +87,7 @@ public function it_aborts_if_the_payload_is_invalid()
->with('services.discourse')
->andReturn([
'secret' => 'secret',
'user' => [
'user' => [
'access' => null,
],
]);
Expand Down Expand Up @@ -151,7 +149,6 @@ public function it_is_backwards_compatible_with_config_that_does_not_have_access
$controller = new SsoController($this->config_mock, $this->sso_helper_mock);

$controller->login($this->request_mock);

}

/**
Expand All @@ -167,7 +164,7 @@ public function it_aborts_if_the_user_does_not_have_access()
->with('services.discourse')
->andReturn([
'secret' => 'secret',
'user' => [
'user' => [
'access' => 'forum_access',
],
]);
Expand Down Expand Up @@ -195,14 +192,14 @@ public function it_builds_the_correct_payload()
->andReturn([
'secret' => 'secret',
// Expect the '/' on the end to not double up
'url' => 'http://discourse/',
'user' => [
'external_id' => 'id',
'email' => 'email',
'url' => 'http://discourse/',
'user' => [
'external_id' => 'id',
'email' => 'email',
// Expect this null_value to not be passed on
'null_value' => null,
'false_value' => false,
'true_value' => true,
'null_value' => null,
'false_value' => false,
'true_value' => true,
'string_value' => 'string',
],
]);
Expand Down Expand Up @@ -243,8 +240,8 @@ public function it_builds_the_correct_payload()
1,
'[email protected]',
[
'false_value' => 'false',
'true_value' => 'true',
'false_value' => 'false',
'true_value' => 'true',
'string_value' => 'string_property',
],
])
Expand All @@ -258,7 +255,7 @@ public function it_builds_the_correct_payload()

function abort($code)
{
throw new Exception("Some error message", $code);
throw new Exception('Some error message', $code);
}

function redirect($path)
Expand Down
16 changes: 6 additions & 10 deletions tests/Listeners/LogoutDiscourseUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Http\Request;
use Mockery;
use Psr\Http\Message\ResponseInterface;
use Ramsey\Collection\AbstractArray;
use Spinen\Discourse\TestCase;
use Symfony\Component\HttpKernel\Log\Logger;

/**
* Class LogoutDiscourseUserTest
*
* @package Spinen\Discourse\Listeners
*/
class LogoutDiscourseUserTest extends TestCase
{
Expand Down Expand Up @@ -106,8 +102,8 @@ public function it_logs_out_the_discourse_user_when_triggered()

$configs = [
'base_uri' => 'http://discourse.example.com',
'headers' => [
'Api-Key' => 'testkey',
'headers' => [
'Api-Key' => 'testkey',
'Api-Username' => 'testuser',
],
];
Expand Down Expand Up @@ -173,8 +169,8 @@ public function on_getting_user_if_discourse_response_code_is_not_200_log_an_err

$configs = [
'base_uri' => 'http://discourse.example.com',
'headers' => [
'Api-Key' => 'testkey',
'headers' => [
'Api-Key' => 'testkey',
'Api-Username' => 'testuser',
],
];
Expand Down Expand Up @@ -226,8 +222,8 @@ public function on_user_logout_if_discourse_response_code_is_not_200_log_a_notic

$configs = [
'base_uri' => 'http://discourse.example.com',
'headers' => [
'Api-Key' => 'testkey',
'headers' => [
'Api-Key' => 'testkey',
'Api-Username' => 'testuser',
],
];
Expand Down
7 changes: 2 additions & 5 deletions tests/SsoServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

/**
* Class SsoServiceProviderTest
*
* @package Spinen\Discourse
*/
class SsoServiceProviderTest extends TestCase
{
Expand Down Expand Up @@ -104,9 +102,9 @@ public function it_boots_the_service()
[
'route',
[
'as' => 'sso.login',
'as' => 'sso.login',
'domain' => 'domain',
'uses' => 'Spinen\Discourse\Controllers\SsoController@login',
'uses' => 'Spinen\Discourse\Controllers\SsoController@login',
],
]
)
Expand All @@ -115,7 +113,6 @@ public function it_boots_the_service()

$route_closure = Mockery::on(
function ($closure) {

$closure($this->router_mock);

return true;
Expand Down
Loading

0 comments on commit ed0e186

Please sign in to comment.