Completed
Pull Request — develop (#121)
by Patrick
09:18
created

RestAPI::sendEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
        \Sentry\init(['dsn' => 'https://[email protected]/4283882' ]);
25
        return $app->any('[/]', $this);
26
    }
27
28
    public function validateLoggedIn($request)
29
    {
30
        $this->user = $request->getAttribute('user');
31
        if($this->user === false || $this->user === null)
32
        {
33
            throw new \Exception('Must be logged in', \Http\Rest\ACCESS_DENIED);
34
        }
35
    }
36
37
    protected function getParsedBody($request)
38
    {
39
        $obj = $request->getParsedBody();
40 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...
41
        {
42
            $request->getBody()->rewind();
43
            $obj = $request->getBody()->getContents();
44
            $tmp = json_decode($obj, true);
45
            if($tmp !== null)
46
            {
47
                $obj = $tmp;
48
            }
49
        }
50
        return $obj;
51
    }
52
53
    protected function sendEmail($email)
54
    {
55
        $emailProvider = \EmailProvider::getInstance();
56
        if($emailProvider->sendEmail($email) === false)
57
        {
58
            throw new \Exception('Unable to send email!');
59
        }
60
    }
61
}
62
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
63