Skip to content

Commit

Permalink
Merge pull request HubSpot#11 from HubSpot/sdk-factory
Browse files Browse the repository at this point in the history
Move logic of auto-discovering to a Factory Base class
  • Loading branch information
atanasiuk-hubspot authored and GitHub Enterprise committed Dec 18, 2019
2 parents 5286c18 + ff1a04a commit 170bb3f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 56 deletions.
39 changes: 39 additions & 0 deletions lib/Factory/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace HubSpot\Factory;

use GuzzleHttp\Client;
use HubSpot\Client\Crm\Owners\Configuration;
use ReflectionClass;
use ReflectionException;

class Base
{
/** @var Client */
protected $client;

/** @var Configuration */
protected $config;

public function __construct($client, $config)
{
$this->client = $client;
$this->config = $config;
}

/**
* @param string $name
* @param mixed $args
*
* @throws ReflectionException
*
* @return mixed
*/
public function __call($name, $args)
{
$className = (new ReflectionClass(get_class($this)))->getShortName();
$resource = '\\HubSpot\\Client\\Crm\\'.$className.'\\Api\\'.ucfirst($name);

return new $resource($this->client, $this->config);
}
}
30 changes: 1 addition & 29 deletions lib/Factory/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,19 @@

namespace HubSpot\Factory;

use GuzzleHttp\Client;
use HubSpot\Client\Crm\Objects\Api\AssociationsApi;
use HubSpot\Client\Crm\Objects\Api\BasicApi;
use HubSpot\Client\Crm\Objects\Api\BatchApi;
use HubSpot\Client\Crm\Objects\Api\CreateNativeObjectsApi;
use HubSpot\Client\Crm\Objects\Api\SearchApi;
use HubSpot\Client\Crm\Objects\Configuration;

/**
* Class Objects.
*
* @method AssociationsApi associationsApi()
* @method BasicApi basicApi
* @method BatchApi batchApi()
* @method CreateNativeObjectsApi createNativeObjectsApi
* @method SearchApi searchApi()
*/
class Objects
class Objects extends Base
{
/** @var Client */
protected $client;

/** @var Configuration */
protected $config;

public function __construct($client, $config)
{
$this->client = $client;
$this->config = $config;
}

/**
* @param string $name
* @param mixed $args
*
* @return mixed
*/
public function __call($name, $args)
{
$resource = '\\HubSpot\\Client\\Crm\\Objects\\Api\\'.ucfirst($name);

return new $resource($this->client, $this->config);
}
}
28 changes: 1 addition & 27 deletions lib/Factory/Owners.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,11 @@

namespace HubSpot\Factory;

use GuzzleHttp\Client;
use HubSpot\Client\Crm\Owners\Api\DefaultApi;
use HubSpot\Client\Crm\Owners\Configuration;

/**
* @method DefaultApi defaultApi()
*/
class Owners
class Owners extends Base
{
/** @var Client */
protected $client;

/** @var Configuration */
protected $config;

public function __construct($client, $config)
{
$this->client = $client;
$this->config = $config;
}

/**
* @param string $name
* @param mixed $args
*
* @return mixed
*/
public function __call($name, $args)
{
$resource = '\\HubSpot\\Client\\Crm\\Owners\\Api\\'.ucfirst($name);

return new $resource($this->client, $this->config);
}
}

0 comments on commit 170bb3f

Please sign in to comment.