/
HelpersTrait.php
101 lines (82 loc) · 2.92 KB
/
HelpersTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
use App\Models\User;
use App\Models\UserProfile;
use Faker\Factory as Faker;
trait HelpersTrait
{
/**
* Keep the array with unique data, to avoid query the db multiple times.
*
* @var array
*/
protected $uniqueUserValues;
/**
* @return Faker
*/
protected function getFakerWithUniqueUserData()
{
// Prepare an array with user data already used
$users = User::all();
if (!$this->uniqueUserValues) {
$uniques = ['username' => [], 'email' => []];
foreach ($users as $user) {
array_push($uniques['username'], [$user->username => null]);
array_push($uniques['email'], [$user->email => null]);
}
$this->uniqueUserValues = $uniques;
}
// As the array of already used faker data is protected in Faker and
// has no accessor method, we'll rely on ReflectionObject to modify
// the property before letting faker generate data.
//though reflected object should be added to the faker itself somehow
// which is hacky
//so we decided to overcome it with bindTo hack
$faker = Faker::create();
$unique = $faker->unique();
$binder = function ($value) {
$this->uniques = $value;
};
$uniqueBinder = $binder->bindTo($unique, $unique);
$uniqueBinder($this->uniqueUserValues);
return $faker;
}
/**
* @param array $attributes
* @param int $times
*
* @return User|void
*/
protected function createUser(array $attributes = [], $times = 1)
{
for ($i = 0; $i < $times; $i++) {
$faker = $this->getFakerWithUniqueUserData();
$default = [
'email' => $faker->unique()->email,
'username' => $faker->unique()->username,
'user_type' => 'admin'
];
$attr = array_merge($default, $attributes);
$user = factory(User::class)->create($attr);
$user->setProfile(factory(UserProfile::class)->make());
}
if ($times === 1) {
return $user;
} else {
return;
}
}
protected function tokenFromUser($user, $customClaims = [])
{
$cfg = $this->app->config->get('jwt');
$validator = new Tymon\JWTAuth\Validators\PayloadValidator;
$request = new Illuminate\Http\Request;
$claimFactory = new Tymon\JWTAuth\Claims\Factory;
$adapter = new App\Extensions\JWTAuth\NamshiAdapter($cfg['secret'], $cfg['algo']);
$payloadFactory = new App\Extensions\JWTAuth\PayloadFactory($claimFactory, $request, $validator);
$claims = ['sub' => $user->user_id, '_user' => $user];
$claims = array_merge($customClaims, $claims);
$payload = $payloadFactory->make($claims);
$token = $adapter->encode($payload->get());
return $token;
}
}