Skip to content

Commit 730e6eb

Browse files
committed
composer exec rector
1 parent 8a3ce78 commit 730e6eb

File tree

11 files changed

+102
-73
lines changed

11 files changed

+102
-73
lines changed

src/Auth.php

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace Leaf;
44

5+
use Exception;
56
use Firebase\JWT\JWT;
67
use Firebase\JWT\Key;
78
use Leaf\Auth\Config;
89
use Leaf\Auth\User;
10+
use Leaf\Exception\General;
911
use Leaf\Helpers\Password;
1012
use Leaf\Http\Session;
13+
use League\OAuth2\Client\Provider\Google;
14+
use PDO;
15+
use Throwable;
1116

1217
/**
1318
* Leaf Simple Auth
@@ -55,31 +60,31 @@ public function __construct()
5560
});
5661

5762
$this->middleware('is', function ($role) {
58-
\Leaf\Exception\General::error(
63+
General::error(
5964
'404',
6065
'<p>The page you are looking for could not be found.</p>',
6166
403
6267
);
6368
});
6469

6570
$this->middleware('isNot', function () {
66-
\Leaf\Exception\General::error(
71+
General::error(
6772
'404',
6873
'<p>The page you are looking for could not be found.</p>',
6974
403
7075
);
7176
});
7277

7378
$this->middleware('can', function () {
74-
\Leaf\Exception\General::error(
79+
General::error(
7580
'404',
7681
'<p>The page you are looking for could not be found.</p>',
7782
403
7883
);
7984
});
8085

8186
$this->middleware('cannot', function () {
82-
\Leaf\Exception\General::error(
87+
General::error(
8388
'404',
8489
'<p>The page you are looking for could not be found.</p>',
8590
403
@@ -141,10 +146,10 @@ public function autoConnect(array $pdoOptions = [])
141146
/**
142147
* Pass in db connection instance directly
143148
*
144-
* @param \PDO $connection A connection instance of your db
149+
* @param PDO $connection A connection instance of your db
145150
* @return $this;
146151
*/
147-
public function dbConnection(\PDO $connection)
152+
public function dbConnection(PDO $connection)
148153
{
149154
$this->db = new Db();
150155
$this->db->connection($connection);
@@ -174,7 +179,7 @@ public function withGoogle(
174179
$options['redirectUri'] = _env('APP_URL') . '/auth/google/callback';
175180
}
176181

177-
$this->withProvider($clientName, new \League\OAuth2\Client\Provider\Google(array_merge([
182+
$this->withProvider($clientName, new Google(array_merge([
178183
'clientId' => $clientId,
179184
'clientSecret' => $clientSecret,
180185
'redirectUri' => $options['redirectUri'],
@@ -283,8 +288,8 @@ public function login(array $credentials): bool
283288
$this->errorsArray['auth'] = Config::get('messages.loginParamsError');
284289
return false;
285290
}
286-
} catch (\Throwable $th) {
287-
throw new \Exception($th->getMessage());
291+
} catch (Throwable $th) {
292+
throw new Exception($th->getMessage());
288293
}
289294

290295
if ($passwordKey !== false) {
@@ -350,8 +355,8 @@ public function register(array $userData): bool
350355
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
351356
return false;
352357
}
353-
} catch (\Throwable $th) {
354-
throw new \Exception($th->getMessage());
358+
} catch (Throwable $th) {
359+
throw new Exception($th->getMessage());
355360
}
356361

357362
$user = $this->db->select($table)->where($userData)->first();
@@ -420,8 +425,8 @@ public function update(array $userData): bool
420425
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
421426
return false;
422427
}
423-
} catch (\Throwable $th) {
424-
throw new \Exception($th->getMessage());
428+
} catch (Throwable $th) {
429+
throw new Exception($th->getMessage());
425430
}
426431

427432
if (Config::get('session')) {
@@ -483,8 +488,8 @@ public function updatePassword(string $oldPassword, string $newPassword): bool
483488
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
484489
return false;
485490
}
486-
} catch (\Throwable $th) {
487-
throw new \Exception($th->getMessage());
491+
} catch (Throwable $th) {
492+
throw new Exception($th->getMessage());
488493
}
489494

490495
$this->user->{$passwordKey} = $newPassword;
@@ -592,8 +597,8 @@ public function createUserFor($userData)
592597
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
593598
return false;
594599
}
595-
} catch (\Throwable $th) {
596-
throw new \Exception($th->getMessage());
600+
} catch (Throwable $th) {
601+
throw new Exception($th->getMessage());
597602
}
598603

599604
$user = $this->db->select($table)->where($userData)->first();
@@ -690,8 +695,8 @@ public function user()
690695
$this->errorsArray = $this->db->errors();
691696
return null;
692697
}
693-
} catch (\Throwable $th) {
694-
throw new \Exception($th->getMessage());
698+
} catch (Throwable $th) {
699+
throw new Exception($th->getMessage());
695700
}
696701

