Issues (17)

src/Api.php (2 issues)

Severity
1
<?php
2
3
namespace Yansongda\LaravelApi;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Carbon;
7
use Illuminate\Support\Str;
8
use Yansongda\LaravelApi\Exceptions\CreateAppException;
9
use Yansongda\LaravelApi\Exceptions\GenerateAccessTokenException;
10
use Yansongda\LaravelApi\Models\AccessToken;
11
use Yansongda\LaravelApi\Models\App;
12
13
class Api
14
{
15
    /**
16
     * User model.
17
     *
18
     * @var Model
19
     */
20
    public static $user;
21
22
    /**
23
     * Route prefix.
24
     *
25
     * @var string
26
     */
27
    public static $routePrefix = 'api';
28
29
    /**
30
     * AccessToken expired after.
31
     *
32
     * @var int
33
     */
34
    public static $ttl = 7200;
35
36
    /**
37
     * Is enable route.
38
     *
39
     * @var bool
40
     */
41
    public static $enableRoute = true;
42
43
    /**
44
     * Set route prefix.
45
     *
46
     * @author yansongda <[email protected]>
47
     *
48
     * @param string $prefix
49
     *
50
     * @return void
51
     */
52
    public static function setRoutePrefix($prefix = '')
53
    {
54
        self::$routePrefix = $prefix;
55
    }
56
57
    /**
58
     * Set ttl.
59
     *
60
     * @author yansongda <[email protected]>
61
     *
62
     * @param int $ttl
63
     */
64
    public static function setTtl($ttl = 7200)
65
    {
66
        self::$ttl = $ttl;
67
    }
68
69
    /**
70
     * Enable route.
71
     *
72
     * @author yansongda <[email protected]>
73
     *
74
     * @param bool $status
75
     *
76
     * @return void
77
     */
78
    public static function enableRoute($status = true)
79
    {
80
        self::$enableRoute = $status;
81
    }
82
83
    /**
84
     * Generate access_token.
85
     *
86
     * @author yansongda <[email protected]>
87
     *
88
     * @param App $app
89
     *
90
     * @return string
91
     * @throws GenerateAccessTokenException
92
     */
93
    public static function generateAccessToken($app)
94
    {
95
        if (! ($app instanceof App)) {
0 ignored issues
show
$app is always a sub-type of Yansongda\LaravelApi\Models\App.
Loading history...
96
            throw new GenerateAccessTokenException('[' . get_class($app) . '] Must Be An Instance Of [Yansongda\LaravelApi\Models\App]');
97
        }
98
99
        return AccessToken::updateOrCreate([
100
            'user_id' => $app->user_id,
101
            'app_id' => $app->app_id,
102
        ], [
103
            'access_token' => md5(uniqid('access_token', true) . $app->user_id . $app->app_id . $app->app_secret),
104
            'expired_at' => Carbon::now()->addSeconds(self::$ttl)
105
        ]);
106
    }
107
108
    /**
109
     * Create app.
110
     *
111
     * @author yansongda <[email protected]>
112
     *
113
     * @param Model $user
114
     * @param string|null $others
115
     *
116
     * @return App
117
     * @throws CreateAppException
118
     */
119
    public static function createApp($user, $others = null)
120
    {
121
        if (!$user instanceof Model) {
0 ignored issues
show
$user is always a sub-type of Illuminate\Database\Eloquent\Model.
Loading history...
122
            throw new CreateAppException('[' . get_class($user) . '] Must Be An Instance Of [Illuminate\Database\Eloquent\Model]');
123
        }
124
125
        return App::create([
126
            'user_id' => $user->{$user->getKeyName()},
127
            'app_id' => md5(uniqid('app_id', true) . $user->{$user->getKeyName()}),
128
            'app_secret' => md5(Str::random(32)),
129
            'others' => $others,
130
        ]);
131
    }
132
}
133