Completed
Push — master ( 6d849d...f9dc97 )
by Michal
02:55 queued 57s
created

DevelopersController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 10.11 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 4
dl 9
loc 89
ccs 57
cts 57
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeFilter() 0 6 1
A login() 0 5 1
B callback() 9 42 5
A logout() 0 9 1
A _authenticateDeveloper() 0 14 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
3
/* vim: set expandtab sw=4 ts=4 sts=4: */
4
/**
5
 * Developer controller handling developer login/logout/register.
6
 *
7
 * phpMyAdmin Error reporting server
8
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 *
17
 * @see      https://www.phpmyadmin.net/
18
 */
19
20
namespace App\Controller;
21
22
use Cake\Core\Configure;
23
use Cake\Event\Event;
24
use Cake\Network\Exception\NotFoundException;
25
use Cake\ORM\TableRegistry;
26
27
/**
28
 * Developer controller handling developer login/logout/register.
29
 */
30
class DevelopersController extends AppController
31
{
32
    public $helpers = array('Html', 'Form');
33
34
    public $components = array(
35
        'GithubApi',
36
    );
37
38 3
    public function beforeFilter(Event $event)
39
    {
40 3
        parent::beforeFilter($event);
41 3
        $this->GithubApi->githubConfig = Configure::read('GithubConfig');
42 3
        $this->GithubApi->githubRepo = Configure::read('GithubRepoPath');
43 3
    }
44
45 1
    public function login()
46
    {
47 1
        $url = $this->GithubApi->getRedirectUrl('user:email,public_repo');
48 1
        $this->redirect($url);
49 1
    }
50
51 1
    public function callback()
52
    {
53 1
        $code = $this->request->query('code');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\ServerRequest::query() has been deprecated with message: 3.4.0 Use getQuery() or the PSR-7 getQueryParams() and withQueryParams() methods instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54 1
        $accessToken = $this->GithubApi->getAccessToken($code);
55 1
        if ($code && $accessToken) {
56 1
            list($userInfo, $status) = $this->GithubApi->getUserInfo($accessToken);
57 1
            if ($status != 200) {
58 1
                $flash_class = 'alert alert-error';
59 1
                $this->Flash->default($userInfo['message'],
60 1
                    array('params' => array('class' => $flash_class)));
61
62 1
                $this->redirect('/');
63 1
                return;
64
            } else {
65 1
                $userInfo['has_commit_access'] = $this->GithubApi->canCommitTo(
66 1
                    $userInfo['login'],
67 1
                    $this->GithubApi->githubRepo,
68 1
                    Configure::read('GithubAccessToken')
69
                );
70
71 1
                $this->_authenticateDeveloper($userInfo, $accessToken);
72
73 1
                $flash_class = 'alert alert-success';
74 1
                $this->Flash->default('You have been logged in successfully',
75 1
                        array('params' => array('class' => $flash_class)));
76
            }
77 View Code Duplication
        } else {
78 1
            $flash_class = 'alert alert-error';
79 1
            $this->Flash->default('We were not able to authenticate you.'
80 1
                    . ' Please try again later',
81 1
                    array('params' => array('class' => $flash_class)));
82
83 1
            $this->redirect('/');
84 1
            return;
85
        }
86
87 1
        $last_page = $this->request->session()->read('last_page');
88 1
        if (empty($last_page)) {
89 1
            $last_page = array('controller' => 'reports', 'action' => 'index');
90
        }
91 1
        $this->redirect($last_page);
92 1
    }
93
94 1
    public function logout()
95
    {
96 1
        $this->request->session()->destroy();
97
98 1
        $flash_class = 'alert alert-success';
99 1
        $this->Flash->default('You have been logged out successfully',
100 1
                array('params' => array('class' => $flash_class)));
101 1
        $this->redirect('/');
102 1
    }
103
104 1
    protected function _authenticateDeveloper($userInfo, $accessToken)
105
    {
106 1
        $developers = $this->Developers->findByGithubId($userInfo['id']);
107 1
        $developer = $developers->all()->first();
108 1
        if (!$developer) {
109 1
            $developer = $this->Developers->newEntity();
110
        } else {
111 1
            $this->Developers->id = $developer['id'];
112
        }
113 1
        $this->Developers->id = $this->Developers->saveFromGithub($userInfo, $accessToken, $developer);
114 1
        $this->request->session()->write('Developer.id', $this->Developers->id);
115 1
        $this->request->session()->write('access_token', $accessToken);
116 1
        $this->request->session()->write('read_only', !($userInfo['has_commit_access']));
117 1
    }
118
}
119