Test Failed
Push — master ( 5851fa...90c56e )
by Maximo
02:00
created

UsersController::devices()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 26
nc 9
nop 0
dl 0
loc 44
ccs 0
cts 33
cp 0
crap 42
rs 8.8817
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Gewaer\Models\Users;
8
use Gewaer\Models\UserLinkedSources;
9
use Baka\Auth\Models\Sources;
10
use Phalcon\Http\Response;
11
use Phalcon\Validation;
12
use Phalcon\Validation\Validator\PresenceOf;
13
use Gewaer\Exception\BadRequestHttpException;
14
use Gewaer\Exception\UnprocessableEntityHttpException;
15
16
/**
17
 * Users controller
18
 *
19
 */
20
class UsersController extends \Baka\Auth\UsersController
21
{
22
    /*
23
     * fields we accept to create
24
     *
25
     * @var array
26
     */
27
    protected $createFields = ['name', 'firstname', 'lastname', 'displayname', 'email', 'password', 'created_at', 'updated_at', 'default_company', 'family'];
28
29
    /*
30
     * fields we accept to create
31
     *
32
     * @var array
33
     */
34
    protected $updateFields = ['name', 'firstname', 'lastname', 'displayname', 'email', 'password', 'created_at', 'updated_at', 'default_company'];
35
36
    /**
37
     * set objects
38
     *
39
     * @return void
40
     */
41
    public function onConstruct()
42
    {
43
        $this->model = new Users();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Gewaer\Models\Users() of type Gewaer\Models\Users is incompatible with the declared type array of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
    }
45
46
    /**
47
     * Associate a Device with the corrent loggedin user
48
     *
49
     * @url /users/{id}/device
50
     * @method POST
51
     * @return Response
52
     */
53
    public function devices(): Response
54
    {
55
        //Ok let validate user password
56
        $validation = new Validation();
57
        $validation->add('app', new PresenceOf(['message' => _('App name is required.')]));
58
        $validation->add('deviceId', new PresenceOf(['message' => _('device ID is required.')]));
59
60
        //validate this form for password
61
        $messages = $validation->validate($this->request->getPost());
62
        if (count($messages)) {
63
            foreach ($messages as $message) {
64
                throw new BadRequestHttpException((string) $message);
65
            }
66
        }
67
68
        $app = $this->request->getPost('app', 'string');
69
        $deviceId = $this->request->getPost('deviceId', 'string');
70
71
        //get the app source
72
        if ($source = Sources::getByTitle($app)) {
73
            if (!$userSource = UserLinkedSources::findFirst(['conditions' => 'user_id = ?0 and source_user_id_text =?1', 'bind' => [$this->userData->getId(), $deviceId]])) {
0 ignored issues
show
Unused Code introduced by
The assignment to $userSource is dead and can be removed.
Loading history...
Bug Best Practice introduced by
The property userData does not exist on Gewaer\Api\Controllers\UsersController. Since you implemented __get, consider adding a @property annotation.
Loading history...
74
                $userSource = new UserLinkedSources();
75
                $userSource->user_id = $this->userData->getId();
0 ignored issues
show
Bug Best Practice introduced by
The property user_id does not exist on Gewaer\Models\UserLinkedSources. Since you implemented __set, consider adding a @property annotation.
Loading history...
76
                $userSource->source_id = $source->source_id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $source->source_id of type Phalcon\Mvc\Model\Resultset or Phalcon\Mvc\Phalcon\Mvc\Model is incompatible with the declared type integer of property $source_id.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
                $userSource->source_user_id = $this->userData->getId();
0 ignored issues
show
Bug Best Practice introduced by
The property source_user_id does not exist on Gewaer\Models\UserLinkedSources. Since you implemented __set, consider adding a @property annotation.
Loading history...
78
                $userSource->source_user_id_text = $deviceId;
0 ignored issues
show
Bug Best Practice introduced by
The property source_user_id_text does not exist on Gewaer\Models\UserLinkedSources. Since you implemented __set, consider adding a @property annotation.
Loading history...
79
                $userSource->source_username = $this->userData->displayname . ' ' . $app;
80
81
                if (!$userSource->save()) {
82
                    throw new UnprocessableEntityHttpException(current($userSource->getMessages()));
83
                }
84
85
                $msg = 'User Device Associated';
86
            } else {
87
                $msg = 'User Device Already Associated';
88
            }
89
        }
90
91
        //clean password @todo move this to a better place
92
        $this->userData->password = null;
93
94
        return $this->response([
95
            'msg' => $msg,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $msg does not seem to be defined for all execution paths leading up to this point.
Loading history...
96
            'user' => $this->userData
97
        ]);
98
    }
99
}
100