Completed
Push — master ( 8f0a09...1a8186 )
by Julito
24:55
created

AbstractResource   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 330
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 109
c 0
b 0
f 0
dl 0
loc 330
rs 9.0399
wmc 42

25 Methods

Rating   Name   Duplication   Size   Complexity  
A getParentResourceNode() 0 3 1
A addGroupLink() 0 24 3
A setResourceNode() 0 5 1
A getResourceNode() 0 3 1
A setResourceLinkList() 0 5 1
A addLink() 0 5 1
A setParent() 0 5 1
A addUserLink() 0 14 1
A hasResourceNode() 0 3 1
A hasParentResourceNode() 0 3 1
A isVisible() 0 8 2
A getLinkVisibility() 0 3 1
B addCourseLink() 0 37 6
A getCourseSessionResourceLink() 0 3 1
A addResourceToUserList() 0 13 3
A getUploadFile() 0 3 1
A getParent() 0 3 1
A hasUploadFile() 0 3 1
A setUploadFile() 0 5 1
A addResourceLink() 0 5 1
A getLinks() 0 3 1
A getFirstResourceLinkFromCourseSession() 0 24 5
A getFirstResourceLink() 0 12 4
A getResourceLinkListFromEntity() 0 3 1
A setParentResourceNode() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like AbstractResource often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AbstractResource, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Entity;
6
7
use ApiPlatform\Core\Annotation\ApiProperty;
8
use ApiPlatform\Core\Annotation\ApiSubresource;
9
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
10
use Chamilo\CourseBundle\Entity\CGroup;
11
use Doctrine\Common\Collections\Criteria;
12
use Doctrine\ORM\Mapping as ORM;
13
use Symfony\Component\Serializer\Annotation\Groups;
14
use Symfony\Component\Validator\Constraints as Assert;
15
16
/**
17
 * @ORM\MappedSuperclass
18
 * @ORM\HasLifecycleCallbacks
19
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\ResourceListener"})
20
 */
