Browse Source

Refactoring Repo

develop
Guillaume 3 years ago
parent
commit
572eedabfb
13 changed files with 336 additions and 67 deletions
  1. +5
    -3
      Doctrine/QueryBuilder.php
  2. +61
    -5
      Repository/AbstractRepositoryQuery.php
  3. +76
    -2
      Repository/AbstractStore.php
  4. +20
    -3
      Repository/Reminder/ReminderStore.php
  5. +51
    -0
      Repository/RepositoryQueryInterface.php
  6. +20
    -1
      Repository/Site/NewsStore.php
  7. +18
    -0
      Repository/Site/PageStore.php
  8. +0
    -26
      Repository/StatusRepositoryQueryTrait.php
  9. +30
    -0
      Repository/StoreInterface.php
  10. +21
    -3
      Repository/Ticket/TicketStore.php
  11. +0
    -24
      Repository/TreeRepositoryQueryTrait.php
  12. +17
    -0
      Repository/User/GroupUserStore.php
  13. +17
    -0
      Repository/User/UserStore.php

+ 5
- 3
Doctrine/QueryBuilder.php View File

@@ -25,8 +25,10 @@ class QueryBuilder extends DoctrineQueryBuilder
return $this;
}

/*public function andIsOnline(){

}*/
public function andWhereStatus($dqlId, $status){
$this->andWhere($dqlId.'.status = :status');
$this->setParameter('status', $status);
return $this;
}

}

+ 61
- 5
Repository/AbstractRepositoryQuery.php View File

@@ -5,14 +5,10 @@ namespace Lc\SovBundle\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Knp\Component\Pager\PaginatorInterface;
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\CaracoleBundle\Model\Section\SectionInterface;
use Lc\SovBundle\Doctrine\EntityInterface;

abstract class AbstractRepositoryQuery
abstract class AbstractRepositoryQuery implements RepositoryQueryInterface
{
use StatusRepositoryQueryTrait;
use TreeRepositoryQueryTrait;

protected ServiceEntityRepository $repository;
protected QueryBuilder $query;
@@ -106,6 +102,15 @@ abstract class AbstractRepositoryQuery
return $data;
}

public function groupBy(string $field): self
{
if (substr($field, 0, 1) === '.') {
return $this->groupBy($field) ;
} else {
return $this->groupBy('.'.$field) ;
}
}

public function orderBy(string $field, string $sort = 'ASC'): self
{
if (substr($field, 0, 1) === '.') {
@@ -122,6 +127,9 @@ abstract class AbstractRepositoryQuery
->setParameter('id', $id);
}

/*
* DEVALIAS
*/
public function filterByDevAlias(string $devAlias): self
{
return $this
@@ -129,6 +137,9 @@ abstract class AbstractRepositoryQuery
->setParameter('devAlias', $devAlias);
}

/*
* SLUG
*/
public function filterBySlug(string $slug)
{
return $this
@@ -136,5 +147,50 @@ abstract class AbstractRepositoryQuery
->setParameter('slug', $slug);
}

/*
* TREE
*/
public function filterIsParent()
{
return $this->andWhere('.parent is NULL');
}

public function filterIsChildren()
{
return $this->andWhere('.parent is NOT NULL');
}

public function filterByParent(EntityInterface $parent)
{
return $this->andWhere('.parent = :parent')->setParameter('parent', $parent);
}

/*
* STATUS
*/
public function filterByStatus(int $status):self
{
return $this->andWhereStatus($this->id, $status);
}

public function filterIsOffline():self
{
return $this->andWhereStatus($this->id, 0);
}

public function filterIsOnline():self
{
return $this->andWhereStatus($this->id, 1);
}

public function filterIsDeleted():self
{
return $this->andWhereStatus($this->id, -1);
}

public function filterIsOnlineAndOffline():self
{
return $this->andWhere('.status >= 0');
}
}


+ 76
- 2
Repository/AbstractStore.php View File

