Passed
Push — master ( b34284...653853 )
by Angel Fernando Quiroz
12:36
created

PortfolioRepository::getIndexCourseItems()   F

Complexity

Conditions 11
Paths 576

Size

Total Lines 107
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 57
c 1
b 0
f 0
nc 576
nop 12
dl 0
loc 107
rs 3.9292

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Repository\Node;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\ExtraField;
11
use Chamilo\CoreBundle\Entity\ExtraFieldRelTag;
12
use Chamilo\CoreBundle\Entity\Portfolio;
13
use Chamilo\CoreBundle\Entity\Session;
14
use Chamilo\CoreBundle\Entity\User;
15
use Chamilo\CoreBundle\Repository\ResourceRepository;
16
use DateTime;
17
use Doctrine\ORM\Query\Expr\Join;
18
use Doctrine\Persistence\ManagerRegistry;
19
20
class PortfolioRepository extends ResourceRepository
21
{
22
    public function __construct(ManagerRegistry $registry)
23
    {
24
        parent::__construct($registry, Portfolio::class);
25
    }
26
27
    public function findItemsByUser(
28
        User $user,
29
        ?Course $course,
30
        ?Session $session,
31
        ?array $orderBy = null,
32
        array $visibility = []
33
    ): array {
34
        $criteria = [];
35
        $criteria['user'] = $user;
36
37
        if ($course) {
38
            $criteria['course'] = $course;
39
            $criteria['session'] = $session;
40
        }
41
42
        if ($visibility) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $visibility of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            $criteria['visibility'] = $visibility;
44
        }
45
46
        return $this->findBy($criteria, $orderBy);
47
    }
48
49
    public function findTemplates(User $creator, ?Course $course, ?Session $session)
50
    {
51
        $qb = $this->getResourcesByCourse($course, $session);
52
53
        $this->addCreatorQueryBuilder($creator, $qb);
54
55
        return $qb
56
            ->andWhere($qb->expr()->eq('resource.isTemplate', true))
57
            ->getQuery()
58
            ->getResult()
59
        ;
60
    }
61
62
    public function getIndexCourseItems(
63
        User $currentUser,
64
        User $owner,
65
        Course $course,
66
        ?Session $session = null,
67
        bool $showBaseContentInSession = false,
68
        bool $listByUser = false,
69
        ?DateTime $date = null,
70
        array $tags = [],
71
        ?string $searchText = null,
72
        array $searchCategories = [],
73
        array $searchNoInCategories = [],
74
        bool $advancedSharingEnabled = false
75
    ): array {
76
        $queryBuilder = $this->getResources();
77
        $this->addCourseQueryBuilder($course, $queryBuilder);
78
79
        if ($session) {
80
            if ($showBaseContentInSession) {
81
                $this->addSessionAndBaseContentQueryBuilder($session, $queryBuilder);
82
            } else {
83
                $this->addSessionOnlyQueryBuilder($session, $queryBuilder);
84
            }
85
        } else {
86
            $this->addSessionNullQueryBuilder($queryBuilder);
87
        }
88
89
        if ($date) {
90
            $queryBuilder
91
                ->andWhere('resource.creationDate >= :date')
92
                ->setParameter(':date', $date)
93
            ;
94
        }
95
96
        if ($tags) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tags of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
97
            $queryBuilder
98
                ->innerJoin(ExtraFieldRelTag::class, 'efrt', Join::WITH, 'efrt.itemId = resource.id')
99
                ->innerJoin(ExtraField::class, 'ef', Join::WITH, 'ef.id = efrt.fieldId')
100
                ->andWhere('ef.extraFieldType = :efType')
101
                ->andWhere('ef.variable = :variable')
102
                ->andWhere('efrt.tagId IN (:tags)')
103
            ;
104
105
            $queryBuilder->setParameter('efType', ExtraField::PORTFOLIO_TYPE);
106
            $queryBuilder->setParameter('variable', 'tags');
107
            $queryBuilder->setParameter('tags', $tags);
108
        }
109
110
        if (!empty($searchText)) {
111
            $queryBuilder->andWhere(
112
                $queryBuilder->expr()->orX(
113
                    $queryBuilder->expr()->like('resource.title', ':text'),
114
                    $queryBuilder->expr()->like('resource.content', ':text')
115
                )
116
            );
117
118
            $queryBuilder->setParameter('text', '%'.$searchText.'%');
119
        }
120
121
        if ($searchCategories) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $searchCategories of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
122
            $queryBuilder->andWhere(
123
                $queryBuilder->expr()->in('resource.category', $searchCategories)
124
            );
125
        }
126
127
        if ($searchNoInCategories) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $searchNoInCategories of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
128
            $queryBuilder->andWhere('resource.category NOT IN('.implode(',', $searchNoInCategories).')');
129
        }
130
131
        if ($listByUser) {
132
            $queryBuilder
133
                ->andWhere('resource.user = :user')
134
                ->setParameter('user', $owner)
135
            ;
136
        }
137
138
        if ($advancedSharingEnabled) {
139
            $queryBuilder->andWhere(
140
                $queryBuilder->expr()->orX(
141
                    $queryBuilder->expr()->eq('resource.visibility', Portfolio::VISIBILITY_VISIBLE),
142
                    $queryBuilder->expr()->eq('links.user', ':current_user')
143
                )
144
            );
145
        } else {
146
            $visibilityCriteria = [Portfolio::VISIBILITY_VISIBLE];
147
148
            if (api_is_allowed_to_edit()) {
149
                $visibilityCriteria[] = Portfolio::VISIBILITY_HIDDEN_EXCEPT_TEACHER;
150
            }
151
152
            $queryBuilder->andWhere(
153
                $queryBuilder->expr()->orX(
154
                    'node.creator = :current_user',
155
                    $queryBuilder->expr()->andX(
156
                        'node.creator != :current_user',
157
                        $queryBuilder->expr()->in('resource.visibility', $visibilityCriteria)
158
                    )
159
                )
160
            );
161
        }
162
163
        $queryBuilder
164
            ->setParameter('current_user', $currentUser->getId())
165
            ->orderBy('node.createdAt', 'DESC')
166
        ;
167
168
        return $queryBuilder->getQuery()->getResult();
169
    }
170
}
171