Skip to content

Commit

Permalink
code styles
Browse files Browse the repository at this point in the history
added download delete method
added new secret question creation
  • Loading branch information
kokspflanze committed Aug 31, 2015
1 parent 5eb3550 commit 6c5275b
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 20 deletions.
15 changes: 15 additions & 0 deletions src/PServerCMS/Entity/Repository/DownloadList.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,19 @@ public function getDownload4Id( $id )

return $query->getOneOrNullResult();
}

/**
* @param $downloadId
* @return mixed
*/
public function deleteDownloadEntry($downloadId)
{
$query = $this->createQueryBuilder('p')
->delete($this->getEntityName(), 'p')
->where('p.id = :id')
->setParameter('id', $downloadId)
->getQuery();

return $query->execute();
}
}
9 changes: 9 additions & 0 deletions src/PServerCMS/Entity/Repository/SecretQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ public function getQuestion4Id( $id )

return $query->getOneOrNullResult();
}

/**
* @return \Doctrine\ORM\QueryBuilder
*/
public function getQuestionQueryBuilder()
{
return $this->createQueryBuilder( 'p' )
->select( 'p' );
}
}
4 changes: 2 additions & 2 deletions src/PServerCMS/Entity/Repository/UserCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getData4CodeType($code, $type)
public function deleteCodes4User($userId, $type)
{
$query = $this->createQueryBuilder('p')
->delete('PServerCMS\Entity\UserCodes','p')
->delete($this->getEntityName(), 'p')
->where('p.user = :user_id')
->setParameter('user_id', $userId)
->andWhere('p.type = :type')
Expand All @@ -57,7 +57,7 @@ public function deleteCodes4User($userId, $type)
*/
public function getCode( $code )
{
return $this->findOneBy(array('code' => $code));
return $this->findOneBy(['code' => $code]);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/PServerCMS/Helper/HelperForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function getAdminUserBlockForm()
return $this->getService('pserver_admin_user_block_form');
}

/**
* @return \PServerAdmin\Form\SecretQuestion
*/
public function getAdminSecretQuestionForm()
{
return $this->getService('pserver_admin_secret_question_form');
}

/**
* @return \PServerAdmin\Form\UserRole
*/
Expand Down
40 changes: 27 additions & 13 deletions src/PServerCMS/Service/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ class Download extends InvokableBase
public function getActiveList()
{
$downloadInfo = $this->getCachingHelperService()->getItem(Caching::DOWNLOAD, function() {
/** @var \PServerCMS\Entity\Repository\DownloadList $repository */
$repository = $this->getEntityManager()->getRepository($this->getEntityOptions()->getDownloadList());
return $repository->getActiveDownloadList();
return $this->getDownloadRepository()->getActiveDownloadList();
});

return $downloadInfo;
Expand All @@ -27,9 +25,7 @@ public function getActiveList()
*/
public function getDownloadList()
{
/** @var \PServerCMS\Entity\Repository\DownloadList $repository */
$repository = $this->getEntityManager()->getRepository($this->getEntityOptions()->getDownloadList());
return $repository->getDownloadList();
return $this->getDownloadRepository()->getDownloadList();
}

/**
Expand All @@ -38,28 +34,28 @@ public function getDownloadList()
*/
public function getDownload4Id( $id )
{
/** @var \PServerCMS\Entity\Repository\DownloadList $repository */
$repository = $this->getEntityManager()->getRepository($this->getEntityOptions()->getDownloadList());
return $repository->getDownload4Id($id);
return $this->getDownloadRepository()->getDownload4Id($id);
}

/**
* @param array $data
* @param null $currentDownload
* @param null|DownloadList $currentDownload
*
* @return bool|DownloadList
*/
public function download( array $data, $currentDownload = null)
{
if($currentDownload == null){
$currentDownload = new DownloadList();
if ($currentDownload == null) {
$class = $this->getEntityOptions()->getDownloadList();
/** @var DownloadList $currentDownload */
$currentDownload = new $class;
}

$form = $this->getAdminDownloadForm();
$form->setData($data);
$form->setHydrator(new HydratorDownload());
$form->bind($currentDownload);
if(!$form->isValid()){
if (!$form->isValid()) {
return false;
}

Expand All @@ -72,4 +68,22 @@ public function download( array $data, $currentDownload = null)

return $download;
}

/**
* @param DownloadList $downloadEntry
* @return mixed
*/
public function deleteDownloadEntry(DownloadList $downloadEntry)
{
return $this->getDownloadRepository()->deleteDownloadEntry($downloadEntry->getId());
}

/**
* @return \PServerCMS\Entity\Repository\DownloadList
*/
protected function getDownloadRepository()
{
return $this->getEntityManager()->getRepository($this->getEntityOptions()->getDownloadList());;
}

}
59 changes: 54 additions & 5 deletions src/PServerCMS/Service/SecretQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace PServerCMS\Service;

use PServerAdmin\Mapper\HydratorSecretQuestion;
use PServerCMS\Entity\UserInterface;
use PServerCMS\Entity\SecretQuestion as Entity;

class SecretQuestion extends InvokableBase
{
Expand All @@ -19,6 +21,7 @@ public function setSecretAnswer( UserInterface $user, $questionId, $answer )
$class = $this->getEntityOptions()->getSecretAnswer();
/** @var \PServerCMS\Entity\SecretAnswer $secretAnswer */
$secretAnswer = new $class;

$secretAnswer->setUser($user)
->setAnswer(trim($answer))
->setQuestion( $this->getQuestion4Id($questionId) );
Expand All @@ -43,7 +46,48 @@ public function isAnswerAllowed( UserInterface $user, $answer )
$realAnswer = strtolower(trim($answerEntity->getAnswer()));
$plainAnswer = strtolower(trim($answer));

return $realAnswer == $plainAnswer;
return $realAnswer === $plainAnswer;
}

/**
* @return \Doctrine\ORM\QueryBuilder
*/
public function getQuestionQueryBuilder()
{
return $this->getQuestionRepository()->getQuestionQueryBuilder();
}


/**
* @param array $data
* @param null|Entity $currentSecretQuestion
*
* @return bool|Entity
*/
public function secretQuestion(array $data, $currentSecretQuestion = null)
{
if ($currentSecretQuestion == null) {
$class = $this->getEntityOptions()->getSecretQuestion();
/** @var Entity $currentSecretQuestion */
$currentSecretQuestion = new $class;
}

$form = $this->getAdminSecretQuestionForm();
$form->setData($data);
$form->setHydrator(new HydratorSecretQuestion());
$form->bind($currentSecretQuestion);
if (!$form->isValid()) {
return false;
}

/** @var Entity $secretQuestion */
$secretQuestion = $form->getData();

$entity = $this->getEntityManager();
$entity->persist($secretQuestion);
$entity->flush();

return $secretQuestion;
}

/**
Expand All @@ -53,10 +97,7 @@ public function isAnswerAllowed( UserInterface $user, $answer )
*/
protected function getQuestion4Id( $questionId )
{
/** @var \PServerCMS\Entity\Repository\SecretQuestion $repository */
$repository = $this->getEntityManager()->getRepository($this->getEntityOptions()->getSecretQuestion());

return $repository->getQuestion4Id( $questionId );
return $this->getQuestionRepository()->getQuestion4Id( $questionId );
}

/**
Expand All @@ -67,4 +108,12 @@ protected function getEntityManagerAnswer()
return $this->getEntityManager()->getRepository($this->getEntityOptions()->getSecretAnswer());
}

/**
* @return \PServerCMS\Entity\Repository\SecretQuestion $repository
*/
protected function getQuestionRepository()
{
return $this->getEntityManager()->getRepository($this->getEntityOptions()->getSecretQuestion());
}

}

0 comments on commit 6c5275b

Please sign in to comment.