Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ A Magento 2 API Object Oriented wrapper for a Laravel application.
- [Products](#products)
- [Schema](#schema)
- [Source Items](#source-items)
- [Custom modules](#custom modules)


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

### Custom modules
Magento modules can have their own API endpoints.
For example:
```xml
<route method="POST" url="/V1/my-custom-endpoint/save">
...
</route>
<route method="GET" url="/V1/my-custom-endpoint/get/:id">
...
</route>
```
To use these you can directly use get/post methods:
```php
$magento->api('my-custom-endpoint')->post('save', [...]);
```
```php
$magento->api('my-custom-endpoint')->get('get/1');
```

<a id="schema"></a>
### Schema

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

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
41 changes: 41 additions & 0 deletions src/Api/Custom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Grayloon\Magento\Api;

use Grayloon\Magento\Magento;

class Custom extends AbstractApi
{
/**
* @var string Magento API endpoint
*/
protected $endpoint;

/**
* Custom constructor.
*
* @param string $endpoint
*/
public function __construct(string $endpoint, Magento $magento)
{
$this->endpoint = $endpoint;

parent::__construct($magento);
}

/**
* Dynamic call to passthrough.
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
if ($method == 'get' || $method == 'post') {
$args[0] = rtrim($this->endpoint, '/').'/'.$args[0];
}

return call_user_func_array([$this, $method], $args);
}
}
14 changes: 11 additions & 3 deletions src/Magento.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Grayloon\Magento;

use Grayloon\Magento\Api\Custom;
use Illuminate\Support\Str;
use InvalidArgumentException;

Expand Down Expand Up @@ -49,6 +50,12 @@ class Magento
*/
public $storeCode;

/**
* Magento constructor.
*
* @param string $baseUrl
* @param string $token
*/
public function __construct($baseUrl = null, $token = null)
{
$this->baseUrl = $baseUrl ?: config('magento.base_url');
Expand All @@ -68,12 +75,13 @@ public function __construct($baseUrl = null, $token = null)
*/
public function api($name)
{
$apiMethodExists = class_exists($name = "\Grayloon\Magento\Api\\".Str::ucfirst($name));
$className = $name;
$apiMethodExists = class_exists($className = "\Grayloon\Magento\Api\\".Str::ucfirst($className));

if (! $apiMethodExists) {
throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name));
return new Custom($name, $this);
}

return new $name($this);
return new $className($this);
}
}
47 changes: 47 additions & 0 deletions tests/Api/CustomTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Grayloon\Magento\Tests;

use Grayloon\Magento\Magento;
use Illuminate\Support\Facades\Http;

class CustomTest extends TestCase
{
public function test_can_get_custom_endpoint()
{
Http::fake([
'*rest/all/V1/foo/bar' => Http::response([], 200),
]);

$magento = new Magento();
$customApi = $magento->api('/foo')->get('bar');

$this->assertTrue($customApi->ok());
}

public function test_can_post_custom_endpoint()
{
Http::fake([
'*rest/all/V1/foo/bar' => Http::response([], 200),
]);

$magento = new Magento();
$customApi = $magento->api('/foo')->post('bar');

$this->assertTrue($customApi->ok());
}

public function test_custom_post_endpoint_passes_params()
{
Http::fake([
'*rest/all/V1/foo/bar' => Http::response([], 200, ['baz' => 'test']),
]);

$magento = new Magento();
$customApi = $magento->api('/foo')->post('bar', [
'baz' => 'test',
]);

$this->assertTrue($customApi->ok());
}
}