Completed
Push — 3.x-dev-kit ( 21be5a )
by
unknown
03:10
created

CommentManager::getPager()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 33
rs 8.439
cc 5
eloc 21
nc 12
nop 4
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\Common\Persistence\ManagerRegistry;
15
use Sonata\CoreBundle\Model\BaseEntityManager;
16
use Sonata\CoreBundle\Model\ManagerInterface;
17
use Sonata\DatagridBundle\Pager\Doctrine\Pager;
18
use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
19
use Sonata\NewsBundle\Model\CommentInterface;
20
use Sonata\NewsBundle\Model\CommentManagerInterface;
21
use Sonata\NewsBundle\Model\PostInterface;
22
23
class CommentManager extends BaseEntityManager implements CommentManagerInterface
24
{
25
    /**
26
     * @var ManagerInterface
27
     */
28
    protected $postManager;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param string           $class
34
     * @param ManagerRegistry  $registry
35
     * @param ManagerInterface $postManager
36
     */
37
    public function __construct($class, ManagerRegistry $registry, ManagerInterface $postManager)
38
    {
39
        parent::__construct($class, $registry);
40
41
        $this->postManager = $postManager;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function save($comment, $andFlush = true)
48
    {
49
        parent::save($comment, $andFlush);
50
51
        $this->updateCommentsCount($comment->getPost());
52
    }
53
54
    /**
55
     * Update the number of comment for a comment.
56
     *
57
     * @param null|\Sonata\NewsBundle\Model\PostInterface $post
58
     */
59
    public function updateCommentsCount(PostInterface $post = null)
60
    {
61
        $commentTableName = $this->getObjectManager()->getClassMetadata($this->getClass())->table['name'];
0 ignored issues
show
Bug introduced by
Accessing table on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
62
        $postTableName = $this->getObjectManager()->getClassMetadata($this->postManager->getClass())->table['name'];
0 ignored issues
show
Bug introduced by
Accessing table on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
63
64
        $this->getConnection()->beginTransaction();
65
        $this->getConnection()->query($this->getCommentsCountResetQuery($postTableName));
66
67
        $this->getConnection()->query($this->getCommentsCountQuery($postTableName, $commentTableName));
68
69
        $this->getConnection()->commit();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function delete($comment, $andFlush = true)
76
    {
77
        $post = $comment->getPost();
78
79
        parent::delete($comment, $andFlush);
80
81
        $this->updateCommentsCount($post);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getPager(array $criteria, $page, $limit = 10, array $sort = array())
88
    {
89
        if (!isset($criteria['mode'])) {
90
            $criteria['mode'] = 'public';
91
        }
92
93
        $parameters = array();
94
95
        $query = $this->getRepository()
96
            ->createQueryBuilder('c')
97
            ->orderby('c.createdAt', 'DESC');
98
99
        if ($criteria['mode'] == 'public') {
100
            $criteria['status'] = isset($criteria['status']) ? $criteria['status'] : CommentInterface::STATUS_VALID;
101
            $query->andWhere('c.status = :status');
102
            $parameters['status'] = $criteria['status'];
103
        }
104
105
        if (isset($criteria['postId'])) {
106
            $query->andWhere('c.post = :postId');
107
            $parameters['postId'] = $criteria['postId'];
108
        }
109
110
        $query->setParameters($parameters);
111
112
        $pager = new Pager();
113
        $pager->setMaxPerPage($limit);
114
        $pager->setQuery(new ProxyQuery($query));
115
        $pager->setPage($page);
116
        $pager->init();
117
118
        return $pager;
119
    }
120
121
    /**
122
     * @param string $postTableName
123
     *
124
     * @return string
125
     */
126
    private function getCommentsCountResetQuery($postTableName)
127
    {
128
        switch ($this->getConnection()->getDriver()->getDatabasePlatform()->getName()) {
129
            case 'postgresql':
130
                return sprintf('UPDATE %s SET comments_count = 0', $postTableName);
131
132
            default:
133
                return sprintf('UPDATE %s p SET p.comments_count = 0', $postTableName);
134
        }
135
    }
136
137
    /**
138
     * @param string $postTableName
139
     * @param string $commentTableName
140
     *
141
     * @return string
142
     */
143
    private function getCommentsCountQuery($postTableName, $commentTableName)
144
    {
145
        switch ($this->getConnection()->getDriver()->getDatabasePlatform()->getName()) {
146
            case 'postgresql':
147
                return sprintf(
148
            'UPDATE %s SET comments_count = count_comment.total
149
            FROM (SELECT c.post_id, count(*) AS total FROM %s AS c WHERE c.status = 1 GROUP BY c.post_id) count_comment
150
            WHERE %s.id = count_comment.post_id', $postTableName, $commentTableName, $postTableName);
151
152
            default:
153
                return sprintf(
154
            'UPDATE %s p, (SELECT c.post_id, count(*) as total FROM %s as c WHERE c.status = 1 GROUP BY c.post_id) as count_comment
155
            SET p.comments_count = count_comment.total
156
            WHERE p.id = count_comment.post_id', $postTableName, $commentTableName);
157
        }
158
    }
159
}
160