Passed
Pull Request — master (#61)
by Stone
08:36 queued 05:08
created

User::isEmailUnique()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 21
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Ajax;
4
5
use App\Models\UserModel;
6
use Core\AjaxController;
7
use Core\Container;
8
use Core\JsonException;
9
use Core\Traits\StringFunctions;
10
11
class User  extends AjaxController{
12
13
    use StringFunctions;
14
15
    private $userModel;
16
17
    public function __construct(Container $container)
18
    {
19
        parent::__construct($container);
20
        $this->userModel = new UserModel($this->container);
21
    }
22
23
    public function isEmailUnique($get)
24
    {
25
        //the router needs a parameter with get functions else throsw a wobbly
26
        //we pass a get variable and call the /controller/function/get?bla
27
        //for better use and security, we must pass "get" as the parameter
28
        if(!$this->startsWith(strtolower($get),"get"))
29
        {
30
            throw new JsonException("invalid call");
31
        }
32
        $email = $this->request->getData("email");
33
        if($email === null)
34
        {
35
            throw new JsonException("Empty email");
36
        }
37
38
        try {
39
            $return = !$this->userModel->isEmailUsed($email);
40
        } catch (\Exception $e) {
41
            throw new JsonException($e->getMessage());
42
        }
43
        echo json_encode($return);
44
45
    }
46
47
    /**
48
     * @throws JsonException
49
     */
50
    public function toggleActivation()
51
    {
52
        $this->onlyAdmin();
53
        $this->onlyPost();
54
        $state = (bool)($this->request->getData("state") === 'true');
55
        $userId = (int)$this->request->getData("userId");
56
57
        $result = array();
58
        $result["success"] = false;
59
        $result["state"] = $state;
60
        $result["userId"] = $userId;
61
62
        // we can not update the Original Admin activation state
63
        if($userId !== 1)
64
        {
65
            $result["success"] = $this->userModel->activateUser(!$state, $userId);
66
            $result["state"] = !$state;
67
            $result["userId"] = $userId;
68
        }
69
70
        echo json_encode($result);
71
    }
72
}