CommentRepository::findCommentById()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 2.024

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 9
cts 11
cp 0.8182
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
crap 2.024
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\CommentBundle\Entity;
13
14
use Doctrine\ORM\NoResultException;
15
use Sulu\Component\Persistence\Repository\ORM\EntityRepository;
16
17
/**
18
 * Repository for querying comments.
19
 */
20
class CommentRepository extends EntityRepository implements CommentRepositoryInterface
21
{
22
    /**
23
     * @inheritdoc}
24
     */
25 View Code Duplication
    public function findComments($type, $entityId, $page = 1, $pageSize = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
26
    {
27
        $query = $this->createQueryBuilder('c')
28
            ->join('c.thread', 't')
29
            ->leftJoin('c.creator', 'creator')
30
            ->leftJoin('c.changer', 'changer')
31
            ->where('t.type = :type AND t.entityId = :entityId')
32
            ->setParameter('type', $type)
33
            ->setParameter('entityId', $entityId)
34
            ->orderBy('c.created', 'DESC')
35
            ->getQuery();
36
37
        if ($pageSize) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pageSize of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
38
            $query->setMaxResults($pageSize);
39
            $query->setFirstResult(($page - 1) * $pageSize);
40
        }
41
42
        return $query->getResult();
43
    }
44
45
    /**
46
     * @inheritdoc}
47
     */
48 1 View Code Duplication
    public function findPublishedComments($type, $entityId, $page, $pageSize)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
49
    {
50 1
        $query = $this->createQueryBuilder('c')
51 1
            ->join('c.thread', 't')
52 1
            ->leftJoin('c.creator', 'creator')
53 1
            ->leftJoin('c.changer', 'changer')
54 1
            ->where('c.state = :state')
55 1
            ->andWhere('t.type = :type AND t.entityId = :entityId')
56 1
            ->setParameter('state', CommentInterface::STATE_PUBLISHED)
57 1
            ->setParameter('type', $type)
58 1
            ->setParameter('entityId', $entityId)
59 1
            ->orderBy('c.created', 'DESC')
60 1
            ->getQuery();
61
62 1
        if ($pageSize) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pageSize of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
63
            $query->setMaxResults($pageSize);
64
            $query->setFirstResult(($page - 1) * $pageSize);
65
        }
66
67 1
        return $query->getResult();
68
    }
69
70
    /**
71
     * @inheritdoc}
72
     */
73 2 View Code Duplication
    public function findCommentsByIds($ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
74
    {
75 2
        $query = $this->createQueryBuilder('c')
76 2
            ->join('c.thread', 't')
77 2
            ->leftJoin('c.creator', 'creator')
78 2
            ->leftJoin('c.changer', 'changer')
79 2
            ->where('c.id IN (:ids)')
80 2
            ->setParameter('ids', $ids)
81 2
            ->getQuery();
82
83 2
        return $query->getResult();
84
    }
85
86
    /**
87
     * @inheritdoc}
88
     */
89 4 View Code Duplication
    public function findCommentById($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
90
    {
91 4
        $query = $this->createQueryBuilder('c')
92 4
            ->join('c.thread', 't')
93 4
            ->leftJoin('c.creator', 'creator')
94 4
            ->leftJoin('c.changer', 'changer')
95 4
            ->where('c.id = :id')
96 4
            ->setParameter('id', $id)
97 4
            ->getQuery();
98
99
        try {
100 4
            return $query->getSingleResult();
101
        } catch (NoResultException $e) {
102
            return;
103
        }
104
    }
105
106
    /**
107
     * @inheritdoc}
108
     */
109 5
    public function persist(CommentInterface $comment)
110
    {
111 5
        $this->getEntityManager()->persist($comment);
112 5
    }
113
114
    /**
115
     * @inheritdoc}
116
     */
117 2
    public function delete(CommentInterface $comment)
118
    {
119 2
        $this->getEntityManager()->remove($comment);
120 2
    }
121
}
122