Skip to content

Commit d736cca

Browse files
committed
Move Arr::find to Arr helper
1 parent 19ccd61 commit d736cca

File tree

7 files changed

+44
-33
lines changed

7 files changed

+44
-33
lines changed

src/Factories/Grant/AuthCodeGrantFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use SimpleSAML\Module\oidc\Factories\Entities\AccessTokenEntityFactory;
2020
use SimpleSAML\Module\oidc\Factories\Entities\AuthCodeEntityFactory;
21+
use SimpleSAML\Module\oidc\Helpers;
2122
use SimpleSAML\Module\oidc\ModuleConfig;
2223
use SimpleSAML\Module\oidc\Repositories\AccessTokenRepository;
2324
use SimpleSAML\Module\oidc\Repositories\AuthCodeRepository;
@@ -39,6 +40,7 @@ public function __construct(
3940
private readonly AccessTokenEntityFactory $accessTokenEntityFactory,
4041
private readonly AuthCodeEntityFactory $authCodeEntityFactory,
4142
private readonly RefreshTokenIssuer $refreshTokenIssuer,
43+
private readonly Helpers $helpers,
4244
) {
4345
}
4446

@@ -57,6 +59,7 @@ public function build(): AuthCodeGrant
5759
$this->accessTokenEntityFactory,
5860
$this->authCodeEntityFactory,
5961
$this->refreshTokenIssuer,
62+
$this->helpers,
6063
);
6164
$authCodeGrant->setRefreshTokenTTL($this->moduleConfig->getRefreshTokenDuration());
6265

src/Helpers/Arr.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66

77
class Arr
88
{
9+
/**
10+
* Find item in array using the given callable.
11+
*
12+
* @return mixed|null
13+
*/
14+
public function findByCallback(array $arr, callable $fn): mixed
15+
{
16+
/** @psalm-suppress MixedAssignment */
17+
foreach ($arr as $x) {
18+
if (call_user_func($fn, $x) === true) {
19+
return $x;
20+
}
21+
}
22+
23+
return null;
24+
}
25+
926
/**
1027
* @param array $values
1128
* @return string[]

src/Server/Grants/AuthCodeGrant.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use SimpleSAML\Module\oidc\Entities\UserEntity;
2828
use SimpleSAML\Module\oidc\Factories\Entities\AccessTokenEntityFactory;
2929
use SimpleSAML\Module\oidc\Factories\Entities\AuthCodeEntityFactory;
30+
use SimpleSAML\Module\oidc\Helpers;
3031
use SimpleSAML\Module\oidc\Repositories\Interfaces\AccessTokenRepositoryInterface;
3132
use SimpleSAML\Module\oidc\Repositories\Interfaces\AuthCodeRepositoryInterface;
3233
use SimpleSAML\Module\oidc\Repositories\Interfaces\RefreshTokenRepositoryInterface;
@@ -58,7 +59,6 @@
5859
use SimpleSAML\Module\oidc\Server\ResponseTypes\Interfaces\NonceResponseTypeInterface;
5960
use SimpleSAML\Module\oidc\Server\ResponseTypes\Interfaces\SessionIdResponseTypeInterface;
6061
use SimpleSAML\Module\oidc\Server\TokenIssuers\RefreshTokenIssuer;
61-
use SimpleSAML\Module\oidc\Utils\Arr;
6262
use SimpleSAML\Module\oidc\Utils\RequestParamsResolver;
6363
use SimpleSAML\Module\oidc\Utils\ScopeHelper;
6464
use SimpleSAML\OpenID\Codebooks\HttpMethodsEnum;
@@ -165,6 +165,7 @@ public function __construct(
165165
AccessTokenEntityFactory $accessTokenEntityFactory,
166166
protected AuthCodeEntityFactory $authCodeEntityFactory,
167167
protected RefreshTokenIssuer $refreshTokenIssuer,
168+
protected Helpers $helpers,
168169
) {
169170
parent::__construct($authCodeRepository, $refreshTokenRepository, $authCodeTTL);
170171

@@ -211,7 +212,7 @@ public function isOidcCandidate(
211212
OAuth2AuthorizationRequest $authorizationRequest,
212213
): bool {
213214
// Check if the scopes contain 'oidc' scope
214-
return (bool) Arr::find(
215+
return (bool) $this->helpers->arr()->findByCallback(
215216
$authorizationRequest->getScopes(),
216217
fn(ScopeEntityInterface $scope) => $scope->getIdentifier() === 'openid',
217218
);

src/Services/Container.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ public function __construct()
441441
$accessTokenEntityFactory,
442442
$authCodeEntityFactory,
443443
$refreshTokenIssuer,
444+
$helpers,
444445
);
445446
$this->services[AuthCodeGrant::class] = $authCodeGrantFactory->build();
446447

src/Utils/Arr.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/unit/src/Helpers/ArrTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ protected function sut(): Arr
1616
return new Arr();
1717
}
1818

19+
public function testCanFindByCallback(): void
20+
{
21+
$this->assertSame(
22+
'a',
23+
$this->sut()->findByCallback(
24+
['a', 'b', 'c'],
25+
fn($item): bool => $item === 'a'
26+
),
27+
);
28+
29+
$this->assertNull($this->sut()->findByCallback(
30+
['a', 'b', 'c'],
31+
fn($item): bool => $item === 'd'
32+
));
33+
}
34+
1935
public function testEnsureStringValues(): void
2036
{
2137
$this->assertSame(

tests/unit/src/Server/Grants/AuthCodeGrantTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPUnit\Framework\TestCase;
1010
use SimpleSAML\Module\oidc\Factories\Entities\AccessTokenEntityFactory;
1111
use SimpleSAML\Module\oidc\Factories\Entities\AuthCodeEntityFactory;
12+
use SimpleSAML\Module\oidc\Helpers;
1213
use SimpleSAML\Module\oidc\ModuleConfig;
1314
use SimpleSAML\Module\oidc\Repositories\Interfaces\AccessTokenRepositoryInterface;
1415
use SimpleSAML\Module\oidc\Repositories\Interfaces\AuthCodeRepositoryInterface;
@@ -33,6 +34,7 @@ class AuthCodeGrantTest extends TestCase
3334
protected Stub $accessTokenEntityFactoryStub;
3435
protected Stub $authCodeEntityFactoryStub;
3536
protected Stub $refreshTokenIssuerStub;
37+
protected Stub $helpersStub;
3638

3739
/**
3840
* @throws \Exception
@@ -49,6 +51,7 @@ protected function setUp(): void
4951
$this->accessTokenEntityFactoryStub = $this->createStub(AccessTokenEntityFactory::class);
5052
$this->authCodeEntityFactoryStub = $this->createStub(AuthcodeEntityFactory::class);
5153
$this->refreshTokenIssuerStub = $this->createStub(RefreshTokenIssuer::class);
54+
$this->helpersStub = $this->createStub(Helpers::class);
5255
}
5356

5457
/**
@@ -68,6 +71,7 @@ public function testCanCreateInstance(): void
6871
$this->accessTokenEntityFactoryStub,
6972
$this->authCodeEntityFactoryStub,
7073
$this->refreshTokenIssuerStub,
74+
$this->helpersStub,
7175
),
7276
);
7377
}

0 commit comments

Comments
 (0)