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

StartController::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 44
rs 9.536
cc 3
nop 0
nc 4
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
        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...
38
            $newFilename = uniqid().'_iddoc.jpg';
39
40
            $imgIddoc->move($userDirName, $newFilename);
41
42
            $log = new Log();
43
            $log
44
                ->setExercise($exercise)
45
                ->setLevel(-1)
46
                ->setImageFilename($newFilename)
47
            ;
48
49
            $this->em->persist($log);
50
        }
51
52
        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...
53
            $newFilename = uniqid().'_learner.jpg';
54
55
            $imgLearner->move($userDirName, $newFilename);
56
57
            $log = new Log();
58
            $log
59
                ->setExercise($exercise)
60
                ->setLevel(-1)
61
                ->setImageFilename($newFilename)
62
            ;
63
64
            $this->em->persist($log);
65
        }
66
67
        $this->em->flush();
68
69
        return HttpResponse::create();
70
    }
71
72
    private function createDirectory(): string
73
    {
74
        $user = api_get_user_entity(api_get_user_id());
75
76
        $pluginDirName = api_get_path(SYS_UPLOAD_PATH).'plugins/exercisemonitoring';
77
        $userDirName = $pluginDirName.'/'.$user->getId();
78
79
        $fs = new Filesystem();
80
        $fs->mkdir(
81
            [$pluginDirName, $userDirName],
82
            api_get_permissions_for_new_directories()
83
        );
84
85
        return $userDirName;
86
    }
87
}
88