Passed
Pull Request — 1.11.x (#4900)
by Angel Fernando Quiroz
09:35
created

StartController::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 50
rs 9.472
c 1
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
/* For license terms, see /license.txt */
4
5
use Chamilo\CourseBundle\Entity\CQuiz;
6
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log;
7
use Doctrine\ORM\EntityManager;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
use Symfony\Component\HttpFoundation\Request as HttpRequest;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HttpRequest. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use Symfony\Component\HttpFoundation\Response as HttpResponse;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HttpResponse. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
13
class StartController
14
{
15
    private $plugin;
16
    private $request;
17
    private $em;
18
19
    public function __construct(ExerciseMonitoringPlugin $plugin, HttpRequest $request, EntityManager $em)
20
    {
21
        $this->plugin = $plugin;
22
        $this->request = $request;
23
        $this->em = $em;
24
    }
25
26
    public function __invoke(): HttpResponse
27
    {
28
        $userDirName = $this->createDirectory();
29
30
        /** @var UploadedFile $imgIddoc */
31
        $imgIddoc = $this->request->files->get('iddoc');
32
        /** @var UploadedFile $imgLearner */
33
        $imgLearner = $this->request->files->get('learner');
34
35
        $exercise = $this->em->find(CQuiz::class, $this->request->request->getInt('exercise_id'));
36
37
        $fileNamesToUpdate = [];
38
39
        if ($imgIddoc) {
0 ignored issues
show
introduced by
$imgIddoc is of type Symfony\Component\HttpFoundation\File\UploadedFile, thus it always evaluated to true.
Loading history...
40
            $newFilename = uniqid().'_iddoc.jpg';
41
            $fileNamesToUpdate[] = $newFilename;
42
43
            $imgIddoc->move($userDirName, $newFilename);
44
45
            $log = new Log();
46
            $log
47
                ->setExercise($exercise)
48
                ->setLevel(-1)
49
                ->setImageFilename($newFilename)
50
            ;
51
52
            $this->em->persist($log);
53
        }
54
55
        if ($imgLearner) {
0 ignored issues
show
introduced by
$imgLearner is of type Symfony\Component\HttpFoundation\File\UploadedFile, thus it always evaluated to true.
Loading history...
56
            $newFilename = uniqid().'_learner.jpg';
57
            $fileNamesToUpdate[] = $newFilename;
58
59
            $imgLearner->move($userDirName, $newFilename);
60
61
            $log = new Log();
62
            $log
63
                ->setExercise($exercise)
64
                ->setLevel(-1)
65
                ->setImageFilename($newFilename)
66
            ;
67
68
            $this->em->persist($log);
69
        }
70
71
        $this->em->flush();
72
73
        ChamiloSession::write($this->plugin->get_name().'_orphan_snapshots', $fileNamesToUpdate);
74
75
        return HttpResponse::create();
76
    }
77
78
    private function createDirectory(): string
79
    {
80
        $user = api_get_user_entity(api_get_user_id());
81
82
        $pluginDirName = api_get_path(SYS_UPLOAD_PATH).'plugins/exercisemonitoring';
83
        $userDirName = $pluginDirName.'/'.$user->getId();
84
85
        $fs = new Filesystem();
86
        $fs->mkdir(
87
            [$pluginDirName, $userDirName],
88
            api_get_permissions_for_new_directories()
89
        );
90
91
        return $userDirName;
92
    }
93
}
94