Completed
Push — master ( cbcdf8...fd3ac8 )
by Patrick
02:07
created

RestAPI   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 23.26 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 4 1
A validateLoggedIn() 0 8 3
A getParsedBody() 10 15 3
A sendEmail() 0 8 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 Http\Rest;
3
4
use \Psr\Http\Message\ServerRequestInterface as Request;
5
use \Psr\Http\Message\ResponseInterface as Response;
6
7
require 'vendor/autoload.php';
8
9
const SUCCESS = 0;
10
const UNRECOGNIZED_METHOD = 1;
11
const INVALID_PARAM = 2;
12
const ALREADY_LOGGED_IN = 3;
13
const INVALID_LOGIN = 4;
14
const ACCESS_DENIED = 5;
15
const INTERNAL_ERROR = 6;
16
const UNKNOWN_ERROR = 255;
17
18
class RestAPI
19
{
20
    protected $user = null;
21
22
    public function setup($app)
23
    {
24
        return $app->any('[/]', $this);
25
    }
26
27
    public function validateLoggedIn($request)
28
    {
29
        $this->user = $request->getAttribute('user');
30
        if($this->user === false || $this->user === null)
31
        {
32
            throw new \Exception('Must be logged in', \Http\Rest\ACCESS_DENIED);
33
        }
34
    }
35
36
    protected function getParsedBody($request)
37
    {
38
        $obj = $request->getParsedBody();
39 View Code Duplication
        if($obj === null)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
40
        {
41
            $request->getBody()->rewind();
42
            $obj = $request->getBody()->getContents();
43
            $tmp = json_decode($obj, true);
44
            if($tmp !== null)
45
            {
46
                $obj = $tmp;
47
            }
48
        }
49
        return $obj;
50
    }
51
52
    protected function sendEmail($email)
53
    {
54
        $emailProvider = \EmailProvider::getInstance();
55
        if($emailProvider->sendEmail($email) === false)
56
        {
57
            throw new \Exception('Unable to send email!');
58
        }
59
    }
60
}
61
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
62