Skip to content

Commit 5a92e65

Browse files
authored
Merge pull request #77 from aligent/feature/redirect-convenicence-method
Add redirect convenience method for products and categories
2 parents 18ee4d9 + 8773cd7 commit 5a92e65

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/BigCommerce/ResourceModels/Redirect/Redirect.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@
55
use BigCommerce\ApiV3\ResourceModels\ResourceModel;
66
use stdClass;
77

8+
/**
9+
* Redirect Resource
10+
*
11+
* Represents the request object for redirect objects.
12+
*
13+
* Contains a convenience method for simply redirecting to a product or category
14+
*
15+
* ```php
16+
* $api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']);
17+
*
18+
* $productRedirect = new Redirect();
19+
* $productRedirect->site_id = 1000;
20+
* $productRedirect->from_path = '/cool-product.html';
21+
* $productRedirect->toProduct(123);
22+
*
23+
* $api->redirects()->upsert([$productRedirect]);
24+
* ```
25+
*
26+
*/
827
class Redirect extends ResourceModel
928
{
1029
public string $from_path;
@@ -20,4 +39,14 @@ public function __construct(?stdClass $optionObject = null)
2039

2140
parent::__construct($optionObject);
2241
}
42+
43+
public function toProduct(int $productId): void
44+
{
45+
$this->to = RedirectTo::buildRedirectTo(RedirectTo::TYPE__PRODUCT, $productId);
46+
}
47+
48+
public function toCategory(int $categoryId): void
49+
{
50+
$this->to = RedirectTo::buildRedirectTo(RedirectTo::TYPE__CATEGORY, $categoryId);
51+
}
2352
}

src/BigCommerce/ResourceModels/Redirect/RedirectTo.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,15 @@ class RedirectTo extends ResourceModel
1515

1616
public string $type;
1717
public ?int $entity_id;
18-
public string $url;
18+
public ?string $url;
19+
20+
public static function buildRedirectTo(string $type, ?int $entityId = null, ?string $url = null): RedirectTo
21+
{
22+
$redirectTo = new self();
23+
$redirectTo->type = $type;
24+
$redirectTo->entity_id = $entityId;
25+
$redirectTo->url = $url;
26+
27+
return $redirectTo;
28+
}
1929
}

tests/BigCommerce/Api/Redirects/RedirectsApiTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public function testCanUpsertRedirects()
1414
$redirect = new Redirect();
1515
$redirect->from_path = '/old-url';
1616
$redirect->site_id = 0;
17-
$redirect->to = new RedirectTo();
18-
$redirect->to->type = RedirectTo::TYPE__PRODUCT;
19-
$redirect->to->entity_id = 0;
20-
$redirect->to->url = '/new-url';
17+
$redirect->toProduct(1);
18+
19+
$this->assertEquals(RedirectTo::TYPE__PRODUCT, $redirect->to->type);
20+
$this->assertEquals(1, $redirect->to->entity_id);
2121

2222
$this->getApi()->redirects()->upsert([$redirect]);
2323
$this->assertEquals('storefront/redirects', $this->getLastRequestPath());

0 commit comments

Comments
 (0)