Skip to content

Commit

Permalink
Optimized a bit and deleted duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Wycisk committed Jan 10, 2018
1 parent ad16e2c commit 6d5668a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 177 deletions.
29 changes: 0 additions & 29 deletions src/Controller/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,13 @@ class Address extends BaseController
protected $excludePing;
protected $editDate;

public function __construct(array $params = array())
{
$this->setParams($params);
}

protected static function transformParamsToIDs(array $params): array
{
$params = self::getAsObjectOrID($params, 'subnetId', ['subnet', 'subnetID'], Subnet::class);
$params = self::getAsObjectOrID($params, 'deviceId', ['device', 'deviceID'], Device::class);
return $params;
}

public static function getByID(int $id)
{
return new Address(self::_getStatic([$id])->getData());
}

public function getPing()
{
return $this->_get([$this->id, 'ping'])->getData();
Expand Down Expand Up @@ -140,20 +130,6 @@ public static function getAddressesByTag(int $id)
}
}

/**
* @param array $params
*
* @return Address
*/
public static function post(array $params): Address
{
$params = self::transformParamsToIDs($params);
$response = self::_postStatic([], $params);
$id = $response->getBody()['id'];

return Address::getByID($id);
}

public static function postFirstFree($subnet, array $params = array())
{
if($subnet instanceof Subnet){
Expand All @@ -166,11 +142,6 @@ public static function postFirstFree($subnet, array $params = array())
return Address::getByID($id);
}

public function delete()
{
return $this->_delete()->isSuccess();
}

public static function deleteByIPAndSubnet(string $ip, $subnet)
{
if($subnet instanceof Subnet){
Expand Down
17 changes: 9 additions & 8 deletions src/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public static function getByID(int $id)
public static function post(array $params)
{
//Params that could be converted from objects to id
$params = static::transformParamsToIDs($params);
$params = static::transformParamsToIDs($params);
$response = static::_postStatic([], $params);
$id = $response->getBody()['id'];
$id = $response->getBody()['id'];

return static::getByID($id);
}
Expand All @@ -66,7 +66,7 @@ public function patch(array $params = array())

public function delete()
{
return $this->_delete([], ['id' => $this->getId()]);
return $this->_delete([], ['id' => $this->getId()])->isSuccess();
}

public abstract function getId();
Expand Down Expand Up @@ -175,15 +175,16 @@ public static function getIDFromParams(array $params, string $key, array $possib
}

/**
* @param $value
* @param $class
* @param $value
* @param $class
* @param bool|null $asObject
*
* @return object|int|null
*/
protected static function getAsObjectOrID($value, $class, bool $asObject)
{
if(is_null($asObject)){
if (is_null($asObject))
{
$asObject = call_user_func(array($class, 'getDefaultAsObjectValue'));
}

Expand Down Expand Up @@ -225,14 +226,14 @@ protected function getDefaultAsObjectValue(): bool
return (bool) static::$defaultAsObject;
}

public function setDefaultAsObjectValue(bool $asObject){
public function setDefaultAsObjectValue(bool $asObject)
{
static::$defaultAsObject = $asObject;
}

protected abstract static function transformParamsToIDs(array $params): array;



protected static function convertSectionsToID(array $arr)
{
if (is_null($arr) or empty($arr))
Expand Down
29 changes: 29 additions & 0 deletions src/Controller/L2Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ public function setDescription(string $description)
return $this;
}

/**
* @param bool|null $asObject
*
* @return array
*/
public function getSections(bool $asObject = null)
{
$sections = [];
foreach ($this->sections as $section)
{
$sections[] = self::getAsObjectOrID($section, Section::class, $asObject);
}
$this->sections = $sections;

return $this->sections;
}

/**
* @param array $sections
*
* @return L2Domain
*/
public function setSections(array $sections)
{
$this->sections = $sections;

return $this;
}

/**
* @return string
*/
Expand Down
65 changes: 0 additions & 65 deletions src/Controller/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,52 +88,12 @@ class Section extends BaseController
*/
protected $DNS;

/**
* Section constructor.
*
* @param array $params
*/
public function __construct(array $params = array())
{
$this->setParams($params);
}

protected static function transformParamsToIDs(array $params): array
{
$params = self::getIDFromParams($params, 'masterSection', ['masterSectionId', 'masterSectionID'], Section::class);
return $params;
}

/**
* Returns all sections
* @return array
*/
public static function getAll()
{
$response = self::_getStatic();
$sections = array();
foreach ($response->getData() as $section)
{
$sections[] = new Section($section);
}

return $sections;
}

/**
* Returns specific section
*
* @param int $id
*
* @return Section
*/
public static function getByID(int $id)
{
$response = self::_getStatic([$id]);

return new Section($response->getData());
}

/**
* Returns all subnets in section
*/
Expand Down Expand Up @@ -199,31 +159,6 @@ public static function post(array $params): Section
return Section::getByName($params['name']);
}

/**
* Updates section
*
* @param array $params
*
* @return bool
*/
public function patch(array $params = array())
{
$this->setParams($params);
$params = $this->getParams();
$response = $this->_patch([], $params);

return $response->isSuccess();
}

/**
* Deletes section with all belonging subnets and addresses
* @return bool
*/
public function delete()
{
return $this->_delete([], ['id' => $this->getId()])->isSuccess();
}

/**
* @return int
*/
Expand Down
46 changes: 13 additions & 33 deletions src/Controller/Subnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ class Subnet extends BaseController
protected $location;
protected $editDate;


public function __construct(array $params = array())
{
$this->setParams($params);
}

protected static function transformParamsToIDs(array $params): array
{
//sectionId, linked_subnet, vlanId, vrfId, masterSubnetId
Expand All @@ -58,11 +52,21 @@ protected static function transformParamsToIDs(array $params): array
return $params;
}

public static function getByID(int $id)
public static function getAll()
{
$response = self::_getStatic([$id]);
$response = static::_getStatic(['all']);
if (is_null($response->getData()) or empty($response->getData()))
{
return [];
}
$objects = [];

return new Subnet($response->getData());
foreach ($response->getData() as $object)
{
$objects[] = new static($object);
}

return $objects;
}

public function getUsage()
Expand Down Expand Up @@ -152,17 +156,6 @@ public function getSearch(string $subnet)
return $this->_get(['search', $subnet])->getData();
}

public static function post(array $params = array())
{
//Params that could be converted from objects to id
$params = self::transformParamsToIDs($params);
$response = self::_postStatic([], $params);
$id = $response->getBody()['id'];

return Subnet::getByID($id);
}


public function postFirstSubnet(int $mask): Subnet
{
$response = $this->_post([$this->id, 'first_subnet', $mask]);
Expand All @@ -172,14 +165,6 @@ public function postFirstSubnet(int $mask): Subnet
return Subnet::getByID($id);
}

public function patch(array $params = array())
{
$this->setParams($params);
$params = $this->getParams();

return $this->_patch([], $params)->isSuccess();
}

public function patchResize(int $mask)
{
try
Expand Down Expand Up @@ -208,11 +193,6 @@ public function patchSplit(int $number)
return $this->_patch([$this->id, 'split'], ['number' => $number])->isSuccess();
}

public function delete()
{
return $this->_delete([$this->id])->isSuccess();
}

public function deleteTruncate()
{
return $this->_delete([$this->id, 'truncate'])->isSuccess();
Expand Down
43 changes: 1 addition & 42 deletions src/Controller/VLAN.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,6 @@ protected static function transformParamsToIDs(array $params): array
return $params;
}

public static function getAll()
{
$response = self::_get();

if (is_null($response->getData()) or empty($response->getData()))
{
return [];
}

$vlans = [];

foreach ($response->getData() as $vlan)
{
$vlans[] = new VLAN($vlan);
}

return $vlans;
}

public static function getByID(int $id)
{
return new VLAN(self::_get([$id])->getData());
}

public function getSubnets()
{
$response = $this->_get([$this->id, 'subnets']);
Expand Down Expand Up @@ -118,26 +94,9 @@ public function getSearch(int $number)
return $vlans;
}

public static function post(array $params)
{
$params = self::transformParamsToIDs($params);
$response = self::_postStatic([], $params);
$id = $response->getBody()['id'];

return VLAN::getByID($id);
}

public function patch(array $params = array())
{
$this->setParams($params);
$params = $this->getParams();

return $this->_patch([], $params)->isSuccess();
}

public function delete()
{
return $this->_delete([$this->id])->isSuccess();
return $this->_delete([], ['vlanId' => $this->getId()])->isSuccess();
}

/**
Expand Down

0 comments on commit 6d5668a

Please sign in to comment.