Skip to content

Commit

Permalink
Changed 'HasLicenses' Api
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercd100 committed Sep 18, 2017
1 parent a135f29 commit bb20170
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 65 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `laravel-licenser` will be documented in this file.

### 2.0.0
- Changed 'HasLicenses' Api

### 1.1.0
- Can now automatically add licenses when checking if there are any available

Expand Down
107 changes: 107 additions & 0 deletions src/HasLicenses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Tylercd100\License;

use Tylercd100\License\License;

trait HasLicenses
{
/**
* Attempt to allocate unused licenses.
*
* @param string $class The License class you want to work with
* @param int $quantity The amount of licenses you want to attempt to use
* @param boolean $add If true then it will increase the maximum available licenses
* @return self
*/
public function licensesAllocate($class, $quantity, $add = false)
{
$license = $this->getLicenseInstance($class);
$license->check($quantity, $add);
return $this;
}

/**
* Returns the amount of unused licenses.
*
* @param string $class The License class you want to work with
* @return int
*/
public function licensesRemaining($class)
{
$license = $this->getLicenseInstance($class);
return $license->remaining();
}

/**
* Returns the amount of used licenses
*
* @param string $class The License class you want to work with
* @return int
*/
public function licensesUsed($class)
{
$license = $this->getLicenseInstance($class);
return $license->used();
}

/**
* Increase the maximum amount of licenses
*
* @param string $class The License class you want to work with
* @param integer $quantity
* @return self
*/
public function licensesAdd($class, $quantity = 1)
{
// Add quantity of licenses
$license = $this->getLicenseInstance($class);
$license->add($quantity);
return $this;
}

/**
* Decreases the maximum amount of licenses available
*
* @param string $class The License class you want to work with
* @param integer $quantity
* @return self
*/
public function licensesSub($class, $quantity = 1)
{
// Subtract quantity of licenses
$license = $this->getLicenseInstance($class);
$license->sub($quantity);
return $this;
}

/**
* Sets the maximum amount of licenses to a specific value
*
* @param string $class The License class you want to work with
* @param integer $quantity
* @return self
*/
public function licensesSet($class, $quantity)
{
// Set the quantity of licenses
$license = $this->getLicenseInstance($class);
$license->set($quantity);
return $this;
}

/**
* Creates an instance of License from the supplied classname string
*
* @param string $class The License class you want to work with
* @return License
*/
private function getLicenseInstance($class)
{
$license = new $class($this);
if (!($license instanceof License)) {
throw LicenseException("Expected ".get_class($license)." to be an instanceof ".License::class);
}
return $license;
}
}
2 changes: 1 addition & 1 deletion src/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Tylercd100\License\Exceptions\LicenseException;
use Tylercd100\License\Models\License as LicenseModel;
use Tylercd100\License\Traits\HasLicenses;
use Tylercd100\License\HasLicenses;

abstract class License
{
Expand Down
64 changes: 0 additions & 64 deletions src/Traits/HasLicenses.php

This file was deleted.

0 comments on commit bb20170

Please sign in to comment.