-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from City-of-Helsinki/UHF-X-project-roles
Added service to determine currently active project roles, such as "core" instance
- Loading branch information
Showing
7 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_api_base\Environment; | ||
|
||
/** | ||
* A service to determine the roles of currently active project. | ||
*/ | ||
final class ActiveProjectRoles { | ||
|
||
public function __construct(private readonly EnvironmentResolverInterface $environmentResolver) { | ||
} | ||
|
||
/** | ||
* Checks if the current instance has the given role. | ||
* | ||
* @return bool | ||
* TRUE if the project has given role. | ||
*/ | ||
public function hasRole(ProjectRoleEnum $role): bool { | ||
return in_array($role, $this->getRoles()); | ||
} | ||
|
||
/** | ||
* Gets the project features. | ||
* | ||
* @return array<\Drupal\helfi_api_base\Environment\ProjectRoleEnum> | ||
* The features. | ||
*/ | ||
private function getRoles(): array { | ||
$roles = []; | ||
|
||
try { | ||
// Instance is considered as a "core" if it's included in main-navigation | ||
// structure, so basically all Drupal instances under www.hel.fi domain. | ||
// | ||
// Currently, only core instances are defined in EnvironmentResolver, so | ||
// we can use ::getActiveProject() to determine if the current instance is | ||
// a core instance. | ||
// @todo Include all instances in EnvironmentResolver and include | ||
// Project role data in Project object. | ||
$this->environmentResolver->getActiveProject(); | ||
|
||
$roles[] = ProjectRoleEnum::Core; | ||
} | ||
catch (\InvalidArgumentException) { | ||
} | ||
return $roles; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_api_base\Environment; | ||
|
||
/** | ||
* Defines the project features. | ||
*/ | ||
enum ProjectRoleEnum { | ||
case Core; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Tests\helfi_api_base\Unit\Environment; | ||
|
||
use Drupal\Tests\UnitTestCase; | ||
use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait; | ||
use Drupal\helfi_api_base\Environment\ActiveProjectRoles; | ||
use Drupal\helfi_api_base\Environment\EnvironmentEnum; | ||
use Drupal\helfi_api_base\Environment\Project; | ||
use Drupal\helfi_api_base\Environment\ProjectRoleEnum; | ||
|
||
/** | ||
* Tests ActiveProjectType. | ||
* | ||
* @group helfi_api_base | ||
*/ | ||
class ActiveProjectRolesTest extends UnitTestCase { | ||
|
||
use EnvironmentResolverTrait; | ||
|
||
/** | ||
* Tests ::isCoreInstance(). | ||
* | ||
* @dataProvider isCoreInstanceData | ||
*/ | ||
public function testIsCoreInstance(bool $expected, ?string $projectName, ?EnvironmentEnum $env): void { | ||
$sut = new ActiveProjectRoles($this->getEnvironmentResolver($projectName, $env)); | ||
$this->assertEquals($expected, $sut->hasRole(ProjectRoleEnum::Core)); | ||
} | ||
|
||
/** | ||
* A data provider. | ||
* | ||
* @return array[] | ||
* The data. | ||
*/ | ||
public function isCoreInstanceData(): array { | ||
return [ | ||
[FALSE, NULL, NULL], | ||
[TRUE, Project::ASUMINEN, EnvironmentEnum::Local], | ||
[FALSE, 'non-existent', NULL], | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters