Created
November 27, 2014 05:21
-
-
Save mockiemockiz/ecd3243cacac389cbbe2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mockie | |
* Date: 11/27/14 | |
* Time: 10:09 AM | |
*/ | |
namespace Mockizart\Bundle\BlogBundle\Service\Entity; | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\ORM\EntityNotFoundException; | |
use Symfony\Component\CssSelector\Exception\InternalErrorException; | |
use Symfony\Component\Form\Form; | |
use Symfony\Component\HttpFoundation\Request; | |
abstract class BaseEntityService { | |
/** | |
* @var Request | |
*/ | |
protected $request; | |
/** | |
* @var EntityManager | |
*/ | |
protected $em; | |
/** | |
* @var Array | |
*/ | |
protected $params; | |
/** | |
* @var Form | |
*/ | |
protected $form; | |
/** | |
* @var Array | |
*/ | |
protected $postData; | |
protected $repo; | |
public function __construct($params) | |
{ | |
$this->params = $params; | |
} | |
public function setDoctrine($em) | |
{ | |
$this->em = $em; | |
} | |
public function setRepo($repo='') | |
{ | |
if (!$this->em) { | |
return new InternalErrorException('em property not set'); | |
} | |
if (is_object($repo)) { | |
$this->repo = $repo; | |
} else { | |
$this->repo = $this->em->getRepository($repo); | |
} | |
return $this; | |
} | |
public function getRepo() | |
{ | |
if (!$this->repo) { | |
self::setRepo(); | |
} | |
return $this->repo; | |
} | |
/** | |
* Set request | |
* | |
* @param Request $request | |
*/ | |
public function setRequest(Request $request) | |
{ | |
$this->request = $request; | |
} | |
public function getRequest() | |
{ | |
return $this->request; | |
} | |
public function setPostData($name) | |
{ | |
if (!$this->getRequest()) { | |
return new InternalErrorException('em property not set'); | |
} | |
$this->postData = $this->getRequest()->get($name); | |
return $this; | |
} | |
public function getPostData($arrayIndex='') | |
{ | |
if (!$arrayIndex) { | |
return $this->postData; | |
} | |
return $this->postData[$arrayIndex]; | |
} | |
/** | |
* Set form to service class | |
* | |
* @param $form Form | |
*/ | |
public function setForm($form) | |
{ | |
$this->form = $form; | |
} | |
/** | |
* @param $id | |
* @return mixed | |
* @throws | |
*/ | |
public function find($id) | |
{ | |
$entity = $this->getRepo()->find($id); | |
if (!$entity) { | |
throw new EntityNotFoundException('Unable to find MockblogPost entity.'); | |
} | |
return $entity; | |
} | |
/** | |
* Save new Entity a.k.a for create only | |
* | |
* @return bool | |
*/ | |
public function create() | |
{ | |
$this->form->handleRequest($this->getRequest()); | |
if ($this->form->isValid()) { | |
$this->em->persist($this->getRepo()); | |
$this->em->flush(); | |
return $this->getRepo()->getId(); | |
} | |
return false; | |
} | |
/** | |
* Update entity. ( update only ) | |
* | |
* @return bool | |
*/ | |
public function save() | |
{ | |
$this->form->handleRequest($this->getRequest()); | |
if ($this->form->isValid()) { | |
$this->em->flush(); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Delete Entity | |
* | |
* @param $id | |
* @return bool | |
*/ | |
public function delete($id) | |
{ | |
$this->form->handleRequest($this->getRequest()); | |
if ($this->form->isValid()) { | |
$entity = $this->find($id); | |
$this->em->remove($entity); | |
$this->em->flush(); | |
return true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mockie | |
* Date: 11/27/14 | |
* Time: 5:27 AM | |
*/ | |
namespace Mockizart\Bundle\BlogBundle\Service\Entity; | |
use Mockizart\Bundle\BlogBundle\Entity\MockblogTag; | |
use Mockizart\Bundle\BlogBundle\Entity\MockblogTagPost; | |
use Mockizart\Bundle\BlogBundle\Entity\User; | |
class MockblogPost extends BaseEntityService { | |
/** | |
* @var User | |
*/ | |
private $userRepo; | |
/** | |
* @var MockblogTag | |
*/ | |
private $tagRepo; | |
/** | |
* Set user logged in id | |
* | |
* @var integer | |
*/ | |
private $userId; | |
public function __construct($params) | |
{ | |
parent::__construct($params); | |
} | |
public function setPostData($name) | |
{ | |
parent::setPostData($name); | |
} | |
public function getRepo() | |
{ | |
if (!$this->repo) { | |
parent::setRepo($this->params['post_entity_name']); | |
} | |
return parent::getRepo(); | |
} | |
public function getUserRepo() | |
{ | |
$this->userRepo = $this->em->getRepository($this->params['user_entity_name']); | |
return $this->userRepo; | |
} | |
public function getTagRepo() | |
{ | |
$this->tagRepo = $this->em->getRepository($this->params['tag_entity_name']); | |
return $this->tagRepo; | |
} | |
/** | |
* for Create only!! | |
* | |
* @return \Mockizart\Bundle\BlogBundle\Entity\MockblogPost | |
*/ | |
public function initialize() | |
{ | |
$entity = new \Mockizart\Bundle\BlogBundle\Entity\MockblogPost(); | |
parent::setRepo($entity); | |
return $entity; | |
} | |
/** | |
* Set slug, if user leave the slug blank, use title as slug instead. | |
* | |
*/ | |
private function setSlug() | |
{ | |
$slug = ($this->getPostData('slug')) ? $this->getPostData('slug') : $this->getPostData('title'); | |
$this->request->request->set('blog_post_type', array_merge($this->getPostData(),['slug' => $slug])); | |
} | |
/** | |
* Save tag_id and post_id to mockblog_tag_post table. | |
*/ | |
public function addTagPostRelation() | |
{ | |
$tag = $this->getPostData('tags'); | |
$tags = null; | |
$tags = ($tag) ? explode(',', $tag) : false; | |
if ($tags) { | |
foreach ($tags as $tagId) { | |
/** | |
* @TODO since I have no idea how to injecting service to service. | |
* don't forget to create service for these and try it. And may god be with you.. XD | |
*/ | |
$tag = $this->getTagRepo()->find($tagId); | |
$mockblogTagPost = new MockblogTagPost(); | |
$mockblogTagPost->setPost($this->getRepo()); | |
$mockblogTagPost->setTag($tag); | |
$this->getRepo()->addTagPostAssociation($mockblogTagPost); | |
} | |
} | |
} | |
public function setUserId($userId) | |
{ | |
$this->userId = $userId; | |
} | |
public function getUserId() | |
{ | |
return $this->userId; | |
} | |
/** | |
* Save new Entity a.k.a for create only | |
* | |
* @return bool | |
*/ | |
public function create() | |
{ | |
$this->setSlug(); | |
$this->getRepo()->setUserId( | |
$this->getUserRepo()->find($this->getUserId()) | |
); | |
$this->addTagPostRelation(); | |
parent::create(); | |
} | |
/** | |
* Update entity. ( update only ) | |
* | |
* @return bool | |
*/ | |
public function save() | |
{ | |
$this->setSlug(); | |
parent::save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment