Completed
Push — master ( 943fe6...d5ae66 )
by archer
02:53
created

AppClient::generateAppId()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 1
nop 0
1
<?php
2
3
namespace ArcherZdip\LaravelApiAuth\Models;
4
5
use Illuminate\Support\Str;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Str was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Database\Eloquent\SoftDeletes;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\SoftDeletes was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class AppClient extends Model
10
{
11
    use SoftDeletes;
12
13
    const EVENT_NAME_CREATED     = 'created';
14
    const EVENT_NAME_ACTIVATED   = 'activated';
15
    const EVENT_NAME_DEACTIVATED = 'deactivated';
16
    const EVENT_NAME_DELETED     = 'deleted';
17
18
    /** @var int activate status */
19
    const ACTIVATE = 1;
20
    const DEACTIVATE = 0;
21
22
    protected static $nameRegex = '/^[a-z0-9-]{1,255}$/';
23
24
    /**
25
     * The attributes that are mass assignable.
26
     *
27
     * @var array
28
     */
29
    protected $fillable = [
30
        'name',
31
        'appid',
32
        'secret',
33
        'active'
34
    ];
35
36
    /**
37
     * The attributes that should be hidden for arrays.
38
     *
39
     * @var array
40
     */
41
    protected $hidden = [];
42
43
    public static function boot()
44
    {
45
        parent::boot();
46
47
        static::created(function(AppClient $appClient) {
48
            self::logApiAuthOprateEvent($appClient, self::EVENT_NAME_CREATED);
49
        });
50
51
        static::updated(function(AppClient $appClient) {
52
53
            $changed = $appClient->getDirty();
54
55
            if (isset($changed) && $changed['active'] === self::ACTIVATE) {
56
                self::logApiAuthOprateEvent($appClient, self::EVENT_NAME_ACTIVATED);
57
            }
58
59
            if (isset($changed) && $changed['active'] === self::DEACTIVATE) {
60
                self::logApiAuthOprateEvent($appClient, self::EVENT_NAME_DEACTIVATED);
61
            }
62
63
        });
64
65
        static::deleted(function(AppClient $appClient) {
66
            self::logApiAuthOprateEvent($appClient, self::EVENT_NAME_DELETED);
67
        });
68
69
    }
70
71
    /**
72
     * AppClient constructor.
73
     * @param array $attributes
74
     */
75
    public function __construct(array $attributes = [])
76
    {
77
        parent::__construct($attributes);
78
79
        $this->setTable(config('apikey.table_name.app_clients'));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

79
        $this->setTable(/** @scrutinizer ignore-call */ config('apikey.table_name.app_clients'));
Loading history...
80
    }
81
82
    /**
83
     * Get secret by appid
84
     *
85
     * @param $appid
86
     * @return mixed
87
     */
88
    public static function getSecretByAppId($appid)
89
    {
90
        return self::where([
91
            'active' => self::ACTIVATE,
92
            'appid'  => $appid
93
        ])->first();
94
    }
95
96
    /**
97
     * Generate AppId
98
     *
99
     * @return string
100
     */
101
    public static function generateAppId()
102
    {
103
        do {
104
            $appId = Str::random(config('apikey.appid_length', 16));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

104
            $appId = Str::random(/** @scrutinizer ignore-call */ config('apikey.appid_length', 16));
Loading history...
105
        } while (self::appIdExists($appId));
106
107
        return $appId;
108
    }
109
110
    /**
111
     * Generate secret
112
     *
113
     * @return string
114
     */
115
    public static function generateSecret()
116
    {
117
        return Str::random(config('apikey.secret_length', 64));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

117
        return Str::random(/** @scrutinizer ignore-call */ config('apikey.secret_length', 64));
Loading history...
118
    }
119
120
    /**
121
     * Check name is valid format
122
     *
123
     * @param $name
124
     * @return bool
125
     */
126
    public static function isValidName($name)
127
    {
128
        return (bool)preg_match(self::$nameRegex, $name);
129
    }
130
131
    /**
132
     * Check name is or not exists
133
     *
134
     * @param $name
135
     * @return bool
136
     */
137
    public static function nameExists($name)
138
    {
139
        return self::where('name', '=', $name)->first() instanceof self;
140
    }
141
142
    /**
143
     * Check AppId is or not exists
144
     *
145
     * @param $secret
146
     * @return bool
147
     */
148
    public static function appIdExists($appId)
149
    {
150
        return self::where('appid', $appId)->withTrashed()->first() instanceof self;
151
    }
152
153
    /**
154
     * Log an app oprate admin event
155
     *
156
     * @param AppClient $appClient
157
     * @param string $eventName
158
     */
159
    protected static function logApiAuthOprateEvent(AppClient $appClient, $eventName)
160
    {
161
        $event = new ApiAuthOprateEvent();
162
        $event->app_client_id = $appClient->id;
163
        $event->ip_address = request()->ip();
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

163
        $event->ip_address = /** @scrutinizer ignore-call */ request()->ip();
Loading history...
164
        $event->event = $eventName;
165
        $event->save();
166
    }
167
}