Skip to content

Commit 4de8c94

Browse files
authored
Merge pull request #1 from Happyr/category_media
Add support for Category and Media
2 parents f907a89 + 3ce21a2 commit 4de8c94

File tree

9 files changed

+342
-15
lines changed

9 files changed

+342
-15
lines changed

Api/WpClient.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ public function getMenu(string $slug): array
7474
return [];
7575
}
7676

77+
/**
78+
* Generic get.
79+
* @param string $uri example "/wp/v2/categories"
80+
*
81+
* @throws \Psr\Http\Client\ClientExceptionInterface
82+
*/
83+
public function getUri(string $uri): array
84+
{
85+
$request = $this->requestFactory->createRequest('GET', $this->baseUrl.$uri);
86+
$response = $this->httpClient->sendRequest($request);
87+
88+
$data = $this->jsonDecode($response);
89+
if (empty($data)) {
90+
return [];
91+
}
92+
93+
return $data;
94+
}
95+
7796
private function jsonDecode(ResponseInterface $response): array
7897
{
7998
$body = $response->getBody()->__toString();

Model/Category.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Happyr\WordpressBundle\Model;
6+
7+
class Category
8+
{
9+
/**
10+
* @var int
11+
*/
12+
private $id;
13+
14+
/**
15+
* @var int
16+
*/
17+
private $count;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $description;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $link;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $name;
33+
34+
/**
35+
* @var string
36+
*/
37+
private $slug;
38+
39+
/**
40+
* @var string
41+
*/
42+
private $taxonomy;
43+
44+
/**
45+
* @var int
46+
*/
47+
private $parentId;
48+
49+
public function __construct(array $data)
50+
{
51+
if (empty($data)) {
52+
return;
53+
}
54+
55+
$this->id = $data['id'];
56+
$this->count = $data['count'];
57+
$this->description = $data['description'];
58+
$this->link = $data['link'];
59+
$this->name = $data['name'];
60+
$this->slug = $data['slug'];
61+
$this->taxonomy = $data['taxonomy'];
62+
$this->parentId = $data['parentId'];
63+
}
64+
65+
/**
66+
* @return int
67+
*/
68+
public function getId()
69+
{
70+
return $this->id;
71+
}
72+
73+
/**
74+
* @return int
75+
*/
76+
public function getCount(): int
77+
{
78+
return $this->count;
79+
}
80+
81+
/**
82+
* @return string
83+
*/
84+
public function getDescription(): string
85+
{
86+
return $this->description;
87+
}
88+
89+
/**
90+
* @return string
91+
*/
92+
public function getLink(): string
93+
{
94+
return $this->link;
95+
}
96+
97+
/**
98+
* @return string
99+
*/
100+
public function getName(): string
101+
{
102+
return $this->name;
103+
}
104+
105+
/**
106+
* @return string
107+
*/
108+
public function getSlug(): string
109+
{
110+
return $this->slug;
111+
}
112+
113+
/**
114+
* @return string
115+
*/
116+
public function getTaxonomy(): string
117+
{
118+
return $this->taxonomy;
119+
}
120+
121+
public function getParentId(): int
122+
{
123+
return $this->parentId;
124+
}
125+
}

Model/Media.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Happyr\WordpressBundle\Model;
6+
7+
class Media
8+
{
9+
/**
10+
* @var int
11+
*/
12+
private $id;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $link;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $slug;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $sourceUrl;
28+
29+
public function __construct(array $data)
30+
{
31+
if (empty($data)) {
32+
return;
33+
}
34+
35+
$this->id = $data['id'];
36+
$this->link = $data['link'];
37+
$this->slug = $data['slug'];
38+
$this->sourceUrl = $data['source_url'];
39+
}
40+
41+
public function getId(): int
42+
{
43+
return $this->id;
44+
}
45+
46+
public function getLink(): string
47+
{
48+
return $this->link;
49+
}
50+
51+
public function getSlug(): string
52+
{
53+
return $this->slug;
54+
}
55+
56+
public function getSourceUrl(): string
57+
{
58+
return $this->sourceUrl;
59+
}
60+
}

Parser/CategoryParserInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Happyr\WordpressBundle\Parser;
6+
7+
use Happyr\WordpressBundle\Model\Category;
8+
9+
interface CategoryParserInterface
10+
{
11+
public function parseCategory(Category $category): void;
12+
}

Parser/MediaParserInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Happyr\WordpressBundle\Parser;
6+
7+
use Happyr\WordpressBundle\Model\Media;
8+
9+
interface MediaParserInterface
10+
{
11+
public function parseMedia(Media $media): void;
12+
}

Parser/MessageParser.php

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Happyr\WordpressBundle\Parser;
66

7+
use Happyr\WordpressBundle\Model\Category;
8+
use Happyr\WordpressBundle\Model\Media;
79
use Happyr\WordpressBundle\Model\Menu;
810
use Happyr\WordpressBundle\Model\Page;
911

@@ -14,24 +16,23 @@
1416
*/
1517
class MessageParser
1618
{
17-
/**
18-
* @var PageParserInterface[]
19-
*/
2019
private $pageParsers;
21-
22-
/**
23-
* @var MenuParserInterface[]
24-
*/
2520
private $menuParsers;
21+
private $mediaParsers;
22+
private $categoryParsers;
2623

2724
/**
28-
* @param PageParserInterface[] $pageParsers
29-
* @param MenuParserInterface[] $menuParsers
25+
* @param PageParserInterface[] $pageParsers
26+
* @param MenuParserInterface[] $menuParsers
27+
* @param MediaParserInterface[] $mediaParsers
28+
* @param CategoryParserInterface[] $categoryParsers
3029
*/
31-
public function __construct(iterable $pageParsers, iterable $menuParsers)
30+
public function __construct(iterable $pageParsers, iterable $menuParsers, iterable $mediaParsers, iterable $categoryParsers)
3231
{
3332
$this->pageParsers = $pageParsers;
3433
$this->menuParsers = $menuParsers;
34+
$this->mediaParsers = $mediaParsers;
35+
$this->categoryParsers = $categoryParsers;
3536
}
3637

3738
public function parsePage(array $data): ?Page
@@ -63,4 +64,46 @@ public function parseMenu(array $data): ?Menu
6364

6465
return $menu;
6566
}
67+
68+
/**
69+
* @return Category[]
70+
*/
71+
public function parseCategories(array $data): array
72+
{
73+
$collection = [];
74+
foreach ($data as $d) {
75+
try {
76+
$category = new Category($d);
77+
foreach ($this->categoryParsers as $parser) {
78+
$parser->parseCategory($category);
79+
}
80+
$collection[] = $category;
81+
} catch (\Throwable $t) {
82+
continue;
83+
}
84+
}
85+
86+
return $collection;
87+
}
88+
89+
/**
90+
* @return Media[]
91+
*/
92+
public function parseMedia(array $data): array
93+
{
94+
$collection = [];
95+
foreach ($data as $d) {
96+
try {
97+
$media = new Media($d);
98+
foreach ($this->mediaParsers as $parser) {
99+
$parser->parseMedia($media);
100+
}
101+
$collection[] = $media;
102+
} catch (\Throwable $t) {
103+
continue;
104+
}
105+
}
106+
107+
return $collection;
108+
}
66109
}