@@ -2,11 +2,21 @@

namespace Lc\SovBundle\Repository;

use Lc\SovBundle\Doctrine\EntityInterface;
use Lc\SovBundle\Model\Site\PageInterface;

abstract class AbstractStore
abstract class AbstractStore implements StoreInterface
{
public function createQuery($query = null)
public function createDefaultQuery(RepositoryQueryInterface $query = null): RepositoryQueryInterface
{
$query = $this->createQuery($query);
$this->filtersDefault($query);
$this->relationsDefault($query);
$this->orderByDefault($query);
return $query;
}

public function createQuery(RepositoryQueryInterface $query = null): RepositoryQueryInterface
{
if (is_null($query)) {
$query = $this->query->create();
@@ -14,7 +24,23 @@ abstract class AbstractStore

return $query;
}
/*
public function orderByDefault(RepositoryQueryInterface $query)
{
return $query;
}

public function filtersDefault(RepositoryQueryInterface $query)
{
$query->filterIsOnlineAndOffline();
return $query;
}

public function relationsDefault($query)
{
return $query;
}
*/
public function getRepositoryQuery()
{
return $this->query;
@@ -28,4 +54,52 @@ abstract class AbstractStore

return $query->findOne();
}

public function getOneBySlug(string $slug, bool $isOnline = true, $query = null)
{
$query = $this->createDefaultQuery($query);
$query->filterBySlug($slug);

if ($isOnline) {
$query->filterIsOnline();
}

return $query->findOne();
}

public function getOneByDevAlias(string $devAlias, bool $isOnline = true, $query = null)
{
$query = $this->createDefaultQuery($query);
$query->filterByDevAlias($devAlias);

if ($isOnline) {
$query->filterIsOnline();
}

return $query->findOne();
}

public function get($query = null)
{
$query = $this->createDefaultQuery($query);
return $query->find();
}

public function getOnline($query = null)
{
$query = $this->createDefaultQuery($query);
$query->filterIsOnline();
return $query->find();
}

public function getParent(bool $isOnline = true, $query = null)
{
$query = $this->createDefaultQuery($query);
if ($isOnline) {
$query->filterIsOnline();
}
$query->filterIsParent();

return $query->find();
}
}

+ 20
- 3
Repository/Reminder/ReminderStore.php View File

@@ -4,6 +4,7 @@ namespace Lc\SovBundle\Repository\Reminder;

use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Repository\AbstractStore;
use Lc\SovBundle\Repository\RepositoryQueryInterface;

class ReminderStore extends AbstractStore implements ReminderStoreInterface
{
@@ -14,9 +15,25 @@ class ReminderStore extends AbstractStore implements ReminderStoreInterface
$this->query = $query;
}

public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
$query->orderBy('id');
return $query;
}

public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}

public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}