21
abstract class AbstractResource
22
{
23
    /**
24
     * @var string|null
25
     *
26
     * @ApiProperty(iri="http://schema.org/contentUrl")
27
     * @Groups({"resource_file:read", "resource_node:read", "document:read", "media_object_read"})
28
     */
29
    public $contentUrl;
30
31
    /**
32
     * @var string|null
33
     *
34
     * @Groups({"resource_file:read", "resource_node:read", "document:read", "document:write", "media_object_read"})
35
     */
36
    public $contentFile;
37
38
    /**
39
     * @Assert\Valid()
40
     * @ApiSubresource()
41
     * @Groups({"resource_node:read", "resource_node:write", "document:write" })
42
     * @ORM\OneToOne(
43
     *     targetEntity="Chamilo\CoreBundle\Entity\ResourceNode",
44
     *     cascade={"persist", "remove"},
45
     *     orphanRemoval=true
46
     * )
47
     * @ORM\JoinColumn(name="resource_node_id", referencedColumnName="id", onDelete="CASCADE")
48
     */
49
    public $resourceNode;
50
51
    /**
52
     * @Groups({"resource_node:read", "resource_node:write", "document:read", "document:write"})
53
     */
54
    public $parentResourceNode;
55
56
    /**
57
     * @ApiProperty(iri="http://schema.org/image")
58
     */
59
    public $uploadFile;
60
61
    /**
62
     * @Groups({"resource_node:read", "document:read"})
63
     */
64
    public $resourceLinkList;
65
66
    /** @var ResourceLink[] */
67
    public $linkEntityList;
68
69
    /** @var AbstractResource */
70
    public $parentResource;
71
72
    abstract public function getResourceName(): string;
73
74
    abstract public function setResourceName(string $name);
75
76
    public function addLink(ResourceLink $link)
77
    {
78
        $this->linkEntityList[] = $link;
79
80
        return $this;
81
    }
82
83
    public function addCourseLink(Course $course, Session $session = null, CGroup $group = null, int $visibility = ResourceLink::VISIBILITY_PUBLISHED)
84
    {
85
        if (null === $this->getParent()) {
86
            throw new \Exception('addCourseLink requires to set the parent.');
87
        }
88
89
        $resourceLink = new ResourceLink();
90
        $resourceLink
91
            ->setVisibility($visibility)
92
            ->setCourse($course)
93
            ->setSession($session)
94
            ->setGroup($group)
95
        ;
96
        $this->addLink($resourceLink);
97
98
        $rights = [];
99
        switch ($visibility) {
100
            case ResourceLink::VISIBILITY_PENDING:
101
            case ResourceLink::VISIBILITY_DRAFT:
102
                $editorMask = ResourceNodeVoter::getEditorMask();
103
                $resourceRight = new ResourceRight();
104
                $resourceRight
105
                    ->setMask($editorMask)
106
                    ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
107
                ;
108
                $rights[] = $resourceRight;
109
110
                break;
111
        }
112
113
        if (!empty($rights)) {
114
            foreach ($rights as $right) {
115
                $resourceLink->addResourceRight($right);
116
            }
117
        }
118
119
        return $this;
120
    }
121
122
    public function addGroupLink(Course $course, Session $session = null, CGroup $group = null)
123
    {
124
        $resourceNode = $this->getResourceNode();
125
        $exists = $resourceNode->getResourceLinks()->exists(
126
            function ($key, $element) use ($group) {
127
                if ($element->getGroup()) {
128
                    return $group->getIid() == $element->getGroup()->getIid();
0 ignored issues
show
Bug introduced by
The method getIid() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
                    return $group->/** @scrutinizer ignore-call */ getIid() == $element->getGroup()->getIid();

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...
129
                }
130
            }
131
        );
132
133
        if (false === $exists) {
134
            $resourceLink = new ResourceLink();
135
            $resourceLink
136
                ->setResourceNode($resourceNode)
137
                ->setCourse($course)
138
                ->setSession($session)
139
                ->setGroup($group)
140
                ->setVisibility(ResourceLink::VISIBILITY_PUBLISHED)
141
            ;
142
            $this->addLink($resourceLink);
143
        }
144
145
        return $this;
146
    }
147
148
    public function addUserLink(User $user, Course $course = null, Session $session = null, CGroup $group = null)
149
    {
150
        $resourceLink = new ResourceLink();
151
        $resourceLink
152
            ->setVisibility(ResourceLink::VISIBILITY_PUBLISHED)
153
            ->setUser($user)
154
            ->setCourse($course)
155
            ->setSession($session)
156
            ->setGroup($group)
157
        ;
158
159
        $this->addLink($resourceLink);
160
161
        return $this;
162
    }
163
164
    public function setParent(AbstractResource $parent)
165
    {
166
        $this->parentResource = $parent;
167
168
        return $this;
169
    }
170
171
    public function getParent()
172
    {
173
        return $this->parentResource;
174
    }
175
176
    /**
177
     * @param array $userList User id list
178
     */
179
    public function addResourceToUserList(
180
        array $userList,
181
        Course $course = null,
182
        Session $session = null,
183
        CGroup $group = null
184
    ) {
185
        if (!empty($userList)) {
186
            foreach ($userList as $user) {
187
                $this->addUserLink($user, $course, $session, $group);
188
            }
189
        }
190
191
        return $this;
192
    }
193
194
    public function getLinks()
195
    {
196
        return $this->linkEntityList;
197
    }
198
199
    public function addResourceLink($link)
200
    {
201
        $this->resourceLinkList[] = $link;
202
203
        return $this;
204
    }
205
206
    public function setResourceLinkList($links)
207
    {
208
        $this->resourceLinkList = $links;
209
210
        return $this;
211
    }
212
213
    public function getResourceLinkListFromEntity()
214
    {
215
        return $this->resourceLinkList;
216
    }
217
218
    /*public function getResourceLinkList(): array
219
    {
220
        $resourceNode = $this->getResourceNode();
221
        $links = $resourceNode->getResourceLinks();
222
        $resourceLinkList = [];
223
224
        foreach ($links as $link) {
225
            $resourceLinkList[] = [
226
                'id' => $link->getId(),
227
                'session' => $link->getSession(),
228
                'course' => $link->getCourse(),
229
                'visibility' => $link->getVisibility(),
230
                'visibilityName' => $link->getVisibilityName(),
231
                'group' => $link->getGroup(),
232
                'userGroup' => $link->getUserGroup(),
233
            ];
234
        }
235
236
        return $resourceLinkList;
237
    }*/
238
239
    public function hasParentResourceNode(): bool
240
    {
241
        return null !== $this->parentResourceNode;
242
    }
243
244
    public function setParentResourceNode($resourceNode): self
245
    {
246
        $this->parentResourceNode = $resourceNode;
247
248
        return $this;
249
    }
250
251
    public function getParentResourceNode()
252
    {
253
        return $this->parentResourceNode;
254
    }
255
256
    public function hasUploadFile(): bool
257
    {
258
        return null !== $this->uploadFile;
259
    }
260
261
    public function getUploadFile()
262
    {
263
        return $this->uploadFile;
264
    }
265
266
    public function setUploadFile($file): self
267
    {
268
        $this->uploadFile = $file;
269
270
        return $this;
271
    }
272
273
    public function setResourceNode(ResourceNode $resourceNode): self
274
    {
275
        $this->resourceNode = $resourceNode;
276
277
        return $this;
278
    }
279
280
    public function hasResourceNode(): bool
281
    {
282
        return $this->resourceNode instanceof ResourceNode;
283
    }
284
285
    public function getResourceNode(): ResourceNode
286
    {
287
        return $this->resourceNode;
288
    }
289
290
    public function getCourseSessionResourceLink(Course $course, Session $session = null): ?ResourceLink
291
    {
292
        return $this->getFirstResourceLinkFromCourseSession($course, $session);
293
    }
294
295
    public function getFirstResourceLink(): ?ResourceLink
296
    {
297
        $resourceNode = $this->getResourceNode();
298
299
        if ($resourceNode && $resourceNode->getResourceLinks()->count()) {
300
            $result = $resourceNode->getResourceLinks()->first();
301
            if ($result) {
302
                return $result;
303
            }
304
        }
305
306
        return null;
307
    }
308
309
    /**
310
     * See ResourceLink to see the visibility constants. Example: ResourceLink::VISIBILITY_DELETED.
311
     */
312
    public function getLinkVisibility(Course $course, Session $session = null): ?ResourceLink
313
    {
314
        return $this->getCourseSessionResourceLink($course, $session)->getVisibility();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getCourseS...ssion)->getVisibility() returns the type integer which is incompatible with the type-hinted return Chamilo\CoreBundle\Entity\ResourceLink|null.
Loading history...
315
    }
316
317
    public function isVisible(Course $course, Session $session = null): bool
318
    {
319
        $link = $this->getCourseSessionResourceLink($course, $session);
320
        if (null === $link) {
321
            return false;
322
        }
323
324
        return ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility();
325
    }
326
327
    public function getFirstResourceLinkFromCourseSession(Course $course, Session $session = null): ?ResourceLink
328
    {
329
        $criteria = Criteria::create();
330
        $criteria
331
            ->where(Criteria::expr()->eq('course', $course))
332
            ->andWhere(
333
                Criteria::expr()->eq('session', $session)
334
            );
335
        $resourceNode = $this->getResourceNode();
336
337
        $result = null;
338
        if ($resourceNode && $resourceNode->getResourceLinks()->count() > 0) {
339
            //var_dump($resourceNode->getResourceLinks()->count());
340
            foreach ($resourceNode->getResourceLinks() as $link) {
341
                //var_dump(get_class($link));
342
            }
343
            $result = $resourceNode->getResourceLinks()->matching($criteria)->first();
344
            //var_dump($result);
345
            if ($result) {
346
                return $result;
347
            }
348
        }
349
350
        return null;
351
    }
352
}
353