|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2018 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2018 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Doctrine\ORM; |
|
18
|
|
|
|
|
19
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
20
|
|
|
use Knp\Component\Pager\Pagination\PaginationInterface; |
|
21
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\RelatedArticleRepositoryInterface; |
|
22
|
|
|
use SWP\Bundle\StorageBundle\Doctrine\ORM\EntityRepository; |
|
23
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
|
24
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
|
25
|
|
|
|
|
26
|
|
|
class RelatedArticleRepository extends EntityRepository implements RelatedArticleRepositoryInterface |
|
27
|
|
|
{ |
|
28
|
|
View Code Duplication |
public function getByCriteria(Criteria $criteria, array $sorting): array |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$queryBuilder = $this->createQueryBuilder('ra'); |
|
31
|
|
|
|
|
32
|
|
|
$this->applyCustomCriteria($queryBuilder, $criteria); |
|
33
|
|
|
$this->applyCriteria($queryBuilder, $criteria, 'ra'); |
|
34
|
|
|
$this->applySorting($queryBuilder, $sorting, 'ra', $criteria); |
|
35
|
|
|
$this->applyLimiting($queryBuilder, $criteria); |
|
36
|
|
|
|
|
37
|
|
|
return $queryBuilder |
|
38
|
|
|
->getQuery() |
|
39
|
|
|
->getResult() |
|
40
|
|
|
; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
View Code Duplication |
public function countByCriteria(Criteria $criteria): int |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$queryBuilder = $this->createQueryBuilder('ra') |
|
46
|
|
|
->select('COUNT(ra.id)'); |
|
47
|
|
|
|
|
48
|
|
|
$this->applyCustomCriteria($queryBuilder, $criteria); |
|
49
|
|
|
$this->applyCriteria($queryBuilder, $criteria, 'ra'); |
|
50
|
|
|
|
|
51
|
|
|
return (int) $queryBuilder->getQuery()->getSingleScalarResult(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
View Code Duplication |
public function getPaginatedByCriteria(Criteria $criteria, array $sorting = [], PaginationData $paginationData = null): PaginationInterface |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
$queryBuilder = $this->getQueryByCriteria($criteria, $sorting, 'ra'); |
|
57
|
|
|
$this->applyCustomCriteria($queryBuilder, $criteria); |
|
58
|
|
|
|
|
59
|
|
|
if (null === $paginationData) { |
|
60
|
|
|
$paginationData = new PaginationData(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->getPaginator($queryBuilder, $paginationData); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
View Code Duplication |
private function applyCustomCriteria(QueryBuilder $queryBuilder, Criteria $criteria): void |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
if ($criteria->has('article')) { |
|
69
|
|
|
$article = $criteria->get('article'); |
|
70
|
|
|
$queryBuilder |
|
71
|
|
|
->where('ra.relatesTo = :article') |
|
72
|
|
|
->setParameter('article', $article); |
|
73
|
|
|
|
|
74
|
|
|
$criteria->remove('article'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.