Issues (150)

src/Extension/JWTTrait.php (2 issues)

Labels
1
<?php
2
namespace SoliDry\Extension;
3
4
use SoliDry\Helpers\Json;
5
use SoliDry\Helpers\Jwt;
6
use SoliDry\Types\JwtInterface;
7
8
/**
9
 * Class JWTTrait
10
 *
11
 * @package SoliDry\Extension
12
 *
13
 * @property ApiController model
14
 */
15
trait JWTTrait
16
{
17
    /**
18
     *  Creates new user with JWT + password hashed
19
     */
20
    protected function createJwtUser()
21
    {
22
        if(empty($this->model->password)) {
23
            Json::outputErrors(
24
                [
25
                    [
26
                        JSONApiInterface::ERROR_TITLE  => 'Password should be provided',
27
                        JSONApiInterface::ERROR_DETAIL => 'To get refreshed token in future usage of application - user password should be provided',
28
                    ],
29
                ]
30
            );
31
        }
32
33
        /** @var \Illuminate\Database\Eloquent\Model $model */
34
        $model           = $this->getEntity($this->model->id);
0 ignored issues
show
It seems like getEntity() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

34
        /** @scrutinizer ignore-call */ 
35
        $model           = $this->getEntity($this->model->id);
Loading history...
The property id does not seem to exist on SoliDry\Extension\ApiController.
Loading history...
35
        $model->jwt      = Jwt::create($this->model->id);
36
        $model->password = password_hash($this->model->password, PASSWORD_DEFAULT);
37
        $model->save();
38
        $this->model = $model;
39
        unset($this->model->password);
40
    }
41
42
    /**
43
     * @param $model
44
     * @param $jsonApiAttributes
45
     */
46
    private function updateJwtUser(&$model, $jsonApiAttributes)
47
    {
48
        if(password_verify($jsonApiAttributes[JwtInterface::PASSWORD], $model->password) === false) {
49
            Json::outputErrors(
50
                [
51
                    [
52
                        JSONApiInterface::ERROR_TITLE  => 'Password is invalid.',
53
                        JSONApiInterface::ERROR_DETAIL => 'To get refreshed token - pass the correct password',
54
                    ],
55
                ]
56
            );
57
        }
58
59
        $model->jwt = Jwt::create($model->id);
60
        unset($model->password);
61
    }
62
}