diff --git a/README.md b/README.md
index adc8063..4093c18 100644
--- a/README.md
+++ b/README.md
@@ -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
@@ -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
+
+ ...
+
+
+ ...
+
+```
+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');
+```
+
### Schema
@@ -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.
\ No newline at end of file
+The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
diff --git a/src/Api/Custom.php b/src/Api/Custom.php
new file mode 100644
index 0000000..a28f819
--- /dev/null
+++ b/src/Api/Custom.php
@@ -0,0 +1,41 @@
+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);
+ }
+}
diff --git a/src/Magento.php b/src/Magento.php
index 03a8e3c..9eebb36 100644
--- a/src/Magento.php
+++ b/src/Magento.php
@@ -2,6 +2,7 @@
namespace Grayloon\Magento;
+use Grayloon\Magento\Api\Custom;
use Illuminate\Support\Str;
use InvalidArgumentException;
@@ -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');
@@ -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);
}
}
diff --git a/tests/Api/CustomTest.php b/tests/Api/CustomTest.php
new file mode 100644
index 0000000..8d2f3f2
--- /dev/null
+++ b/tests/Api/CustomTest.php
@@ -0,0 +1,47 @@
+ 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());
+ }
+}