Passed
Push — master ( fb1eb0...02cee8 )
by Guido
08:09
created

Examples   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 9.38 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 9
loc 96
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 4

10 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 4 1
A login() 0 8 1
A logout() 0 5 1
A asd() 0 4 1
A qwe() 0 13 1
A lorep() 0 4 1
A authorization() 0 6 1
A transformer() 0 8 1
A authTest() 0 4 1
A basicAuth() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Gvera\Controllers;
3
4
use Gvera\Helpers\http\JSONResponse;
5
use Gvera\Helpers\http\Response;
6
use Gvera\Helpers\http\TransformerResponse;
7
use Gvera\Helpers\locale\Locale;
8
use Gvera\Helpers\transformers\UserTransformer;
9
use Gvera\Models\User;
10
11
/**
12
 * @OA\Info(title="My First API", version="0.1")
13
 */
14
15
/**
16
 * Controller Class Doc Comment
17
 *
18
 * @category Class
19
 * @package  src/controllers
20
 * @author    Guido Vera
21
 * @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
22
 * @link     http://www.github.com/veraguido/gv
23
 * @method getEntityManager()
24
 */
25
class Examples extends GvController
26
{
27
    /**
28
     * @OA\Get(
29
     *     path="/examples",
30
     *     @OA\Response(response="200", description="An example resource")
31
     * )
32
     */
33
    public function index()
34
    {
35
        $this->viewParams = ['csrf' => $this->generateCSRFToken()];
36
    }
37
38
    /**
39
     * @throws \Exception
40
     * @method $this->diContainer->get('loginCommand');
41
     *
42
     */
43
    public function login()
44
    {
45
        $loginCommand = $this->getLoginCommand();
46
        $loginCommand->setUsername($this->httpRequest->getParameter('username'));
47
        $loginCommand->setPassword($this->httpRequest->getParameter('password'));
48
49
        $loginCommand->execute();
50
    }
51
52
    public function logout()
53
    {
54
        $userService = $this->getUserService();
55
        $userService->logout();
56
    }
57
58
    public function asd()
59
    {
60
        $this->httpResponse->response(new Response("trough routes.yml"));
61
    }
62
63
    /**
64
     * @throws \Doctrine\ORM\OptimisticLockException
65
     * @throws \Exception
66
     * @httpMethod("POST")
67
     */
68
    public function qwe()
69
    {
70
        $this->validateCSRFToken($this->httpRequest->getParameter('csrf'));
71
        $registerUserCommand = $this->getCreateNewUserCommand();
72
        $registerUserCommand
73
            ->setName($this->httpRequest->getParameter('username'))
74
            ->setEmail($this->httpRequest->getParameter('email'))
75
            ->setPassword($this->httpRequest->getParameter('password'));
76
                
77
        $registerUserCommand->execute();
78
79
        $this->httpRequest->moveFileToDirectory("/tmp/", 'avatar-pic');
80
    }
81
82
    public function lorep()
83
    {
84
        $this->httpResponse->response(Locale::getLocale("Hello world"));
85
    }
86
87
    public function authorization()
88
    {
89
        $user = $this->getEntityManager()->getRepository(User::class)->findOneBy(['username' => 'mod']);
90
        $service = $this->getUserService();
91
        $this->httpResponse->response(new JSONResponse(['can all' => $service->userCan($user, 'all')]));
92
    }
93
94
    /**
95
     * @throws \Gvera\Exceptions\NotImplementedMethodException
96
     */
97
    public function transformer()
98
    {
99
        $entityManager = $this->getEntityManager();
100
        $repository = $entityManager->getRepository(User::class);
101
        $user = $repository->findOneBy(['username' => 'asda']);
102
103
        $this->httpResponse->response(new TransformerResponse(new UserTransformer($user)));
104
    }
105
106
    public function authTest()
107
    {
108
        $this->httpResponse->response($this->checkAuthorization());
109
    }
110
111 View Code Duplication
    public function basicAuth()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        try {
114
            $this->checkApiAuthentication();
115
        } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
116
            $content = ['message' => $e->getMessage()];
117
            $this->httpResponse->response(new JSONResponse($content, Response::HTTP_RESPONSE_UNAUTHORIZED));
118
        }
119
    }
120
}
121