Passed
Pull Request — master (#6838)
by
unknown
08:39
created

hardDeleteShortcutsForCourse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 2
dl 0
loc 15
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\ResourceInterface;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Chamilo\CoreBundle\Entity\User;
13
use Chamilo\CoreBundle\Repository\ResourceRepository;
14
use Chamilo\CourseBundle\Entity\CShortcut;
15
use Doctrine\Persistence\ManagerRegistry;
16
17
final class CShortcutRepository extends ResourceRepository
18
{
19
    public function __construct(ManagerRegistry $registry)
20
    {
21
        parent::__construct($registry, CShortcut::class);
22
    }
23
24
    public function getShortcutFromResource(ResourceInterface $resource): ?CShortcut
25
    {
26
        $criteria = [
27
            'shortCutNode' => $resource->getResourceNode(),
28
        ];
29
30
        return $this->findOneBy($criteria);
31
    }
32
33
    public function addShortCut(ResourceInterface $resource, User $user, Course $course, ?Session $session = null): CShortcut
34
    {
35
        $shortcut = $this->getShortcutFromResource($resource);
36
37
        if (null === $shortcut) {
38
            $shortcut = (new CShortcut())
39
                ->setTitle($resource->getResourceName())
40
                ->setShortCutNode($resource->getResourceNode())
41
                ->setCreator($user)
42
                ->setParent($course)
43
                ->addCourseLink($course, $session)
44
            ;
45
46
            $this->create($shortcut);
47
        }
48
49
        return $shortcut;
50
    }
51
52
    public function hardDeleteShortcutsForCourse(ResourceInterface $resource, Course $course): int
53
    {
54
        $conn = $this->getEntityManager()->getConnection();
55
56
        $sql = <<<SQL
57
DELETE s
58
FROM c_shortcut s
59
INNER JOIN resource_node rn ON rn.id = s.resource_node_id
60
WHERE s.shortcut_node_id = :shortcutNodeId
61
  AND rn.parent_id       = :courseNodeId
62
SQL;
63
64
        return $conn->executeStatement($sql, [
65
            'shortcutNodeId' => $resource->getResourceNode()->getId(),
66
            'courseNodeId'   => $course->getResourceNode()->getId(),
67
        ]);
68
    }
69
70
    public function removeShortCut(ResourceInterface $resource): bool
71
    {
72
        $em = $this->getEntityManager();
73
        $shortcut = $this->getShortcutFromResource($resource);
74
        if (null !== $shortcut) {
75
            $em->remove($shortcut);
76
            // $em->flush();
77
78
            return true;
79
        }
80
81
        return false;
82
    }
83
}
84