Skip to content

Commit 17be2af

Browse files
authored
Merge pull request #5 from eiling-io/batteryincluded-php-sdk-3
#3 - add browse service and extension classes for handling various ex…
2 parents a8e104a + 5ed232c commit 17be2af

12 files changed

+275
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use BatteryIncludedSdk\Client\ApiClient;
5+
use BatteryIncludedSdk\Client\CurlHttpClient;
6+
use BatteryIncludedSdk\Shop\BrowseSearchStruct;
7+
use BatteryIncludedSdk\Shop\BrowseService;
8+
9+
require_once __DIR__ . '/../../vendor/autoload.php';
10+
require_once __DIR__ . '/../credentials.php';
11+
12+
$apiClient = new ApiClient(
13+
new CurlHttpClient(),
14+
'https://api.batteryincluded.io/api/v1/collections/',
15+
$collection,
16+
$apiKey
17+
);
18+
19+
$syncService = new BrowseService($apiClient);
20+
$searchStruct = new BrowseSearchStruct();
21+
$searchStruct->setQuery('extension');
22+
$result = $syncService->browse($searchStruct);
23+
24+
echo '<pre>';
25+
print_r($result->getAllExtensions());
26+
echo '</pre>';
27+
exit;

src/Shop/BrowseResponse.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,36 @@
55
namespace BatteryIncludedSdk\Shop;
66

77
use BatteryIncludedSdk\Service\Response;
8+
use BatteryIncludedSdk\Shop\Extension\AbstractExtension;
89

910
class BrowseResponse extends Response
1011
{
12+
private array $extensions = [];
13+
private array $teaserExtensions = [];
14+
private array $redirectsExtension = [];
15+
private array $codesExtension = [];
16+
private array $promotionsExtension = [];
17+
1118
public function __construct(string $responseRaw, private BrowseSearchStruct $searchStruct)
1219
{
1320
parent::__construct($responseRaw);
21+
22+
foreach ($this->getBody()['extensions'] as $extensionData) {
23+
switch ($extensionData['type']) {
24+
case 'teaser':
25+
$this->teaserExtensions[] = $this->extensions[] = new Extension\TeaserExtension($extensionData['data']);
26+
break;
27+
case 'redirects':
28+
$this->redirectsExtension[] = $this->extensions[] = new Extension\RedirectsExtension($extensionData['data']);
29+
break;
30+
case 'codes':
31+
$this->codesExtension[] = $this->extensions[] = new Extension\CodesExtension($extensionData['data']);
32+
break;
33+
case 'promotions':
34+
$this->promotionsExtension[] = $this->extensions[] = new Extension\PromotionsExtension($extensionData['data']);
35+
break;
36+
}
37+
}
1438
}
1539

1640
public function getHits(): array
@@ -48,6 +72,34 @@ public function getFacets(string $categoryField = '_PRODUCT.categories'): array
4872
return $result;
4973
}
5074

