Skip to content

Commit

Permalink
Merge pull request #24 from angelxmoreno/22-getter-and-setter-docbloc…
Browse files Browse the repository at this point in the history
…k-for-entities

refactor: added doc block methods to magic getters of repository
  • Loading branch information
angelxmoreno committed Jul 22, 2024
2 parents 494608d + d39e48f commit 4687161
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ jobs:
- name: Run Tests
run: composer test-ci

- name: Upload to Codecov
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODE_COV_TOKEN }}
files: coverage.info
verbose: true

- name: Upload to Codacy
run: bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r coverage.info --project-token=${{ secrets.CODACY_PROJECT_TOKEN }}
# - name: Upload to Codecov
# uses: codecov/codecov-action@v2
# with:
# token: ${{ secrets.CODE_COV_TOKEN }}
# files: coverage.info
# verbose: true

# - name: Upload to Codacy
# run: bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r coverage.info --project-token=${{ secrets.CODACY_PROJECT_TOKEN }}
47 changes: 43 additions & 4 deletions src/Core/RepositoryRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
use WPRestClient\Core\Repository\RepositoryBase;
use WPRestClient\Repository;

/**
* RepositoryRegistry class for managing and retrieving repository instances.
*
* Provides methods to dynamically retrieve repository instances using magic methods.
*
* @method Repository\CategoriesRepository categories()
* @method Repository\CommentsRepository comments()
* @method Repository\MediasRepository medias()
* @method Repository\PagesRepository pages()
* @method Repository\PostsRepository posts()
* @method Repository\TagsRepository tags()
* @method Repository\UsersRepository users()
*/
class RepositoryRegistry
{
protected ApiClient $apiClient;
Expand All @@ -27,18 +40,28 @@ class RepositoryRegistry
];

/**
* @param ApiClient $apiClient
* Constructor method.
*
* @param ApiClient $apiClient The API client instance.
*/
public function __construct(ApiClient $apiClient)
{
$this->apiClient = $apiClient;
}

/**
* Add a new repository to the registry.
*
* @param string $className The class name of the repository.
* @param string $alias The alias for the repository.
* @return void
* @throws UnexpectedValueException If the class does not exist or is not a subclass of RepositoryBase.
*/
public function addRepository(string $className, string $alias): void
{
if (!class_exists($className)) {
throw new UnexpectedValueException(sprintf(
'%s can not be found',
'%s cannot be found',
$className
));
}
Expand All @@ -54,6 +77,14 @@ public function addRepository(string $className, string $alias): void
$this->repositories[strtolower($alias)] = $className;
}

/**
* Handle dynamic method calls to get repositories by alias.
*
* @param string $methodName The name of the method being called.
* @param array $args The arguments passed to the method.
* @return RepositoryBase|string The repository class or instance.
* @throws RuntimeException If the method does not exist or if arguments are provided.
*/
public function __call($methodName, $args)
{
if (empty($args) && isset($this->repositories[$methodName])) {
Expand All @@ -64,8 +95,11 @@ public function __call($methodName, $args)
}

/**
* @param string $alias
* @return string|RepositoryBase
* Get a repository by alias.
*
* @param string $alias The alias of the repository.
* @return string|RepositoryBase The repository class or instance.
* @throws RuntimeException If the alias is not valid.
*/
public function getRepository(string $alias): string
{
Expand All @@ -78,6 +112,11 @@ public function getRepository(string $alias): string
return $repository;
}

/**
* Get all repository aliases.
*
* @return array An array of repository aliases.
*/
public function getAliases(): array
{
return array_keys($this->repositories);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Core/RepositoryRegistry.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
describe('when the class does not exist', function () {
it('throws an exception', function () {
$className = 'Foo';
$message = sprintf('%s can not be found', $className);
$message = sprintf('%s cannot be found', $className);
expect(function () use ($className) {
$this->registry->addRepository($className, 'foo');
})->toThrow(new UnexpectedValueException($message));
Expand Down

0 comments on commit 4687161

Please sign in to comment.