Skip to content

Commit

Permalink
#291 Avoid using class_exists for decisions
Browse files Browse the repository at this point in the history
  • Loading branch information
j3nsch committed Aug 11, 2022
1 parent 6ebc203 commit 77cdaa7
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions library/Opus/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@

use function class_exists;

/**
* Creates model and model repository objects.
*
*
*/
class ModelFactory implements ModelFactoryInterface
{
/** @var string[] Mapping of model types to separate DocumentRepository classes */
protected $repositoryClasses = [
'Document' => 'Opus\DocumentRepository'
];

/**
* @param string $type
* @return mixed
Expand Down Expand Up @@ -73,12 +83,14 @@ public function get($type, $modelId)
* @return mixed
*
* TODO reuse repository instance
* TODO Using class_exists causes exceptions in the autoloader, because it tries to load the class. If autoload
* is disabled, it doesn't find existing classes. Therefore mapping in $repositoryClasses was created to
* avoid having to check if a class exists for the decision which one to use.
*/
public function getRepository($type)
{
$repositoryClass = 'Opus\\' . $type . 'Repository';

if (class_exists($repositoryClass)) {
if (array_key_exists($type, $this->repositoryClasses)) {
$repositoryClass = $this->repositoryClasses[$type];
return new $repositoryClass();
} else {
// TODO in old implementation model classes also serve as "repositories"
Expand Down

0 comments on commit 77cdaa7

Please sign in to comment.