Completed
Push — master ( 619948...d55d2c )
by
unknown
02:29
created

PostManager::findOneByPermalink()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 42
rs 8.439
cc 5
eloc 23
nc 16
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NewsBundle\Entity;
13
14
use Doctrine\ORM\Query\Expr\Join;
15
use Sonata\ClassificationBundle\Model\CollectionInterface;
16
use Sonata\CoreBundle\Model\BaseEntityManager;
17
use Sonata\DatagridBundle\Pager\Doctrine\Pager;
18
use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
19
use Sonata\NewsBundle\Model\BlogInterface;
20
use Sonata\NewsBundle\Model\PostManagerInterface;
21
22
class PostManager extends BaseEntityManager implements PostManagerInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function findOneByPermalink($permalink, BlogInterface $blog)
28
    {
29
        $query = $this->getRepository()->createQueryBuilder('p');
30
31
        $urlParameters = $blog->getPermalinkGenerator()->getParameters($permalink);
32
33
        $parameters = array();
34
35
        if (isset($urlParameters['year'], $urlParameters['month'], $urlParameters['day'])) {
36
            $dateQueryParts = $this->getPublicationDateQueryParts(
37
                sprintf('%d-%d-%d', $urlParameters['year'], $urlParameters['month'], $urlParameters['day']),
38
                'day'
39
            );
40
41
            $parameters = $dateQueryParts['params'];
42
43
            $query->andWhere($dateQueryParts['query']);
44
        }
45
46
        if (isset($urlParameters['slug'])) {
47
            $query->andWhere('p.slug = :slug');
48
            $parameters['slug'] = $urlParameters['slug'];
49
        }
50
51
        if (isset($urlParameters['collection'])) {
52
            $collectionQueryParts = $this->getPublicationCollectionQueryParts($urlParameters['collection']);
53
54
            $parameters = array_merge($parameters, $collectionQueryParts['params']);
55
56
            $query
57
                ->leftJoin('p.collection', 'c')
58
                ->andWhere($collectionQueryParts['query']);
59
        }
60
61
        if (count($parameters) == 0) {
62
            return;
63
        }
64
65
        $query->setParameters($parameters);
66
67
        return $query->getQuery()->getSingleResult();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getPager(array $criteria, $page, $limit = 10, array $sort = array())
74
    {
75
        if (!isset($criteria['mode'])) {
76
            $criteria['mode'] = 'public';
77
        }
78
79
        $parameters = array();
80
        $query = $this->getRepository()
81
            ->createQueryBuilder('p')
82
            ->select('p, t')
83
            ->orderBy('p.publicationDateStart', 'DESC');
84
85
        if ($criteria['mode'] == 'admin') {
86
            $query
87
                ->leftJoin('p.tags', 't')
88
                ->leftJoin('p.author', 'a')
89
            ;
90
        } else {
91
            $query
92
                ->leftJoin('p.tags', 't', Join::WITH, 't.enabled = true')
93
                ->leftJoin('p.author', 'a', Join::WITH, 'a.enabled = true')
94
            ;
95
        }
96
97
        if (!isset($criteria['enabled']) && $criteria['mode'] == 'public') {
98
            $criteria['enabled'] = true;
99
        }
100
        if (isset($criteria['enabled'])) {
101
            $query->andWhere('p.enabled = :enabled');
102
            $parameters['enabled'] = $criteria['enabled'];
103
        }
104
105
        if (isset($criteria['date'], $criteria['date']['query'], $criteria['date']['params'])) {
106
            $query->andWhere($criteria['date']['query']);
107
            $parameters = array_merge($parameters, $criteria['date']['params']);
108
        }
109
110
        if (isset($criteria['tag'])) {
111
            $query
112
                ->leftJoin('p.tags', 't2')
113
                ->andWhere('t2.slug LIKE :tag');
114
            $parameters['tag'] = (string) $criteria['tag'];
115
        }
116
117
        if (isset($criteria['author'])) {
118
            if (!is_array($criteria['author']) && stristr($criteria['author'], 'NULL')) {
119
                $query->andWhere('p.author IS '.$criteria['author']);
120
            } else {
121
                $query->andWhere(sprintf('p.author IN (%s)', implode((array) $criteria['author'], ',')));
122
            }
123
        }
124
125
        if (isset($criteria['collection']) && $criteria['collection'] instanceof CollectionInterface) {
126
            $query->andWhere('p.collection = :collectionid');
127
            $parameters['collectionid'] = $criteria['collection']->getId();
0 ignored issues
show
Bug introduced by
The method getId() does not seem to exist on object<Sonata\Classifica...el\CollectionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
        }
129
130
        $query->setParameters($parameters);
131
132
        $pager = new Pager();
133
        $pager->setMaxPerPage($limit);
134
        $pager->setQuery(new ProxyQuery($query));
135
        $pager->setPage($page);
136
        $pager->init();
137
138
        return $pager;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getPublicationDateQueryParts($date, $step, $alias = 'p')
145
    {
146
        return array(
147
            'query' => sprintf('%s.publicationDateStart >= :startDate AND %s.publicationDateStart < :endDate', $alias, $alias),
148
            'params' => array(
149
                'startDate' => new \DateTime($date),
150
                'endDate' => new \DateTime($date.'+1 '.$step),
151
            ),
152
        );
153
    }
154
155
    /**
156
     * @param string $collection
157
     *
158
     * @return array
159
     */
160
    protected function getPublicationCollectionQueryParts($collection)
161
    {
162
        $queryParts = array('query' => '', 'params' => array());
163
164
        if (null === $collection) {
165
            $queryParts['query'] = 'p.collection IS NULL';
166
        } else {
167
            $queryParts['query'] = 'c.slug = :collection';
168
            $queryParts['params'] = array('collection' => $collection);
169
        }
170
171
        return $queryParts;
172
    }
173
}
174