Passed
Pull Request — master (#57)
by Stone
03:27
created

User::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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;
0 ignored issues
show
Bug introduced by
The trait Core\Traits\StringFunctions requires the property $childNodes which is not provided by App\Controllers\Ajax\User.
Loading history...
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
        try {
34
            $return = !$this->userModel->isEmailUsed($email);
0 ignored issues
show
Bug introduced by
It seems like $email can also be of type null; however, parameter $email of App\Models\UserModel::isEmailUsed() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
            $return = !$this->userModel->isEmailUsed(/** @scrutinizer ignore-type */ $email);
Loading history...
35
        } catch (\Exception $e) {
36
            throw new JsonException($e->getMessage());
37
        }
38
        echo json_encode($return);
39
40
    }
41
42
    /**
43
     * @throws JsonException
44
     */
45
    public function toggleActivation()
46
    {
47
        $this->onlyAdmin();
48
        $this->onlyPost();
49
        $state = (bool)($this->request->getData("state") === 'true');
50
        $userId = (int)$this->request->getData("userId");
51
52
        $result = array();
53
        $result["success"] = false;
54
        $result["state"] = $state;
55
        $result["userId"] = $userId;
56
57
        // we can not update the Original Admin activation state
58
        if($userId !== 1)
59
        {
60
            $result["success"] = $this->userModel->activateUser(!$state, $userId);
61
            $result["state"] = !$state;
62
            $result["userId"] = $userId;
63
        }
64
65
        echo json_encode($result);
66
    }
67
}