Skip to content

Commit 97db04b

Browse files
committed
add theme actions
1 parent c9eb7df commit 97db04b

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/Actions/FetchMainTheme.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Osiset\ShopifyApp\Actions;
6+
7+
use Illuminate\Support\Facades\Log;
8+
use Osiset\ShopifyApp\Contracts\ShopModel;
9+
10+
final class FetchMainTheme
11+
{
12+
/**
13+
* @return array{id: ?string, name: ?string}
14+
*/
15+
public function handle(ShopModel $shop): array
16+
{
17+
$response = $shop->api()->graph('{
18+
themes(first: 1, roles: MAIN) {
19+
nodes {
20+
id
21+
name
22+
}
23+
}
24+
}');
25+
26+
if (blank(data_get($response['body']->toArray(), 'data.themes.userErrors'))) {
27+
return data_get($response['body']->toArray(), 'data.themes.nodes.0', []);
28+
}
29+
30+
Log::error('Fetching main theme error: ' . json_encode(data_get($response['body']->toArray(), 'data.themes.userErrors')));
31+
32+
return [];
33+
}
34+
}

src/Actions/FetchThemeAssets.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Osiset\ShopifyApp\Actions;
6+
7+
use Illuminate\Support\Facades\Log;
8+
use Osiset\ShopifyApp\Contracts\ShopModel;
9+
10+
final class FetchThemeAssets
11+
{
12+
/**
13+
* @param array<int, array{filename: string, content: string}> $filenames
14+
*/
15+
public function handle(ShopModel $shop, string $mainThemeId, array $filenames): array
16+
{
17+
$response = $shop->api()->graph('query ($id: ID!, $filenames: [String!]) {
18+
theme(id: $id) {
19+
id
20+
name
21+
role
22+
files(filenames: $filenames) {
23+
nodes {
24+
filename
25+
body {
26+
... on OnlineStoreThemeFileBodyText {
27+
content
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}', [
34+
'id' => $mainThemeId,
35+
'filenames' => $filenames,
36+
]);
37+
38+
if (blank(data_get($response['body']->toArray(), 'data.theme.userErrors'))) {
39+
return array_map(fn (array $data) => [
40+
'filename' => $data['filename'],
41+
'content' => $data['body']['content'],
42+
], data_get($response['body']->toArray(), 'data.theme.files.nodes'));
43+
}
44+
45+
Log::error('Fetching settings data error: ' . json_encode(data_get($response['body']->toArray(), 'data.theme.userErrors')));
46+
47+
return [];
48+
}
49+
}

0 commit comments

Comments
 (0)