Passed
Push — master ( 76abd2...ab5d59 )
by Julito
10:01
created

SequenceRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Repository;
5
6
use Chamilo\CoreBundle\Entity\Sequence;
7
use Chamilo\CoreBundle\Entity\SequenceResource;
8
use Fhaculty\Graph\Set\Vertices;
9
use Fhaculty\Graph\Vertex;
10
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
11
use Doctrine\Common\Persistence\ManagerRegistry;
12
13
/**
14
 * Class SequenceRepository
15
 * The functions inside this class should return an instance of QueryBuilder.
16
 *
17
 * @package Chamilo\CoreBundle\Repository
18
 */
19
class SequenceRepository extends ServiceEntityRepository
20
{
21
    /**
22
     * SequenceRepository constructor.
23
     *
24
     * @param ManagerRegistry $registry
25
     */
26
    public function __construct(ManagerRegistry $registry)
27
    {
28
        parent::__construct($registry, Sequence::class);
29
    }
30
31
    /**
32
     * Find the SequenceResource based in the resourceId and type.
33
     *
34
     * @param int $resourceId
35
     * @param int $type
36
     *
37
     * @return SequenceResource
38
     */
39
    public function findRequirementForResource($resourceId, $type)
40
    {
41
        return $this->findOneBy(['resourceId' => $resourceId, 'type' => $type]);
42
    }
43
44
    /**
45
     * @todo implement for all types only work for sessions
46
     *
47
     * @param int $resourceId
48
     * @param int $type
49
     *
50
     * @return array
51
     */
52
    public function getRequirementAndDependencies($resourceId, $type)
53
    {
54
        $sequence = $this->findRequirementForResource($resourceId, $type);
55
        $result = ['requirements' => '', 'dependencies' => []];
56
        if ($sequence && $sequence->hasGraph()) {
57
            $graph = $sequence->getSequence()->getUnSerializeGraph();
58
            $vertex = $graph->getVertex($resourceId);
59
            $from = $vertex->getVerticesEdgeFrom();
60
61
            foreach ($from as $subVertex) {
62
                $vertexId = $subVertex->getId();
63
                $sessionInfo = api_get_session_info($vertexId);
64
                $sessionInfo['admin_link'] = '<a href="'.\SessionManager::getAdminPath($vertexId).'">'.$sessionInfo['name'].'</a>';
0 ignored issues
show
Bug introduced by
Are you sure SessionManager::getAdminPath($vertexId) of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

64
                $sessionInfo['admin_link'] = '<a href="'./** @scrutinizer ignore-type */ \SessionManager::getAdminPath($vertexId).'">'.$sessionInfo['name'].'</a>';
Loading history...
65
                $result['requirements'][] = $sessionInfo;
66
            }
67
68
            $to = $vertex->getVerticesEdgeTo();
69
            foreach ($to as $subVertex) {
70
                $vertexId = $subVertex->getId();
71
                $sessionInfo = api_get_session_info($vertexId);
72
                $sessionInfo['admin_link'] = '<a href="'.\SessionManager::getAdminPath($vertexId).'">'.$sessionInfo['name'].'</a>';
73
                $result['dependencies'][] = $sessionInfo;
74
            }
75
        }
76
77
        return $result;
78
    }
79
80
    /**
81
     * Deletes a node and check in all the dependencies if the node exists in
82
     * order to deleted.
83
     *
84
     * @param int $resourceId
85
     * @param int $type
86
     */
87
    public function deleteResource($resourceId, $type)
88
    {
89
        $sequence = $this->findRequirementForResource($resourceId, $type);
90
        if ($sequence && $sequence->hasGraph()) {
91
            $em = $this->getEntityManager();
92
            $graph = $sequence->getSequence()->getUnSerializeGraph();
93
            $mainVertex = $graph->getVertex($resourceId);
94
            $vertices = $graph->getVertices();
95
96
            /** @var Vertex $vertex */
97
            foreach ($vertices as $vertex) {
98
                $subResourceId = $vertex->getId();
99
                $subSequence = $this->findRequirementForResource($subResourceId, $type);
100
                if ($sequence && $subSequence->hasGraph()) {
101
                    $graph = $subSequence->getSequence()->getUnSerializeGraph();
102
                    $subMainVertex = $graph->getVertex($resourceId);
103
                    $subMainVertex->destroy();
104
                    $subSequence->getSequence()->setGraphAndSerialize($graph);
105
                    $em->persist($subSequence);
106
                }
107
            }
108
109
            $mainVertex->destroy();
110
            $em->remove($sequence);
111
            $em->flush();
112
        }
113
    }
114
115
    /**
116
     * Get the requirements for a resource only.
117
     *
118
     * @param int $resourceId The resource ID
119
     * @param int $type       The type of sequence resource
120
     *
121
     * @return array
122
     */
123
    public function getRequirements($resourceId, $type)
124
    {
125
        $sequencesResource = $this->findBy([
126
            'resourceId' => $resourceId,
127
            'type' => $type,
128
        ]);
129
130
        $result = [];
131
        /** @var SequenceResource $sequenceResource */
132
        foreach ($sequencesResource as $sequenceResource) {
133
            if (!$sequenceResource->hasGraph()) {
134
                continue;
135
            }
136
137
            $sequence = $sequenceResource->getSequence();
138
            $graph = $sequence->getUnSerializeGraph();
139
            $vertex = $graph->getVertex($resourceId);
140
            $from = $vertex->getVerticesEdgeFrom();
141
142
            $sequenceInfo = [
143
                'name' => $sequence->getName(),
144
                'requirements' => [],
145
            ];
146
147
            foreach ($from as $subVertex) {
148
                $vertexId = $subVertex->getId();
149
150
                switch ($type) {
151
                    case SequenceResource::SESSION_TYPE:
152
                        $session = $this->getEntityManager()->getReference(
153
                            'ChamiloCoreBundle:Session',
154
                            $vertexId
155
                        );
156
157
                        if (empty($session)) {
158
                            break;
159
                        }
160
161
                        $sequenceInfo['requirements'][$vertexId] = $session;
162
                        break;
163
                }
164
            }
165
166
            $result[$sequence->getId()] = $sequenceInfo;
167
        }
168
169
        return $result;
170
    }
171
172
    /**
173
     * Get the requirements and dependencies within a sequence for a resource.
174
     *
175
     * @param int $resourceId The resource ID
176
     * @param int $type       The type of sequence resource
177
     *
178
     * @return array
179
     */
180
    public function getRequirementsAndDependenciesWithinSequences($resourceId, $type)
181
    {
182
        $sequencesResource = $this->findBy([
183
            'resourceId' => $resourceId,
184
            'type' => $type,
185
        ]);
186
187
        $result = [];
188
189
        /** @var SequenceResource $sequenceResource */
190
        foreach ($sequencesResource as $sequenceResource) {
191
            if (!$sequenceResource->hasGraph()) {
192
                continue;
193
            }
194
195
            $sequence = $sequenceResource->getSequence();
196
            $graph = $sequence->getUnSerializeGraph();
197
            $vertex = $graph->getVertex($resourceId);
198
            $from = $vertex->getVerticesEdgeFrom();
199
            $to = $vertex->getVerticesEdgeTo();
200
201
            $requirements = [];
202
            $dependencies = [];
203
204
            switch ($type) {
205
                case SequenceResource::SESSION_TYPE:
206
                    $requirements = $this->findSessionFromVerticesEdges($from);
207
                    $dependencies = $this->findSessionFromVerticesEdges($to);
208
                    break;
209
            }
210
211
            $result[$sequence->getId()] = [
212
                'name' => $sequence->getName(),
213
                'requirements' => $requirements,
214
                'dependencies' => $dependencies,
215
            ];
216
        }
217
218
        return [
219
            'sequences' => $result,
220
        ];
221
    }
222
223
    /**
224
     * Get sessions from vertices.
225
     *
226
     * @param Vertices $verticesEdges The vertices
227
     *
228
     * @return array
229
     */
230
    protected function findSessionFromVerticesEdges(Vertices $verticesEdges)
231
    {
232
        $sessionVertices = [];
233
        foreach ($verticesEdges as $supVertex) {
234
            $vertexId = $supVertex->getId();
235
            $session = $this->getEntityManager()->getReference(
236
                'ChamiloCoreBundle:Session',
237
                $vertexId
238
            );
239
240
            if (empty($session)) {
241
                continue;
242
            }
243
244
            $sessionVertices[$vertexId] = $session;
245
        }
246
247
        return $sessionVertices;
248
    }
249
}
250