forked from livewire/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSponsor.php
118 lines (106 loc) · 4.05 KB
/
Sponsor.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Sushi\Sushi;
class Sponsor extends Model
{
use Sushi;
protected $keyType = 'string';
public function user()
{
return $this->hasOne(User::class, 'github_username', 'username');
}
public function getsScreencasts()
{
// If they sponsor for more than $8, they get access to screencasts.
return $this->tier_price_in_cents > 8 * 100;
}
public function getRows()
{
return Cache::remember('sponsors', now()->addHour(), function () {
return collect($this->fetchRawSponsors())
->map(function ($sponsor) {
return [
'id' => $sponsor['sponsorEntity']['id'],
'tier_id' => $sponsor['tier']['id'],
'tier_name' => $sponsor['tier']['name'],
'tier_description' => $sponsor['tier']['descriptionHTML'],
'tier_price' => $sponsor['tier']['monthlyPriceInDollars'],
'tier_price_in_cents' => $sponsor['tier']['monthlyPriceInCents'],
'username' => $sponsor['sponsorEntity']['login'],
'name' => $sponsor['sponsorEntity']['name'],
'email' => $sponsor['sponsorEntity']['email'],
'avatar' => $sponsor['sponsorEntity']['avatarUrl'],
'location' => $sponsor['sponsorEntity']['location'],
'website' => $sponsor['sponsorEntity']['websiteUrl'],
'created_at' => $sponsor['createdAt'],
'url' => $sponsor['sponsorEntity']['url'],
];
})
->toArray();
});
}
public function fetchRawSponsors($runningSponsors = [], $afterCursor = null) {
$afterCursor = json_encode($afterCursor);
$response = Http::withToken(
env('GITHUB_TOKEN')
)->post('https://api.github.com/graphql', [
'query' => <<<EOT
{
viewer {
sponsorshipsAsMaintainer(after: {$afterCursor}, first: 50, includePrivate: true) {
nodes {
id
tier {
id
descriptionHTML
monthlyPriceInDollars
monthlyPriceInCents
name
}
sponsorEntity {
... on Organization {
avatarUrl
email
id
login
name
url
location
websiteUrl
}
... on User {
avatarUrl
email
id
login
name
url
location
websiteUrl
}
}
createdAt
}
totalCount
pageInfo {
hasNextPage
endCursor
}
}
}
}
EOT,
]);
$sponsors = $response['data']['viewer']['sponsorshipsAsMaintainer']['nodes'];
$hasNextPage = $response['data']['viewer']['sponsorshipsAsMaintainer']['pageInfo']['hasNextPage'];
$endCursor = $response['data']['viewer']['sponsorshipsAsMaintainer']['pageInfo']['endCursor'];
$allSponsors = array_merge($runningSponsors, $sponsors);
if (! $hasNextPage) {
return $allSponsors;
}
return $this->fetchRawSponsors($allSponsors, $endCursor);
}
}