Completed
Push — master ( bb050e...36e41a )
by Paweł
11:10
created

ArticleRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getByCriteria() 0 7 1
A getArticlesByCriteriaIds() 0 8 1
A getArticleBySlugForPackage() 0 12 1
A applySorting() 0 31 5
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
/**
26
 * Class ArticleRepository.
27
 */
28
class ArticleRepository extends ContentBundleArticleRepository implements ArticleRepositoryInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getByCriteria(Criteria $criteria, array $sorting): QueryBuilder
34
    {
35
        $qb = parent::getByCriteria($criteria, $sorting);
36
        $qb->leftJoin('a.articleStatistics', 'stats')->addSelect('stats');
37
38
        return $qb;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getArticlesByCriteriaIds(Criteria $criteria): QueryBuilder
45
    {
46
        $queryBuilder = parent::getArticlesByCriteriaIds($criteria)
47
            ->addSelect('stats')
48
            ->leftJoin('a.articleStatistics', 'stats');
49
50
        return $queryBuilder;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getArticleBySlugForPackage(string $slug, PackageInterface $package): QueryBuilder
57
    {
58
        $queryBuilder = $this->createQueryBuilder('a');
59
        $queryBuilder
60
            ->where('a.slug = :slug')
61
                ->setParameter('slug', $slug)
62
            ->andWhere('a.package != :package')
63
                ->setParameter('package', $package)
64
        ;
65
66
        return $queryBuilder;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function applySorting(QueryBuilder $queryBuilder, array $sorting, string $alias, Criteria $criteria = null)
73
    {
74
        if (isset($sorting['pageViews']) && !empty($sorting['pageViews'])) {
75
            if ($criteria instanceof Criteria && null !== $dateRange = $criteria->get('dateRange', null)) {
76
                $start = new \DateTime();
77
                $start->setTimestamp(strtotime($dateRange[0]));
78
                $start->setTime(23, 59, 59);
79
                $end = new \DateTime();
80
                $end->setTimestamp(strtotime($dateRange[1]));
81
                $end->setTime(0, 0, 0);
82
83
                $articleEventsQuery = $this->_em->createQueryBuilder()
84
                    ->from(ArticleEvent::class, 'ae')
85
                    ->select('COUNT(ae.id)')
86
                    ->where('ae.createdAt <= :start')
87
                    ->andWhere('ae.createdAt >= :end')
88
                    ->andWhere('ae.articleStatistics = stats.id');
89
90
                $queryBuilder
91
                    ->addSelect(sprintf('(%s) as HIDDEN events_count', $articleEventsQuery))
92
                    ->setParameter('start', $start)
93
                    ->setParameter('end', $end);
94
                $queryBuilder->addOrderBy('events_count', $sorting['pageViews']);
95
            } else {
96
                $queryBuilder->addOrderBy($this->getPropertyName('pageViewsNumber', 'stats'), $sorting['pageViews']);
97
            }
98
            unset($sorting['pageViews']);
99
        }
100
101
        return parent::applySorting($queryBuilder, $sorting, $alias);
102
    }
103
}
104