Skip to content

Commit

Permalink
#5 The root Repository class implemented Some first Repository cl…
Browse files Browse the repository at this point in the history
…asses added.
  • Loading branch information
automatix committed Jul 19, 2018
1 parent 7db8179 commit a0c3884
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/Base/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
namespace App\Base\Repository;

use App\Base\Entity\AbstractEntity;
use App\Base\Exceptions\GeneralErrorContextCode;
use App\Base\Exceptions\GeneralException;
use App\Base\Selectors\AbstractSelector;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManagerInterface;

/**
* The class AbstractRepository is the parent class of all Repository classes
* and proveds the basic functionality for the data access.
*
* @package App\Base\Repository
* @author Ilya Khanataev <[email protected]>
*/
abstract class AbstractRepository
{

/** @var EntityManagerInterface */
private $entityManager;

/**
* AbstractRepository constructor.
* @param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

/**
* @param AbstractSelector $id
* @return AbstractEntity
* @throws GeneralException
*/
public function find(AbstractSelector $id)
{
$this->checkSelector($id);
return $this->getRepository()->find($id);
}

/**
* @return AbstractEntity[]
*/
public function findAll()
{
return $this->getRepository()->findAll();
}

/**
* @return string The FQCN of the Entity class, the repository is responsible for.
*/
abstract public function getEntityClass();

/**
* @return string The FQCN of the Selector for the managed Entity.
*/
abstract public function getEntitySelectorClass();

/**
* @return EntityManagerInterface
*/
protected function getEntityManager(): EntityManagerInterface
{
return $this->entityManager;
}

/**
* @param EntityManagerInterface $entityManager
* @return AbstractRepository
*/
protected function setEntityManager(EntityManagerInterface $entityManager): AbstractRepository
{
$this->entityManager = $entityManager;
return $this;
}

/**
* @return ServiceEntityRepositoryInterface
*/
protected function getRepository(): ObjectRepository
{
return $this->entityManager->getRepository($this->getEntityClass());
}

/**
* @param AbstractSelector $id
* @throws GeneralException
*/
private function checkSelector(AbstractSelector $id): void
{
$entitySelectorClass = $this->getEntitySelectorClass();
if (!($id instanceof $entitySelectorClass)) {
throw new GeneralException(null, GeneralErrorContextCode::INVALID_ARGUMENT());
}
}

}
26 changes: 26 additions & 0 deletions src/Base/Repository/ChatRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace App\Base\Repository;

use App\Base\Entity\Chat;
use App\Base\Selectors\ChatSelector;

class ChatRepository extends AbstractRepository
{

/**
* @inheritdoc
*/
public function getEntityClass()
{
return Chat::class;
}

/**
* @inheritdoc
*/
public function getEntitySelectorClass()
{
return ChatSelector::class;
}

}
26 changes: 26 additions & 0 deletions src/Base/Repository/UserRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace App\Base\Repository;

use App\Base\Entity\User;
use App\Base\Selectors\UserSelector;

class UserRepository extends AbstractRepository
{

/**
* @inheritdoc
*/
public function getEntityClass()
{
return User::class;
}

/**
* @inheritdoc
*/
public function getEntitySelectorClass()
{
return UserSelector::class;
}

}
Empty file removed src/Base/Repository/empty
Empty file.

0 comments on commit a0c3884

Please sign in to comment.