Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2f38e66

Browse files
authored
Merge pull request #34 from VincentBean/master
Feature: Add custom endpoints
2 parents afced87 + dfa59cb commit 2f38e66

File tree

4 files changed

+120
-4
lines changed

4 files changed

+120
-4
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ A Magento 2 API Object Oriented wrapper for a Laravel application.
2929
- [Products](#products)
3030
- [Schema](#schema)
3131
- [Source Items](#source-items)
32+
- [Custom modules](#custom modules)
3233

3334

3435
## Installation
@@ -223,6 +224,25 @@ Get info about a product by the product SKU:
223224
$magento->api('products')->show($sku);
224225
```
225226

227+
### Custom modules
228+
Magento modules can have their own API endpoints.
229+
For example:
230+
```xml
231+
<route method="POST" url="/V1/my-custom-endpoint/save">
232+
...
233+
</route>
234+
<route method="GET" url="/V1/my-custom-endpoint/get/:id">
235+
...
236+
</route>
237+
```
238+
To use these you can directly use get/post methods:
239+
```php
240+
$magento->api('my-custom-endpoint')->post('save', [...]);
241+
```
242+
```php
243+
$magento->api('my-custom-endpoint')->get('get/1');
244+
```
245+
226246
<a id="schema"></a>
227247
### Schema
228248

@@ -265,4 +285,4 @@ If you discover any security related issues, please email webmaster@grayloon.com
265285

266286
## License
267287

268-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
288+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

src/Api/Custom.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Grayloon\Magento\Api;
4+
5+
use Grayloon\Magento\Magento;
6+
7+
class Custom extends AbstractApi
8+
{
9+
/**
10+
* @var string Magento API endpoint
11+
*/
12+
protected $endpoint;
13+
14+
/**
15+
* Custom constructor.
16+
*
17+
* @param string $endpoint
18+
*/
19+
public function __construct(string $endpoint, Magento $magento)
20+
{
21+
$this->endpoint = $endpoint;
22+
23+
parent::__construct($magento);
24+
}
25+
26+
/**
27+
* Dynamic call to passthrough.
28+
*
29+
* @param string $method
30+
* @param array $args
31+
* @return mixed
32+
*/
33+
public function __call($method, $args)
34+
{
35+
if ($method == 'get' || $method == 'post') {
36+
$args[0] = rtrim($this->endpoint, '/').'/'.$args[0];
37+
}
38+
39+
return call_user_func_array([$this, $method], $args);
40+
}
41+
}

src/Magento.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Grayloon\Magento;
44

5+
use Grayloon\Magento\Api\Custom;
56
use Illuminate\Support\Str;
67
use InvalidArgumentException;
78

@@ -49,6 +50,12 @@ class Magento
4950
*/
5051
public $storeCode;
5152

53+
/**
54+
* Magento constructor.
55+
*
56+
* @param string $baseUrl
57+
* @param string $token
58+
*/
5259
public function __construct($baseUrl = null, $token = null)
5360
{
5461
$this->baseUrl = $baseUrl ?: config('magento.base_url');
@@ -68,12 +75,13 @@ public function __construct($baseUrl = null, $token = null)
6875
*/
6976
public function api($name)
7077
{
71-
$apiMethodExists = class_exists($name = "\Grayloon\Magento\Api\\".Str::ucfirst($name));
78+
$className = $name;
79+
$apiMethodExists = class_exists($className = "\Grayloon\Magento\Api\\".Str::ucfirst($className));
7280

7381
if (! $apiMethodExists) {
74-
throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name));
82+
return new Custom($name, $this);
7583
}
7684

77-
return new $name($this);
85+
return new $className($this);
7886
}
7987
}

tests/Api/CustomTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Grayloon\Magento\Tests;
4+
5+
use Grayloon\Magento\Magento;
6+
use Illuminate\Support\Facades\Http;
7+
8+
class CustomTest extends TestCase
9+
{
10+
public function test_can_get_custom_endpoint()
11+
{
12+
Http::fake([
13+
'*rest/all/V1/foo/bar' => Http::response([], 200),
14+
]);
15+
16+
$magento = new Magento();
17+
$customApi = $magento->api('/foo')->get('bar');
18+
19+
$this->assertTrue($customApi->ok());
20+
}
21+
22+
public function test_can_post_custom_endpoint()
23+
{
24+
Http::fake([
25+
'*rest/all/V1/foo/bar' => Http::response([], 200),
26+
]);
27+
28+
$magento = new Magento();
29+
$customApi = $magento->api('/foo')->post('bar');
30+
31+
$this->assertTrue($customApi->ok());
32+
}
33+
34+
public function test_custom_post_endpoint_passes_params()
35+
{
36+
Http::fake([
37+
'*rest/all/V1/foo/bar' => Http::response([], 200, ['baz' => 'test']),
38+
]);
39+
40+
$magento = new Magento();
41+
$customApi = $magento->api('/foo')->post('bar', [
42+
'baz' => 'test',
43+
]);
44+
45+
$this->assertTrue($customApi->ok());
46+
}
47+
}

0 commit comments

Comments
 (0)