Resources/config/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services:
33
arguments: ['@Psr\Http\Client\ClientInterface', '@Psr\Http\Message\RequestFactoryInterface', '%happyr_wordpress.remote_url%']
44

55
Happyr\WordpressBundle\Parser\MessageParser:
6-
arguments: [!tagged happyr_wordpress.parser.page, !tagged happyr_wordpress.parser.menu]
6+
arguments: [!tagged happyr_wordpress.parser.page, !tagged happyr_wordpress.parser.menu, !tagged happyr_wordpress.parser.media, !tagged happyr_wordpress.parser.category]
77

88
Happyr\WordpressBundle\Service\Wordpress:
99
arguments: ['@Happyr\WordpressBundle\Api\WpClient', '@Happyr\WordpressBundle\Parser\MessageParser', ~, ~]

Service/Wordpress.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Happyr\WordpressBundle\Service;
66

77
use Happyr\WordpressBundle\Api\WpClient;
8+
use Happyr\WordpressBundle\Model\Category;
89
use Happyr\WordpressBundle\Model\Menu;
910
use Happyr\WordpressBundle\Model\Page;
1011
use Happyr\WordpressBundle\Parser\MessageParser;
@@ -91,6 +92,38 @@ public function getMenu(string $slug): ?Menu
9192
});
9293
}
9394

95+
public function getCategories(string $slug = ''): array
96+
{
97+
return $this->cache->get($this->getCacheKey('categories', $slug), function (/*ItemInterface*/ CacheItemInterface $item) use ($slug) {
98+
$data = $this->client->get('/wp/v2/categories'.$slug);
99+
if (!$this->isValidResponse($data)) {
100+
$item->expiresAfter(300);
101+
102+
return null;
103+
}
104+
105+
$item->expiresAfter($this->ttl);
106+
107+
return $this->messageParser->parseCategories($data);
108+
});
109+
}
110+
111+
public function getMedia(string $slug = ''): array
112+
{
113+
return $this->cache->get($this->getCacheKey('media', $slug), function (/*ItemInterface*/ CacheItemInterface $item) use ($slug) {
114+
$data = $this->client->get('/wp/v2/media'.$slug);
115+
if (!$this->isValidResponse($data)) {
116+
$item->expiresAfter(300);
117+
118+
return null;
119+
}
120+
121+
$item->expiresAfter($this->ttl);
122+
123+
return $this->messageParser->parseMedia($data);
124+
});
125+
}
126+
94127
/**
95128
* Purge cache for pages and menus.
96129
*/
@@ -114,11 +147,10 @@ private function getCacheKey(string $prefix, string $identifier): string
114147

115148
private function isValidResponse($data): bool
116149
{
117-
if (isset($data['code']) && isset($data['data']['status']) && $data['data']['status'] === 400) {
150+
if (isset($data['code']) && isset($data['data']['status']) && 400 === $data['data']['status']) {
118151
return false;
119152
}
120153

121154
return !empty($data);
122155
}
123-
124156
}

0 commit comments

Comments
 (0)