forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddonsQuery.php
86 lines (67 loc) · 2 KB
/
AddonsQuery.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
<?php
namespace Statamic\Marketplace;
use Facades\Statamic\Marketplace\Client;
use Illuminate\Pagination\Paginator;
use Statamic\Extensions\Pagination\LengthAwarePaginator;
use Statamic\Facades\Addon;
class AddonsQuery
{
protected $search;
protected $installed = false;
protected $page = 1;
public function search($search)
{
$this->search = $search;
return $this;
}
public function page($page)
{
$this->page = $page;
return $this;
}
public function installed(bool $installed)
{
$this->installed = $installed;
return $this;
}
public function get()
{
$installed = $this->installedProducts();
$params = [
'page' => $this->page,
'search' => $this->search,
'filter' => ['statamic' => '3,4,5'],
'sort' => 'most-popular',
'perPage' => 12,
];
if ($this->installed) {
if ($installed->isEmpty()) {
return ['data' => [], 'meta' => ['total' => 0, 'per_page' => 15]];
}
$params['filter']['products'] = $installed->join(',');
}
$response = Client::get('addons', $params);
$response['data'] = collect($response['data'])->map(function ($addon) use ($installed) {
return $addon + [
'installed' => $isInstalled = $installed->contains($addon['id']),
'edition' => $isInstalled ? Addon::get($addon['package'])->edition() : null,
];
})->all();
return $response;
}
public function paginate()
{
$response = $this->get();
return new LengthAwarePaginator(
$response['data'],
$response['meta']['total'],
$response['meta']['per_page'],
$this->page,
['path' => Paginator::resolveCurrentPath()]
);
}
private function installedProducts()
{
return Addon::all()->map->marketplaceId()->filter();
}
}