public function get($params = [], $query = null)
{
$query = $this->createQuery($query);
$query = $this->createDefaultQuery($query);

$query->filterByDone();

@@ -46,7 +63,7 @@ class ReminderStore extends AbstractStore implements ReminderStoreInterface
// findByUser
public function getByUser(UserInterface $user, $query = null): array
{
$query = $this->createQuery($query);
$query = $this->createDefaultQuery($query);

$query = $this->query->create();

@@ -67,7 +84,7 @@ class ReminderStore extends AbstractStore implements ReminderStoreInterface
int $entityId = null,
$query = null
): array {
$query = $this->createQuery($query);
$query = $this->createDefaultQuery($query);

$query
->filterByUser($user)

+ 51
- 0
Repository/RepositoryQueryInterface.php View File

@@ -0,0 +1,51 @@
<?php

namespace Lc\SovBundle\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Lc\SovBundle\Doctrine\EntityInterface;

interface RepositoryQueryInterface
{
public function create();

public function call(callable $fn): \Lc\SovBundle\Repository\AbstractRepositoryQuery;

public function count(): string;

public function findOne(): ?EntityInterface;

public function find(): array;

public function limit(int $maxResults);

public function paginate(int $page = 1, int $limit = 20);

public function getRepository(): ServiceEntityRepository;

public function groupBy(string $field): \Lc\SovBundle\Repository\AbstractRepositoryQuery;

public function orderBy(string $field, string $sort = 'ASC'): \Lc\SovBundle\Repository\AbstractRepositoryQuery;

public function filterById(int $id);

public function filterByDevAlias(string $devAlias): \Lc\SovBundle\Repository\AbstractRepositoryQuery;

public function filterBySlug(string $slug);

public function filterIsParent();

public function filterIsChildren();

public function filterByParent(EntityInterface $parent);

public function filterByStatus($status): \Lc\SovBundle\Repository\AbstractRepositoryQuery;

public function filterIsOffline(): \Lc\SovBundle\Repository\AbstractRepositoryQuery;

public function filterIsOnline(): \Lc\SovBundle\Repository\AbstractRepositoryQuery;

public function filterIsDeleted(): \Lc\SovBundle\Repository\AbstractRepositoryQuery;

public function filterIsOnlineAndOffline(): \Lc\SovBundle\Repository\AbstractRepositoryQuery;
}

+ 20
- 1
Repository/Site/NewsStore.php View File

@@ -2,10 +2,13 @@

namespace Lc\SovBundle\Repository\Site;

use Lc\CaracoleBundle\Repository\SectionStoreTrait;
use Lc\SovBundle\Repository\AbstractStore;
use Lc\SovBundle\Repository\RepositoryQueryInterface;

class NewsStore extends AbstractStore implements NewsStoreInterface
{

protected NewsRepositoryQueryInterface $query;

public function __construct(NewsRepositoryQueryInterface $query)
@@ -13,10 +16,26 @@ class NewsStore extends AbstractStore implements NewsStoreInterface
$this->query = $query;
}

public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
$query->orderBy('id');
return $query;
}

public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}

public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}

// findLatests
public function getLatests(int $maxResults = 0, $query = null): array
{
$query = $this->createQuery($query);
$query = $this->createDefaultQuery($query);

$query
->filterIsOnline()

+ 18
- 0
Repository/Site/PageStore.php View File

@@ -4,6 +4,7 @@ namespace Lc\SovBundle\Repository\Site;

use Lc\SovBundle\Model\Site\PageInterface;
use Lc\SovBundle\Repository\AbstractStore;
use Lc\SovBundle\Repository\RepositoryQueryInterface;

class PageStore extends AbstractStore implements PageStoreInterface
{
@@ -13,4 +14,21 @@ class PageStore extends AbstractStore implements PageStoreInterface
{
$this->query = $query;
}

public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
$query->orderBy('position');
return $query;
}

public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
$query->filterIsOnlineAndOffline();
return $query;
}

public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}
}

+ 0
- 26
Repository/StatusRepositoryQueryTrait.php View File

@@ -1,26 +0,0 @@
<?php

namespace Lc\SovBundle\Repository;

trait StatusRepositoryQueryTrait
{
public function filterByStatus(int $status):self
{
return $this->andWhere('.status = :status')->setParameter(':status', $status);
}

public function filterIsOffline():self
{
return $this->andWhere('.status = :status')->setParameter(':status', 0);
}

public function filterIsOnline():self
{
return $this->andWhere('.status = :status')->setParameter(':status', 1);
}

public function filterIsOnlineAndOffline():self
{
return $this->andWhere('.status >= 0');
}
}

+ 30
- 0
Repository/StoreInterface.php View File

@@ -0,0 +1,30 @@
<?php

namespace Lc\SovBundle\Repository;

