AccountController::ApplicationsByAdvert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Advert;
6
use App\Entity\Application;
7
use App\Entity\User;
8
use App\Form\AccountType;
9
use App\Form\CurriculumType;
10
use App\Repository\ApplicationRepository;
11
use App\Service\FileUploader;
12
use Doctrine\Common\Persistence\ObjectManager;
13
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Routing\Annotation\Route;
17
use Symfony\Component\HttpFoundation\File\Exception\FileException;
18
19
class AccountController extends AbstractController
20
{
21
    /**
22
     * @Route("/account/cv/{id}", name="add_cv")
23
     */
24
    public function curriculum(Request $request, $id, FileUploader $fileUploader)
25
    {
26
        $em = $this->getDoctrine()->getManager();
27
        $user = $em->getRepository(User::class)->find($id);
28
        //dump($user);die();
29
30
        $form = $this->createForm(CurriculumType::class, $user);
31
        $form->handleRequest($request);
32
33
34
        if ($form->isSubmitted() && $form->isValid()) {
35
            // $file stores the uploaded PDF file
36
            /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
37
            $file = $form->get('curriculum')->getData();
38
            $fileName = $fileUploader->upload($file);
39
40
            $user->setCurriculum($fileName);
41
42
            $em->persist($user);
43
            $em->flush();
44
45
            $this->addFlash('success', 'Votre CV a été téléchargé');
46
            return $this->redirectToRoute('account', ['id' => $user->getId()]);
47
        }
48
49
        return $this->render('account/cv.html.twig', [
50
            'cvForm' => $form->createView(),
51
        ]);
52
    }
53
54
    /**
55
     * @Route("/account/cv/edit/{id}", name="edit_cv")
56
     */
57
    public function editCurriculum(Request $request, $id, FileUploader $fileUploader)
58
    {
59
        $em = $this->getDoctrine()->getManager();
60
        $user = $em->getRepository(User::class)->find($id);
61
        //dump($user);die();
62
63
        $form = $this->createForm(CurriculumType::class, $user);
64
        $form->handleRequest($request);
65
66
67
        if ($form->isSubmitted() && $form->isValid()) {
68
            // $file stores the uploaded PDF file
69
            /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
70
            $file = $form->get('curriculum')->getData();
71
            ;
72
            $fileName = $fileUploader->upload($file);
73
74
            $user->setCurriculum($fileName);
75
76
            $em->persist($user);
77
            $em->flush();
78
79
            $this->addFlash('success', 'Votre CV a été téléchargé');
80
            return $this->redirectToRoute('account', ['id' => $user->getId()]);
81
        }
82
83
        return $this->render('account/cv_edit.html.twig', [
84
            'cvForm' => $form->createView(),
85
        ]);
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    private function generateUniqueFileName()
0 ignored issues
show
Unused Code introduced by
The method generateUniqueFileName() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
92
    {
93
        // md5() reduces the similarity of the file names generated by
94
        // uniqid(), which is based on timestamps
95
        return md5(uniqid());
96
    }
97
98
    /**
99
     * @Route("/account/{id}", name="account")
100
     */
101
    public function view($id, Request $request, ObjectManager $manager)
102
    {
103
        $user = $this->getDoctrine()->getRepository(User::class)->find($id);
104
        if (null === $user) {
105
            throw new NotFoundHttpException("L'utilisateur ".$id." n'existe pas");
106
        }
107
        //dump($this->getUser()->getEmail());die();
108
        if ($user->getEmail() === $this->getUser()->getEmail()) {
109
            $form = $this->createForm(AccountType::class, $user);
110
            $form->handleRequest($request);
111
            if ($form->isSubmitted() && $form->isValid()) {
112
                $user->setEmail($this->getUser()->getEmail());
113
                $manager->persist($user);
114
                $manager->flush();
115
                $this->addFlash(
116
                    'notice',
117
                    'Votre compte a bien été modifié!'
118
                );
119
                dump($request);
0 ignored issues
show
Bug introduced by
The call to dump() has too few arguments starting with valid. ( Ignorable by Annotation )

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

119
                /** @scrutinizer ignore-call */ 
120
                dump($request);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
120
                die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
121
                return $this->redirectToRoute('account', ['id' => $user->getId()]);
0 ignored issues
show
Unused Code introduced by
return $this->redirectTo...id' => $user->getId())) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
122
            }
123
            return $this->render('account/index.html.twig', [
124
                'user' => $user,
125
                'formAccount' => $form->createView()
126
            ]);
127
        } else {
128
            $this->addFlash('warning', 'Vous ne pouvez pas accéder à cette page !');
129
            return $this->redirectToRoute('home');
130
        }
131
    }
132
133
    /**
134
     * @Route("/account/myadverts/{id}", name="my_adverts")
135
     */
136
    public function myAdverts($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

136
    public function myAdverts(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        $em = $this->getDoctrine()->getManager();
0 ignored issues
show
Unused Code introduced by
The assignment to $em is dead and can be removed.
Loading history...
139
        $adverts = $this->getDoctrine()->getRepository(Advert::class)->findByAuthor($this->getUser());
0 ignored issues
show
Bug introduced by
The method findByAuthor() does not exist on App\Repository\AdvertRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

139
        $adverts = $this->getDoctrine()->getRepository(Advert::class)->/** @scrutinizer ignore-call */ findByAuthor($this->getUser());
Loading history...
140
141
        return $this->render('account/myadverts.html.twig', [
142
            'adverts' => $adverts
143
        ]);
144
    }
145
146
    /**
147
     * @Route("/account/myadvert/applications/{id}", name="list_applications")
148
     */
149
    public function ApplicationsByAdvert($id)
150
    {
151
        $em = $this->getDoctrine()->getManager();
152
        $advert = $this->getDoctrine()->getRepository(Advert::class)->find($id);
153
154
        $listApplications = $em
155
            ->getRepository(Application::class)
156
            ->findBy(['advert' => $advert])
157
        ;
158
159
        return $this->render('account/applications.html.twig', [
160
            'advert' => $advert,
161
            'listApplications' => $listApplications
162
        ]);
163
    }
164
}
165