1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2017 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 2017 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Repository; |
18
|
|
|
|
19
|
|
|
use Doctrine\ORM\QueryBuilder; |
20
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ORM\ArticleRepository as ContentBundleArticleRepository; |
21
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleEvent; |
22
|
|
|
use SWP\Bundle\CoreBundle\Model\PackageInterface; |
23
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
24
|
|
|
|
25
|
|
|
class ArticleRepository extends ContentBundleArticleRepository implements ArticleRepositoryInterface |
26
|
|
|
{ |
27
|
|
View Code Duplication |
public function getByCriteria(Criteria $criteria, array $sorting): QueryBuilder |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$qb = parent::getByCriteria($criteria, $sorting) |
30
|
|
|
->leftJoin('a.articleStatistics', 'stats')->addSelect('stats') |
31
|
|
|
->leftJoin('a.externalArticle', 'ext')->addSelect('ext'); |
32
|
|
|
|
33
|
|
|
return $qb; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
public function getArticlesByCriteriaIds(Criteria $criteria): QueryBuilder |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$queryBuilder = parent::getArticlesByCriteriaIds($criteria) |
39
|
|
|
->leftJoin('a.articleStatistics', 'stats')->addSelect('stats') |
40
|
|
|
->leftJoin('a.externalArticle', 'ext')->addSelect('ext'); |
41
|
|
|
|
42
|
|
|
return $queryBuilder; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getArticleBySlugForPackage(string $slug, PackageInterface $package): QueryBuilder |
46
|
|
|
{ |
47
|
|
|
$queryBuilder = $this->createQueryBuilder('a'); |
48
|
|
|
$queryBuilder |
49
|
|
|
->where('a.slug = :slug') |
50
|
|
|
->setParameter('slug', $slug) |
51
|
|
|
->andWhere('a.package != :package') |
52
|
|
|
->setParameter('package', $package) |
53
|
|
|
; |
54
|
|
|
|
55
|
|
|
return $queryBuilder; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getArticlesByPackage(PackageInterface $package): QueryBuilder |
59
|
|
|
{ |
60
|
|
|
$queryBuilder = $this->createQueryBuilder('a'); |
61
|
|
|
$queryBuilder |
62
|
|
|
->andWhere('a.package = :package') |
63
|
|
|
->setParameter('package', $package) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
return $queryBuilder; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getArticleByPackageExtraData(string $key, string $value): QueryBuilder |
70
|
|
|
{ |
71
|
|
|
$queryBuilder = $this->createQueryBuilder('a'); |
72
|
|
|
$queryBuilder |
73
|
|
|
->leftJoin('a.package', 'p') |
74
|
|
|
->leftJoin('p.externalData', 'e') |
75
|
|
|
->andWhere('e.key = :key') |
76
|
|
|
->andWhere('e.value = :value') |
77
|
|
|
->setParameters(['key' => $key, 'value' => $value]) |
78
|
|
|
; |
79
|
|
|
|
80
|
|
|
return $queryBuilder; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getArticleByExtraData(string $key, string $value): QueryBuilder |
84
|
|
|
{ |
85
|
|
|
$criteria = new Criteria([ |
86
|
|
|
'maxResults' => 1, |
87
|
|
|
'extra' => [ |
88
|
|
|
$key => $value, |
89
|
|
|
], |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
return $this->getArticlesByCriteria($criteria); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function applySorting(QueryBuilder $queryBuilder, array $sorting, string $alias, Criteria $criteria = null) |
96
|
|
|
{ |
97
|
|
|
$properties = \array_merge($this->getClassMetadata()->getFieldNames(), $this->getClassMetadata()->getAssociationNames()); |
98
|
|
|
foreach ($sorting as $property => $order) { |
99
|
|
|
if ('pageViews' === $property && !empty($order)) { |
100
|
|
|
if ($criteria instanceof Criteria && null !== $dateRange = $criteria->get('dateRange', null)) { |
101
|
|
|
$start = new \DateTime(); |
102
|
|
|
$start->setTimestamp(strtotime($dateRange[0])); |
103
|
|
|
$start->setTime(23, 59, 59); |
104
|
|
|
$end = new \DateTime(); |
105
|
|
|
$end->setTimestamp(strtotime($dateRange[1])); |
106
|
|
|
$end->setTime(0, 0, 0); |
107
|
|
|
|
108
|
|
|
$articleEventsQuery = $this->_em->createQueryBuilder() |
109
|
|
|
->from(ArticleEvent::class, 'ae') |
110
|
|
|
->select('COUNT(ae.id)') |
111
|
|
|
->where('ae.createdAt <= :start') |
112
|
|
|
->andWhere('ae.createdAt >= :end') |
113
|
|
|
->andWhere('ae.articleStatistics = stats.id'); |
114
|
|
|
|
115
|
|
|
$queryBuilder |
116
|
|
|
->addSelect(sprintf('(%s) as HIDDEN events_count', $articleEventsQuery)) |
117
|
|
|
->setParameter('start', $start) |
118
|
|
|
->setParameter('end', $end); |
119
|
|
|
$queryBuilder->addOrderBy('events_count', $sorting['pageViews']); |
120
|
|
|
} else { |
121
|
|
|
$queryBuilder->addOrderBy($this->getPropertyName('pageViewsNumber', 'stats'), $sorting['pageViews']); |
122
|
|
|
} |
123
|
|
|
unset($sorting['pageViews']); |
124
|
|
|
|
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!\in_array($property, $properties)) { |
129
|
|
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (!empty($order)) { |
133
|
|
|
$queryBuilder->addOrderBy($this->getPropertyName($property, $alias), $order); |
134
|
|
|
unset($sorting[$property]); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.