diff --git a/src/Core/Interfaces/RequestChainableInterface.php b/src/Core/Interfaces/RequestChainableInterface.php index 450f6ad..c1ecf0f 100644 --- a/src/Core/Interfaces/RequestChainableInterface.php +++ b/src/Core/Interfaces/RequestChainableInterface.php @@ -2,6 +2,9 @@ namespace Gitea\Core\Interfaces; +use Gitea\Client; +use Gitea\Model\Repository; + /** * Interface that allows the tracking of request heirarchies * @@ -65,4 +68,17 @@ interface RequestChainableInterface * @return array */ public function debugRequestChain(): string; + + /** + * Climb up the request chain searching for + * a repository object. If a repository is found + * it will be returned otherwise the method will + * make an API request to retrieve it + * + * @author Benjamin Blake (sitelease.ca) + * @param string $owner The owner of the repository + * @param string $name The name of the repository + * @return Repository|null + */ + public function findOrRequestRepository(string $owner, string $name): ?Repository; } diff --git a/src/Core/Traits/RequestChainable.php b/src/Core/Traits/RequestChainable.php index b3386c9..1d37f08 100644 --- a/src/Core/Traits/RequestChainable.php +++ b/src/Core/Traits/RequestChainable.php @@ -3,6 +3,8 @@ namespace Gitea\Core\Traits; use Gitea\Client; +use Gitea\Model\Repository; + use Gitea\Core\Interfaces\RequestChainableInterface; /** @@ -127,4 +129,27 @@ trait RequestChainable return $requestChainDebug; } + /** + * Climb up the request chain searching for + * a repository object. If a repository is found + * it will be returned otherwise the method will + * make an API request to retrieve it + * + * @author Benjamin Blake (sitelease.ca) + * @param string $owner The owner of the repository + * @param string $name The name of the repository + * @return Repository|null + */ + public function findOrRequestRepository(string $owner, string $name): ?Repository + { + $repository = $this->searchRequestChain(Repository::class); + + if (!$repository) { + $client = $this->getClient(); + $repository = $client->repositories()->getByName($owner, $name); + } + + return $repository; + } + }