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)
        );
    });

}
\$\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);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.