Skip to content

Commit

Permalink
refactoring repos, moving in traits, adding isVerified to Person
Browse files Browse the repository at this point in the history
  • Loading branch information
cam-spacebar committed May 14, 2021
1 parent 2a8c6b0 commit 1f94a7a
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 7 deletions.
23 changes: 23 additions & 0 deletions Entity/BasePerson.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ class BasePerson extends BaseEntity implements BasePersonInterface, JsonSerializ
*/
protected $verificationToken;

/**
* the random string that is sent to a new user's email address to verify it's real
*
* @var string
*
* @ORM\Column(name="isVerified", type="boolean", nullable=false)
*/
protected $isVerified;

/**
* @ORM\Column(type="string", length=255)
* This must be the encoded string (not the raw password string)
Expand Down Expand Up @@ -606,4 +615,18 @@ public function createVerificationHash(): string

return $hash;
}

public function setIsVerified(bool $isVerified): self
{

$this->isVerified = $isVerified;

return $this;
}

public function getIsVerified(): bool
{

return $this->isVerified;
}
}
17 changes: 17 additions & 0 deletions Exceptions/PersonNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace App\VisageFour\Bundle\ToolsBundle\Exceptions;


class PersonNotFound extends \Exception
{
public function __construct($email)
{
parent::__construct();

$this->message =
'Person with email address: '. $email .' was not found.'
;
}
}
24 changes: 21 additions & 3 deletions Repository/BasePersonRepository.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

namespace VisageFour\Bundle\ToolsBundle\Repository;
use App\VisageFour\Bundle\ToolsBundle\Exceptions\PersonNotFound;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use VisageFour\Bundle\ToolsBundle\Entity\BasePerson;

/**
Expand All @@ -10,23 +13,38 @@
*/
class BasePersonRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, $entityClassName)
{
// note: you must create a class that overrides this and passes in the correct $entityClassName parameter.
// the commented out section below cannot be used:
// parent::__construct($registry, BasePerson::class);

parent::__construct($registry, $entityClassName);
}
public function doesPersonExist($email)
{
$person = $this->findOneByEmailCanonical($email);
$person = $this->findOneByEmailCanonical($email, false);

if (!empty($person)) {
return true;
}

return false;
}

/**
* Canonicalize email
*/
public function findOneByEmailCanonical ($email) {
public function findOneByEmailCanonical ($email, $throwExceptionIfNotFound = true) {
$emailCanon = BasePerson::canonicalizeEmail($email);
return $this->findOneBy(array(

$result = $this->findOneBy(array(
'emailCanonical' => $emailCanon
));

if (empty($result) && $throwExceptionIfNotFound){
throw new PersonNotFound($email);
}
return $result;
}
}
9 changes: 8 additions & 1 deletion Repository/CarrierNumberRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

namespace VisageFour\Bundle\ToolsBundle\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use VisageFour\Bundle\ToolsBundle\Entity\CarrierNumber;

/**
* CarrierNumberRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CarrierNumberRepository extends \Doctrine\ORM\EntityRepository
class CarrierNumberRepository extends ServiceEntityRepository
{
public function __construct (ManagerRegistry $registry) {
parent::__construct($registry, CarrierNumber::class);
}
}
6 changes: 5 additions & 1 deletion Repository/CodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace VisageFour\Bundle\ToolsBundle\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use VisageFour\Bundle\ToolsBundle\Entity\Code;

/**
Expand All @@ -10,8 +11,11 @@
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CodeRepository extends ServiceEntityRepository
class CodeRepository extends ServiceEntityRepository
{
public function __construct (ManagerRegistry $registry) {
parent::__construct($registry, Code::class);
}
public function getByCode($code) : ?Code
{
return $this->findOneBy([
Expand Down
4 changes: 4 additions & 0 deletions Repository/EmailRegisterRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace VisageFour\Bundle\ToolsBundle\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use VisageFour\Bundle\ToolsBundle\Entity\EmailRegister;

/**
Expand All @@ -13,6 +14,9 @@
*/
class EmailRegisterRepository extends ServiceEntityRepository
{
public function __construct (ManagerRegistry $registry) {
parent::__construct($registry, EmailRegister::class);
}
public function countSpooled () {
$qb = $this->createQueryBuilder('er')
->select('COUNT(er)')
Expand Down
8 changes: 7 additions & 1 deletion Repository/SlugRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

namespace VisageFour\Bundle\ToolsBundle\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
* SlugRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class SlugRepository extends \Doctrine\ORM\EntityRepository
class SlugRepository extends ServiceEntityRepository
{
public function __construct (ManagerRegistry $registry) {
parent::__construct($registry, SlugRepository::class);
}
}
2 changes: 2 additions & 0 deletions Services/BaseEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class: Twencha\Bundle\EventRegistrationBundle\Services\EventSeriesManager
public function __construct(EntityManager$em, $class, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) {
$this->em = $em;
$this->repo = $this->em->getRepository($class);
// dump($this->repo, $class);
$this->dispatcher = $eventDispatcher; // this should be phased out as a variable
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
Expand Down Expand Up @@ -166,6 +167,7 @@ public function getOneById($id) {
$result = $this->repo->findOneBy(
array ('id' => $id)
);

return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion Services/CSVMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace VisageFour\Bundle\ToolsBundle\Services;

use App\Traits\LoggerTrait;
use VisageFour\Bundle\ToolsBundle\Traits\LoggerTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\Response;
Expand Down
38 changes: 38 additions & 0 deletions Traits/LoggerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/*
* created on: 03/06/2020 at 4:47 PM
* by: cameronrobertburns
*/

namespace VisageFour\Bundle\ToolsBundle\Traits;

use Psr\Log\LoggerInterface;

trait LoggerTrait
{
/**
* @var LoggerInterface|null
*/
protected $logger;

/**
* @required
* note: required is what tells symfony to call this and inject $logger
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}

private function logInfo(string $message, array $context = [])
{
$this->checkLoggerIsSet();
$this->logger->info($message, $context);
}

private function checkLoggerIsSet () {
if (empty($this->logger)) {
throw new \Exception ("LoggerTrait dependency: 'Logger' has not been set. Please set it prior to using the: ". __CLASS__ ." class." );
}
}
}
37 changes: 37 additions & 0 deletions Traits/RouterTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/*
* created on: 30/06/2020 at 10:53 PM
* by: cameronrobertburns
*/

namespace VisageFour\Bundle\ToolsBundle\Traits;

use Symfony\Component\Routing\Router;

trait RouterTrait
{
/**
* @var Router|null
*/
private $router;

/**
* @required
*/
public function setRouter(Router $router)
{
$this->router = $router;
}

protected function getRouter() : Router
{
$this->checkRouterIsSet();
return $this->router;
}

private function checkRouterIsSet () {
if (empty($this->router)) {
throw new \Exception ("RouterTrait dependency: 'Router' has not been set. Please set it prior to using the: ". __CLASS__ ." class." );
}
}
}

0 comments on commit 1f94a7a

Please sign in to comment.