0
\$\begingroup\$

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)
 );
 });
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 13, 2016 at 5:59
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You can merge the methods findBySlug, findByTitle and other findBy methods into one method that accepts parameter field and value

$this->findBy($field, $title);

And move it in BaseRepository Class. See this article Repository Pattern

so it would look something like this

public function findBy( $field, $value ) {
 return $this->model->where($field, $value);
}
answered Aug 25, 2016 at 9:16
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.