Completed
Push — master ( e8c97b...6d1563 )
by Grégoire
14s queued 11s
created

src/Document/CommentManager.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NewsBundle\Document;
15
16
use Sonata\Doctrine\Document\BaseDocumentManager;
17
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\Pager;
18
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
19
use Sonata\NewsBundle\Model\CommentInterface;
20
use Sonata\NewsBundle\Model\CommentManagerInterface;
21
use Sonata\NewsBundle\Model\PostInterface;
22
23
class CommentManager extends BaseDocumentManager implements CommentManagerInterface
24
{
25
    /**
26
     * @param int $page
27
     * @param int $limit
28
     *
29
     * @return Pager
30
     */
31
    public function getPager(array $criteria, $page, $limit = 10, array $sort = [])
32
    {
33
        $qb = $this->getDocumentManager()->getRepository($this->class)
34
            ->createQueryBuilder()
35
            ->sort('createdAt', 'desc');
36
37
        $criteria['status'] = $criteria['status'] ?? CommentInterface::STATUS_VALID;
38
        $qb->field('status')->equals($criteria['status']);
39
40
        if (isset($criteria['postId'])) {
41
            $qb->field('post')->equals($criteria['postId']);
42
        }
43
44
        $pager = new Pager(500); // no limit
45
        $pager->setQuery(new ProxyQuery($qb));
46
        $pager->setPage($page);
47
        $pager->init();
48
49
        return $pager;
50
    }
51
52
    /**
53
     * Update the comments count.
54
     *
55
     * @param PostInterface $post
56
     */
57
    public function updateCommentsCount(PostInterface $post = null): void
58
    {
59
        $post->setCommentsCount($post->getCommentsCount() + 1);
60
        $this->getDocumentManager()->persist($post);
0 ignored issues
show
It seems like $post defined by parameter $post on line 57 can be null; however, Doctrine\Common\Persiste...bjectManager::persist() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
61
        $this->getDocumentManager()->flush();
62
    }
63
}
64