Created
November 28, 2014 03:41
-
-
Save mockiemockiz/f1b40ac673deb2e57f50 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 $defaultRepo; | |
protected $defaultEntity; | |
public function __construct($params) | |
{ | |
$this->params = $params; | |
} | |
public function setDoctrine($em) | |
{ | |
$this->em = $em; | |
} | |
public function setDefaultRepo($repo) | |
{ | |
$this->defaultRepo = $this->em->getRepository($repo); | |
return $this; | |
} | |
public function getDefaultRepo() | |
{ | |
return $this->defaultRepo; | |
} | |
public function setDefaultEntity($entity) | |
{ | |
$this->defaultEntity = $entity; | |
} | |
public function getDefaultEntity() | |
{ | |
return $this->defaultEntity; | |
} | |
/** | |
* @param $id | |
* @return mixed | |
* @throws | |
*/ | |
public function find($id) | |
{ | |
$entity = $this->getDefaultRepo()->find($id); | |
if (!$entity) { | |
throw new EntityNotFoundException('Unable to find MockblogPost entity.'); | |
} | |
return $entity; | |
} | |
/** | |
* 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; | |
} | |
/** | |
* 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->getDefaultEntity()); | |
$this->em->flush(); | |
return $this->getDefaultEntity()->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
public function updateAction(Request $request, $id) | |
{ | |
$deleteForm = $this->createDeleteForm($id); | |
$entity = $this->get('post_entity'); // MockblogPost.php | |
$userEntity = $entity->find($id); | |
$entity->setDefaultEntity($userEntity); | |
$editForm = $this->createEditForm($userEntity); | |
$entity->setRequest($request); | |
$entity->setForm($editForm); | |
$entity->setPostData('blog_post_type'); | |
if ($entity->save($userEntity->getId())) { | |
return $this->redirect($this->generateUrl('post_edit', array('id' => $id))); | |
} | |
return $this->render($this->container->getParameter('post_entity_name') . ':edit.html.twig', array( | |
'edit_form' => $editForm->createView(), | |
'delete_form' => $deleteForm->createView(), | |
)); | |
} |
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; | |
private $tagPostRepo; | |
/** | |
* 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 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; | |
} | |
public function getTagPostRepo() | |
{ | |
$this->tagPostRepo = $this->em->getRepository($this->params['tag_post_entity_name']); | |
return $this->tagPostRepo; | |
} | |
/** | |
* for Create only!! | |
* | |
* @return \Mockizart\Bundle\BlogBundle\Entity\MockblogPost | |
*/ | |
public function initialize() | |
{ | |
$entity = new \Mockizart\Bundle\BlogBundle\Entity\MockblogPost(); | |
self::setDefaultEntity($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) { | |
$tag = $this->getTagRepo()->find($tagId); | |
$mockblogTagPost = new MockblogTagPost($tag, $this->getDefaultEntity()); | |
$this->getDefaultEntity()->addTagPostAssociation($mockblogTagPost); | |
} | |
$this->getDefaultEntity()->toRemove(); | |
} | |
} | |
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->getDefaultEntity()->setUserId( | |
$this->getUserRepo()->find($this->getUserId()) | |
); | |
$this->addTagPostRelation(); | |
parent::create(); | |
} | |
/** | |
* Update entity. ( update only ) | |
* | |
* @return bool | |
*/ | |
public function save() | |
{ | |
$this->setSlug(); | |
$this->addTagPostRelation(); | |
parent::save(); | |
} | |
} |
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 | |
namespace Mockizart\Bundle\BlogBundle\Entity; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\ORM\Mapping as ORM; | |
use DMS\Filter\Rules as Filter; | |
use DMS\Bundle\FilterBundle\Rule as SfFilter; | |
use FOS\UserBundle\Model\User; | |
use MyProject\Proxies\__CG__\stdClass; | |
/** | |
* MockblogPost | |
*/ | |
class MockblogPost | |
{ | |
/** | |
* @var integer | |
*/ | |
private $id; | |
/** | |
* @var integer | |
*/ | |
private $creationDate; | |
/** | |
* @var integer | |
*/ | |
private $lastUpdate; | |
/** | |
* @var integer | |
*/ | |
private $status; | |
/** | |
* @var integer | |
*/ | |
private $userId; | |
/** | |
* @var integer | |
*/ | |
private $categoryId; | |
/** | |
* @Filter\StripTags() | |
* @Filter\Trim() | |
* @Filter\StripNewlines() | |
* | |
* @var string | |
*/ | |
private $title; | |
/** | |
* @var string | |
*/ | |
private $content; | |
/** | |
* @var integer | |
*/ | |
private $viewCount; | |
/** | |
* @Filter\StripTags() | |
* @Filter\Trim() | |
* @Filter\ToLower() | |
* @SfFilter\Service(service="url_friendly", method="filter") | |
* | |
* @var string | |
*/ | |
private $slug; | |
private $tags; | |
private $tagPostArray; | |
private $newTags; | |
/** | |
* | |
* @var string | |
*/ | |
public $tagPostAssociations; | |
public function __construct() { | |
$this->tagPostAssociations = new ArrayCollection(); | |
} | |
public function addTagPostAssociation(MockblogTagPost $tagPostAssociations) | |
{ | |
$newTag = $tagPostAssociations; | |
$this->newTags[$newTag->getTagId().$newTag->getPostId()] = $newTag; | |
$hasTagPost = $this->hasTagPost($newTag); | |
if (!$hasTagPost) { | |
$this->tagPostAssociations[] = $tagPostAssociations; | |
} | |
return $this; | |
} | |
public function toRemove() | |
{ | |
$tagCollections = array_replace($this->tagPostArray(), $this->newTags); | |
$old = array_keys($this->tagPostArray()); | |
$new = array_keys($this->newTags); | |
$diff = array_diff($old, $new); | |
if ($diff) { | |
foreach ($diff as $v) { | |
$this->removeTagPostAssociation($tagCollections[$v]); | |
} | |
} | |
} | |
public function hasTagPost($newTag) | |
{ | |
return in_array($newTag->getTagId().$newTag->getPostId(), array_keys($this->tagPostArray())); | |
} | |
public function tagPostArray() | |
{ | |
$this->tagPostArray = []; | |
if ($this->tagPostAssociations) { | |
foreach ($this->tagPostAssociations as $v) { | |
$this->tagPostArray[$v->getTagId().$v->getPostId()] = $v; | |
} | |
} | |
return $this->tagPostArray; | |
} | |
public function removeTagPostAssociation(MockblogTagPost $tagPost) | |
{ | |
$this->tagPostAssociations->removeElement($tagPost); | |
return $this; | |
} | |
public function getTagPostAssociations() | |
{ | |
return $this->tagPostAssociations; | |
} | |
public function setTags($tags) | |
{ | |
$this->tags = $tags; | |
return $this; | |
} | |
public function getTags() | |
{ | |
return $this->tags; | |
} | |
public function setTagsDefault(){} | |
public function getTagsDefault(){} | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set creationDate | |
* | |
* @param integer $creationDate | |
* @return MockblogPost | |
*/ | |
public function setcreationDate($creationDate) | |
{ | |
$this->creationDate = $creationDate; | |
return $this; | |
} | |
/** | |
* Get creationDate | |
* | |
* @return integer | |
*/ | |
public function getcreationDate() | |
{ | |
return $this->creationDate; | |
} | |
/** | |
* @param integer $lastUpdate | |
* | |
* @return MockblogPost | |
*/ | |
public function setLastUpdate($lastUpdate) | |
{ | |
$this->lastUpdate = $lastUpdate; | |
return $this; | |
} | |
/** | |
* Get last update | |
* | |
* @return int | |
*/ | |
public function getLastUpdate() | |
{ | |
return $this->lastUpdate; | |
} | |
/** | |
* Set status | |
* | |
* @param $status | |
* @return $this | |
* | |
*/ | |
public function setStatus($status) | |
{ | |
$this->status = $status; | |
return $this; | |
} | |
/** | |
* Get status | |
* | |
* @return int | |
*/ | |
public function getStatus() | |
{ | |
return $this->status; | |
} | |
/** | |
* Set userId | |
* | |
* @param $user | |
* @internal param int $userId | |
* @return MockblogPost | |
*/ | |
public function setUserId(User $user) | |
{ | |
$this->userId = $user; | |
return $this; | |
} | |
/** | |
* Get userId | |
* | |
* @return integer | |
*/ | |
public function getUserId() | |
{ | |
return $this->userId; | |
} | |
/** | |
* Set categoryId | |
* | |
* @param MockblogCategory $category | |
* @internal param int|\Mockizart\Bundle\BlogBundle\Entity\MockblogCategory $categoryId | |
* @return MockblogPost | |
*/ | |
public function setCategoryId(MockblogCategory $category) | |
{ | |
$this->categoryId = $category; | |
return $this; | |
} | |
/** | |
* Get categoryId | |
* | |
* @return integer | |
*/ | |
public function getCategoryId() | |
{ | |
return $this->categoryId; | |
} | |
/** | |
* Set title | |
* | |
* @param string $title | |
* @return MockblogPost | |
*/ | |
public function setTitle($title) | |
{ | |
$this->title = $title; | |
return $this; | |
} | |
/** | |
* Get title | |
* | |
* @return string | |
*/ | |
public function getTitle() | |
{ | |
return $this->title; | |
} | |
/** | |
* Set content | |
* | |
* @param string $content | |
* @return MockblogPost | |
*/ | |
public function setContent($content) | |
{ | |
$this->content = $content; | |
return $this; | |
} | |
/** | |
* Get content | |
* | |
* @return string | |
*/ | |
public function getContent() | |
{ | |
return $this->content; | |
} | |
/** | |
* Set viewCount | |
* | |
* @param integer $viewCount | |
* @return MockblogPost | |
*/ | |
public function setViewCount($viewCount) | |
{ | |
$this->viewCount = $viewCount; | |
return $this; | |
} | |
/** | |
* Get viewCount | |
* | |
* @return integer | |
*/ | |
public function getViewCount() | |
{ | |
return $this->viewCount; | |
} | |
/** | |
* Set slug | |
* | |
* @param string $slug | |
* @return MockblogPost | |
*/ | |
public function setSlug($slug) | |
{ | |
$this->slug = $slug; | |
return $this; | |
} | |
/** | |
* Get slug | |
* | |
* @return string | |
*/ | |
public function getSlug() | |
{ | |
return $this->slug; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment