Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| TextbausteinRepository | |
0.00% |
0 / 48 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| findeTextbausteineFuerOrdner | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| countLastQb | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| setLimitToQueryBuilder | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getQb | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| addSuche | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Satag\AmicronEntityBundle\Repository; |
| 4 | |
| 5 | use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
| 6 | use Doctrine\ORM\Query; |
| 7 | use Doctrine\ORM\QueryBuilder; |
| 8 | use Doctrine\Persistence\ManagerRegistry; |
| 9 | use Satag\AmicronEntityBundle\Entity\Textbaustein; |
| 10 | |
| 11 | /** |
| 12 | * @extends ServiceEntityRepository<Textbaustein> |
| 13 | */ |
| 14 | class TextbausteinRepository extends ServiceEntityRepository |
| 15 | { |
| 16 | protected ?QueryBuilder $qbLastCount = null; |
| 17 | |
| 18 | public function __construct(ManagerRegistry $registry, string $entityClass = Textbaustein::class) |
| 19 | { |
| 20 | parent::__construct($registry, $entityClass); |
| 21 | } |
| 22 | |
| 23 | public function findeTextbausteineFuerOrdner(string $ordner, int $limit = 30, int $page = 1, ?string $suche = null): mixed |
| 24 | { |
| 25 | $qb = $this->getQb(); |
| 26 | $this->addSuche($qb, $suche ?? ''); |
| 27 | $qb->andWhere('t.privat = \'N\''); |
| 28 | if ($ordner !== '') { |
| 29 | $qb->andWhere('LOWER(t.ordner) = :ordner '); |
| 30 | $qb->setParameter(':ordner', strtolower($ordner), 'string'); |
| 31 | } |
| 32 | $this->setLimitToQueryBuilder($qb, $limit, $page); |
| 33 | |
| 34 | $query = $qb->orderBy('t.betreff', 'ASC') |
| 35 | ->getQuery(); |
| 36 | |
| 37 | $query->setHint(Query::HINT_READ_ONLY, true); |
| 38 | |
| 39 | return $query->getResult(); |
| 40 | } |
| 41 | |
| 42 | public function countLastQb(): ?int |
| 43 | { |
| 44 | if ($this->qbLastCount) { |
| 45 | $this->qbLastCount->select('count(t.lfdnr)'); |
| 46 | |
| 47 | return (int) $this->qbLastCount->getQuery() |
| 48 | ->getSingleScalarResult(); |
| 49 | } |
| 50 | |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | protected function setLimitToQueryBuilder(QueryBuilder $qb, int $limit, int $page): void |
| 55 | { |
| 56 | $this->qbLastCount = clone $qb; |
| 57 | if ($limit > 0) { |
| 58 | $qb->setFirstResult(($page - 1) * $limit) |
| 59 | ->setMaxResults($limit); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | protected function getQb(): QueryBuilder |
| 64 | { |
| 65 | $qb = $this->getEntityManager() |
| 66 | ->createQueryBuilder() |
| 67 | ->select('PARTIAL t.{lfdnr, ordner, betreff, textformatiert, textunformatiert, privat}') |
| 68 | ->from(Textbaustein::class, 't'); |
| 69 | |
| 70 | return $qb; |
| 71 | } |
| 72 | |
| 73 | protected function addSuche(QueryBuilder $qb, string $suche): void |
| 74 | { |
| 75 | $suche = trim((string) $suche); |
| 76 | if ($suche !== '') { |
| 77 | $qb->andWhere( |
| 78 | $qb->expr()->like( |
| 79 | 'LOWER(COALESCE(t.betreff,\'\'))', |
| 80 | '?1' |
| 81 | ) |
| 82 | ); |
| 83 | $qb->orWhere( |
| 84 | $qb->expr()->like( |
| 85 | 'LOWER(COALESCE(t.textformatiert,\'\'))', |
| 86 | '?1' |
| 87 | ) |
| 88 | ); |
| 89 | $qb->orWhere( |
| 90 | $qb->expr()->like( |
| 91 | 'LOWER(COALESCE(t.textunformatiert,\'\'))', |
| 92 | '?1' |
| 93 | ) |
| 94 | ); |
| 95 | |
| 96 | $suche = '%' . mb_strtolower(str_replace(' ', '%', $suche)) . '%'; |
| 97 | $qb->setParameter(1, $suche, 'string'); |
| 98 | } |
| 99 | } |
| 100 | } |