697702
return $this->user = (new User(
@@ -736,8 +741,8 @@ public function tokens()
736741
*/
737742
public function middleware(string $middleware, callable $callback)
738743
{
739-
if (!class_exists(\Leaf\App::class)) {
740-
throw new \Exception('This feature is only available for Leaf apps');
744+
if (!class_exists(App::class)) {
745+
throw new Exception('This feature is only available for Leaf apps');
741746
}
742747

743748
if ($middleware === 'auth.required') {
@@ -833,7 +838,7 @@ public function parseToken()
833838
$bearerToken,
834839
new Key(Config::get('token.secret'), 'HS256')
835840
);
836-
} catch (\Throwable $th) {
841+
} catch (Throwable $th) {
837842
$this->errorsArray['token'] = $th->getMessage();
838843
return null;
839844
}
@@ -875,7 +880,7 @@ public function verifyToken(string $token, ?string $purpose = null)
875880
}
876881

877882
return $user;
878-
} catch (\Throwable $th) {
883+
} catch (Throwable $th) {
879884
$this->errorsArray['token'] = $th->getMessage();
880885
return null;
881886
}
@@ -894,13 +899,13 @@ public function db()
894899
protected function checkDbConnection(): void
895900
{
896901
if (!$this->db && function_exists('db')) {
897-
if (db()->connection() instanceof \PDO || db()->autoConnect()) {
902+
if (db()->connection() instanceof PDO || db()->autoConnect()) {
898903
$this->db = db();
899904
}
900905
}
901906

902907
if (!$this->db) {
903-
throw new \Exception('You need to connect to your database first');
908+
throw new Exception('You need to connect to your database first');
904909
}
905910
}
906911

@@ -916,7 +921,7 @@ protected function getFromSession($value)
916921
protected function sessionCheck()
917922
{
918923
if (!Config::get('session')) {
919-
throw new \Exception('Turn on sessions to use this feature.');
924+
throw new Exception('Turn on sessions to use this feature.');
920925
}
921926
}
922927

src/Auth/Model.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Leaf\Auth;
44

5+
use Leaf\Date;
6+
use Leaf\Db;
57
use PDOStatement;
68

79
/**
@@ -27,7 +29,7 @@ class Model
2729
/**
2830
* DB connection
2931
*/
30-
protected \Leaf\Db $db;
32+
protected Db $db;
3133

3234
/**
3335
* User data to save
@@ -53,7 +55,7 @@ public function create(array $data): ?PDOStatement
5355
$data['user_id'] = $this->user->id();
5456

5557
if (Config::get('timestamps')) {
56-
$now = (new \Leaf\Date())->tick()->format(Config::get('timestamps.format'));
58+
$now = (new Date())->tick()->format(Config::get('timestamps.format'));
5759
$data['created_at'] = $now;
5860
$data['updated_at'] = $now;
5961
}
@@ -66,11 +68,11 @@ public function create(array $data): ?PDOStatement
6668
*
6769
* @param array $data Data to be updated
6870
*
69-
* @return \Leaf\Db
71+
* @return Db
7072
*/
71-
public function update(array $data): \Leaf\Db
73+
public function update(array $data): Db
7274
{
73-
$data['updated_at'] = (new \Leaf\Date())->tick()->format(Config::get('timestamps.format'));
75+
$data['updated_at'] = (new Date())->tick()->format(Config::get('timestamps.format'));
7476

7577
return $this->db->update($this->table)
7678
->params($data)
@@ -80,9 +82,9 @@ public function update(array $data): \Leaf\Db
8082
/**
8183
* Delete a model resource
8284
*
83-
* @return \Leaf\Db
85+
* @return Db
8486
*/
85-
public function delete(): \Leaf\Db
87+
public function delete(): Db
8688
{
8789
return $this->db->delete($this->table)
8890
->where('user_id', $this->user->id());
@@ -93,9 +95,9 @@ public function delete(): \Leaf\Db
9395
*
9496
* @param string $columns Columns to search by separated by commas or *
9597
*
96-
* @return \Leaf\Db
98+
* @return Db
9799
*/
98-
public function table($columns = '*'): \Leaf\Db
100+
public function table($columns = '*'): Db
99101
{
100102
return $this->db->select($this->table, $columns)
101103
->where('user_id', $this->user->id());

src/Auth/User.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Leaf\Auth;
44

5+
use Exception;
56
use Firebase\JWT\JWT;
7+
use Leaf\Db;
68
use Leaf\Http\Session;
9+
use Throwable;
710

811
/**
912
* Auth User
@@ -20,7 +23,7 @@ class User
2023

2124
/**
2225
* Internal instance of Leaf database
23-
* @var \Leaf\DB
26+
* @var Db
2427
*/
2528
protected $db;
2629

@@ -69,7 +72,7 @@ public function __construct($data, $session = true)
6972
$sessionLifetime = strtotime($sessionLifetime);
7073

7174
if (!$sessionLifetime) {
72-
throw new \Exception('Invalid session lifetime');
75+
throw new Exception('Invalid session lifetime');
7376
}
7477
} else {
7578
$sessionLifetime = time() + $sessionLifetime;
@@ -209,7 +212,7 @@ public function verifyEmail(): bool
209212
->execute();
210213

211214
return true;
212-
} catch (\Throwable $th) {
215+
} catch (Throwable $th) {
213216
return false;
214217
}
215218
}
@@ -243,7 +246,7 @@ public function get()
243246

244247
/**
245248
* Set user db instance
246-
* @param \Leaf\Db $db
249+
* @param Db $db
247250
* @return User
248251
*/
249252
public function setDb($db)
@@ -293,14 +296,14 @@ public function __unset($name)
293296
*
294297
* @param mixed $method The table to relate to
295298
* @param mixed $args
296-
* @throws \Exception
299+
* @throws Exception
297300
*
298301
* @return Model
299302
*/
300303
public function __call($method, $args)
301304
{
302305
if (!class_exists('Leaf\App')) {
303-
throw new \Exception('Relations are only available in Leaf apps.');
306+
throw new Exception('Relations are only available in Leaf apps.');
304307
}
305308

306309
return (new Model([

src/Auth/UsesRoles.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Leaf\Auth;
44

5+
use Throwable;
6+
57
/**
68
* Functionality for user permissions
79
* ----
@@ -46,7 +48,7 @@ public function assign($role): bool
4648
])
4749
->where(Config::get('id.key'), $this->data[Config::get('id.key')])
4850
->execute();
49-
} catch (\Throwable $th) {
51+
} catch (Throwable $th) {
5052
return false;
5153
}
5254

src/Auth/UsesSubscriptions.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Leaf\Auth;
44

5+
use Leaf\Billing\Subscription;
6+
57
/**
68
* Functionality for user subscriptions
79
* ----
@@ -50,7 +52,7 @@ public function subscription(): ?array
5052
*/
5153
public function hasSubscription(): bool
5254
{
53-
return $this->subscription() && $this->subscription['status'] !== \Leaf\Billing\Subscription::STATUS_CANCELLED;
55+
return $this->subscription() && $this->subscription['status'] !== Subscription::STATUS_CANCELLED;
5456
}
5557

5658
/**
@@ -59,7 +61,7 @@ public function hasSubscription(): bool
5961
*/
6062
public function hasActiveSubscription(): bool
6163
{
62-
return $this->subscription() && ($this->subscription['status'] === \Leaf\Billing\Subscription::STATUS_ACTIVE || $this->subscription['status'] === \Leaf\Billing\Subscription::STATUS_TRIAL);
64+
return $this->subscription() && ($this->subscription['status'] === Subscription::STATUS_ACTIVE || $this->subscription['status'] === Subscription::STATUS_TRIAL);
6365
}
6466

6567
/**

src/functions.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
use Leaf\Auth;
4+
use Leaf\Config;
5+
36
if (!function_exists('auth') && class_exists('Leaf\App')) {
47
/**
58
* Return the leaf auth object
@@ -8,12 +11,12 @@
811
*/
912
function auth()
1013
{
11-
if (!(\Leaf\Config::getStatic('auth'))) {
12-
\Leaf\Config::singleton('auth', function () {
13-
return new \Leaf\Auth();
14+
if (!(Config::getStatic('auth'))) {
15+
Config::singleton('auth', function () {
16+
return new Auth();
1417
});
1518
}
1619

17-
return \Leaf\Config::get('auth');
20+
return Config::get('auth');
1821
}
1922
}

0 commit comments

Comments
 (0)