I am practicing to code in the right way and make it testable. Please review this code and tell me anything bad practices of it.
Entity:
namespace MockBlog\Entities;
use Doctrine\ORM\Mapping as ORM;
//use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="mockblog_page")
*/
class Page {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $title;
/**
* @ORM\Column(type="string")
*/
protected $slug;
/**
* @param $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @param $slug
*/
public function setSlug($slug)
{
$this->slug = $slug;
}
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* get url of the page
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
}
Interface post repository:
interface PostRepository {
/**
* Find post by the title
*
* @param $title
* @return mixed
*/
public function findByTitle($title);
/**
* Find post by the url
*
* @param $slug
* @return mixed
*/
public function findBySlug($slug);
/**
* Find post by user id
*
* @param $userId
* @return mixed
*/
public function findByUserId($userId);
}
Base repository (still blank but i think i will need it later):
class BaseRepository extends EntityRepository {
}
Base post repository :
abstract class BasePostRepository extends BaseRepository implements PostRepository {
abstract public function save($data, $id);
public function findBySlug($slug)
{
return $this->findBy(['slug' => $slug]);
}
public function findByTitle($title)
{
return $this->findBy(['title' => $title]);
}
public function findByUserId($userId)
{
return $this->findBy(['user_id' => $userId]);
}
}
Doctrine page repository:
class DoctrinePageRepository extends BasePostRepository {
public function save($data, $id = 0)
{
$entity = (empty($id)) ? $this->_class : $this->find($id);
$entity->setTitle($data['title']);
$entity->setSlug($data['slug']);
try{
EntityManager::persist($entity);
EntityManager::flush();
return true;
} catch (Exception $err){
return FALSE;
}
}
}
Service provider:
public function register()
{
// TODO: Implement register() method.
$this->app->bind(DoctrinePageRepository::class, function($app){
return new DoctrinePageRepository(
$app['em'], $app['em']->getClassMetaData(Page::class)
);
});
}