interface StoreInterface
{
public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface;

public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface;

public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface;

public function createDefaultQuery(RepositoryQueryInterface $query = null): RepositoryQueryInterface;

public function createQuery(RepositoryQueryInterface $query = null): RepositoryQueryInterface;

public function getRepositoryQuery();

public function getOneById(int $id);

public function getOneBySlug(string $slug, bool $isOnline = true, $query = null);

public function getOneByDevAlias(string $devAlias, bool $isOnline = true, $query = null);

public function get($query = null);

public function getOnline($query = null);

public function getParent(bool $isOnline = true, $query = null);
}

+ 21
- 3
Repository/Ticket/TicketStore.php View File

@@ -3,9 +3,11 @@
namespace Lc\SovBundle\Repository\Ticket;

use App\Entity\Ticket\Ticket;
use Lc\CaracoleBundle\Repository\SectionStoreTrait;
use Lc\SovBundle\Model\Ticket\TicketInterface;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Repository\AbstractStore;
use Lc\SovBundle\Repository\RepositoryQueryInterface;

class TicketStore extends AbstractStore implements TicketStoreInterface
{
@@ -16,10 +18,26 @@ class TicketStore extends AbstractStore implements TicketStoreInterface
$this->query = $query;
}

public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
$query->orderBy('id');
return $query;
}

public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}

public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}

// getTicketsByUser
public function getByUser(UserInterface $user, $query = null): array
{
$query = $this->createQuery($query);
$query = $this->createDefaultQuery($query);
$query->filterByUser($user);
return $query->find();
}
@@ -27,7 +45,7 @@ class TicketStore extends AbstractStore implements TicketStoreInterface
// findAllOpen
public function getOpen(int $limit = 0, $query = null): array
{
$query = $this->createQuery($query);
$query = $this->createDefaultQuery($query);

$query
->filterByStatus(Ticket::TICKET_STATUS_OPEN)
@@ -40,7 +58,7 @@ class TicketStore extends AbstractStore implements TicketStoreInterface
// countAllOpen
public function countOpen($query = null): string
{
$query = $this->createQuery($query);
$query = $this->createDefaultQuery($query);

$query
->selectCount()

+ 0
- 24
Repository/TreeRepositoryQueryTrait.php View File

@@ -1,24 +0,0 @@
<?php

namespace Lc\SovBundle\Repository;

use Lc\SovBundle\Doctrine\EntityInterface;

trait TreeRepositoryQueryTrait
{
public function filterIsParent()
{
return $this->andWhere('.parent is NULL');
}

public function filterIsChildren()
{
return $this->andWhere('.parent is NOT NULL');
}

public function filterByParent(EntityInterface $parent)
{
return $this->andWhere('.parent = :parent')->setParameter('parent', $parent);
}

}

+ 17
- 0
Repository/User/GroupUserStore.php View File

@@ -3,6 +3,7 @@
namespace Lc\SovBundle\Repository\User;

use Lc\SovBundle\Repository\AbstractStore;
use Lc\SovBundle\Repository\RepositoryQueryInterface;

class GroupUserStore extends AbstractStore implements GroupUserStoreInterface
{
@@ -12,4 +13,20 @@ class GroupUserStore extends AbstractStore implements GroupUserStoreInterface
{
$this->query = $query;
}

public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
$query->orderBy('id');
return $query;
}

public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}

public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}
}

+ 17
- 0
Repository/User/UserStore.php View File

@@ -4,6 +4,7 @@ namespace Lc\SovBundle\Repository\User;

use App\Entity\Newsletter\Newsletter;
use Lc\SovBundle\Repository\AbstractStore;
use Lc\SovBundle\Repository\RepositoryQueryInterface;

class UserStore extends AbstractStore implements UserStoreInterface
{
@@ -14,6 +15,22 @@ class UserStore extends AbstractStore implements UserStoreInterface
$this->query = $query;
}

public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
$query->orderBy('id');
return $query;
}

public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}

public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{
return $query;
}

// findAllByNewsletter
public function getByNewsletter(Newsletter $newsletter, $query = null): array
{

Loading…
Cancel
Save