Added findOneByIdOrSlug method

This commit is contained in:
Kevin Frantz 2019-01-13 15:44:25 +01:00
parent 73d2583846
commit 054a136f14

View File

@ -14,6 +14,24 @@ final class SourceRepository extends EntityRepository
*/
public function findOneBySlug(string $slug): ?SourceInterface
{
return $this->findOneBy(['slug' => $slug]);
return $this->findOneBy([
'slug' => $slug,
]);
}
/**
* Loads a source by id or if not defined, by slug.
*
* @param SourceInterface $requestedSource
*
* @return SourceInterface|null
*/
public function findOneByIdOrSlug(SourceInterface $requestedSource): ?SourceInterface
{
try {
return $this->find($requestedSource->getId());
} catch (\Error $error) {
return $this->findOneBySlug($requestedSource->getSlug());
}
}
}