75+
/**
76+
* @return AbstractExtension[]
77+
*/
78+
public function getAllExtensions(): array
79+
{
80+
return $this->extensions;
81+
}
82+
83+
public function getTeaserExtensions(): array
84+
{
85+
return $this->teaserExtensions;
86+
}
87+
88+
public function getRedirectsExtensions(): array
89+
{
90+
return $this->redirectsExtension;
91+
}
92+
93+
public function getCodesExtensions(): array
94+
{
95+
return $this->codesExtension;
96+
}
97+
98+
public function getPromotionsExtensions(): array
99+
{
100+
return $this->promotionsExtension;
101+
}
102+
51103
public function getFound(): int
52104
{
53105
return (int) $this->getBody()['found'];
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdk\Shop\Extension;
6+
7+
abstract class AbstractExtension
8+
{
9+
public function __construct(protected array $data)
10+
{
11+
}
12+
13+
public function getData(): array
14+
{
15+
return $this->data;
16+
}
17+
18+
abstract public function getType(): string;
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdk\Shop\Extension;
6+
7+
class CodesExtension extends AbstractExtension
8+
{
9+
public function getType(): string
10+
{
11+
return 'codes';
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdk\Shop\Extension;
6+
7+
class PromotionsExtension extends AbstractExtension
8+
{
9+
public function getType(): string
10+
{
11+
return 'promotions';
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdk\Shop\Extension;
6+
7+
class RedirectsExtension extends AbstractExtension
8+
{
9+
public function getType(): string
10+
{
11+
return 'redirects';
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdk\Shop\Extension;
6+
7+
class TeaserExtension extends AbstractExtension
8+
{
9+
public function getType(): string
10+
{
11+
return 'teaser';
12+
}
13+
}

tests/Shop/BrowseServiceTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
use BatteryIncludedSdk\Shop\BrowseResponse;
1616
use BatteryIncludedSdk\Shop\BrowseSearchStruct;
1717
use BatteryIncludedSdk\Shop\BrowseService;
18+
use BatteryIncludedSdk\Shop\Extension\CodesExtension;
19+
use BatteryIncludedSdk\Shop\Extension\PromotionsExtension;
20+
use BatteryIncludedSdk\Shop\Extension\RedirectsExtension;
21+
use BatteryIncludedSdk\Shop\Extension\TeaserExtension;
1822
use BatteryIncludedSdk\Shop\FacetCategoryDto;
1923
use BatteryIncludedSdk\Shop\FacetDto;
2024
use BatteryIncludedSdk\Shop\FacetRangeDto;
@@ -31,6 +35,10 @@
3135
#[CoversClass(ApiClient::class)]
3236
#[CoversClass(CurlHttpClient::class)]
3337
#[CoversClass(BrowseSearchStruct::class)]
38+
#[CoversClass(CodesExtension::class)]
39+
#[CoversClass(PromotionsExtension::class)]
40+
#[CoversClass(RedirectsExtension::class)]
41+
#[CoversClass(TeaserExtension::class)]
3442
#[CoversClass(Response::class)]
3543
#[UsesClass(CategoryDto::class)]
3644
#[UsesClass(FacetDto::class)]
@@ -103,4 +111,33 @@ public function testBrowseMethodWithPresetAgainstLiveApi()
103111
$this->assertEquals($result->getPage(), 1);
104112
$this->assertEquals($result->getPages(), 1);
105113
}
114+
115+
public function testBrowseMethodWithExtensionsAgainstLiveApi()
116+
{
117+
$products = Helper::generateProducts(20);
118+
$apiClient = Helper::getApiClient();
119+
$syncService = new SyncService($apiClient);
120+
121+
$result = $syncService->syncOneOrManyElements(...$products);
122+
$this->assertCount(720, $result->getBody());
123+
$browseService = new BrowseService(Helper::getApiClient());
124+
$searchStruct = new BrowseSearchStruct();
125+
$searchStruct->setQuery('extension');
126+
$result = $browseService->browse($searchStruct);
127+
128+
$this->assertContainsOnlyInstancesOf(FacetDto::class, $result->getFacets());
129+
130+
$this->assertInstanceOf(BrowseResponse::class, $result);
131+
$this->assertCount(0, $result->getHits());
132+
$this->assertCount(4, $result->getAllExtensions());
133+
$this->assertCount(1, $result->getCodesExtensions());
134+
$this->assertCount(1, $result->getRedirectsExtensions());
135+
$this->assertCount(1, $result->getPromotionsExtensions());
136+
$this->assertCount(1, $result->getTeaserExtensions());
137+
138+
$this->assertEquals(0, $result->getFound());
139+
140+
$this->assertEquals(1, $result->getPage());
141+
$this->assertEquals(0, $result->getPages());
142+
}
106143
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdkTests\Shop\Extension;
6+
7+
use BatteryIncludedSdk\Shop\Extension\AbstractExtension;
8+
use BatteryIncludedSdk\Shop\Extension\CodesExtension;
9+
use PHPUnit\Framework\Attributes\CoversClass;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[CoversClass(CodesExtension::class)]
13+
#[CoversClass(AbstractExtension::class)]
14+
class CodesExtensionTest extends TestCase
15+
{
16+
public function testExtensionInitialization()
17+
{
18+
$extension = new CodesExtension(['code1', 'code2']);
19+
$this->assertSame('codes', $extension->getType());
20+
$this->assertSame(['code1', 'code2'], $extension->getData());
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shop\Extension;
6+
7+
use BatteryIncludedSdk\Shop\Extension\AbstractExtension;
8+
use BatteryIncludedSdk\Shop\Extension\PromotionsExtension;
9+
use PHPUnit\Framework\Attributes\CoversClass;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[CoversClass(PromotionsExtension::class)]
13+
#[CoversClass(AbstractExtension::class)]
14+
class PromotionsExtensionTest extends TestCase
15+
{
16+
public function testExtensionInitialization()
17+
{
18+
$extension = new PromotionsExtension(['code1', 'code2']);
19+
$this->assertSame('promotions', $extension->getType());
20+
$this->assertSame(['code1', 'code2'], $extension->getData());
21+
}
22+
}

0 commit comments

Comments
 (0)