Skip to content

Commit

Permalink
Added inventory commands
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Jan 16, 2015
1 parent 446828f commit 62ad0d4
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
composer.phar
composer.lock
.DS_Store

tests/_output/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Here is a list of method's along with their exceptions that they can throw.

### NoUserLoggedInException

Occurs when a user ID cannot be retrieved from Sentry or built in Auth driver
Occurs when a user ID cannot be retrieved from Sentry, Sentinel, or built in Auth driver

### StockAlreadyExistsException

Expand Down
17 changes: 17 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
colors: false
memory_limit: 1024M
modules:
config:
Db:
dsn: ''
user: ''
password: ''
dump: tests/_data/dump.sql
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"illuminate/support": "4.*",
"baum/baum": "~1.0"
},
"require-dev": {
"codeception/codeception": "2.*"
},
"suggest": {
"venturecraft/revisionable": "Allows the tracking of inventory stock location and category changes"
},
Expand Down
41 changes: 41 additions & 0 deletions src/Stevebauman/Inventory/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Stevebauman\Inventory\Commands;

use Illuminate\Console\Command;

class InstallCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'inventory:install';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Runs the inventory migrations';

/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->info('Checking Database Schema');

$this->call('inventory:check-schema');

$this->info('Running migrations');

$this->call('inventory:run-migrations');

$this->info('Inventory has been successfully installed');
}

}
28 changes: 28 additions & 0 deletions src/Stevebauman/Inventory/Commands/RunMigrationsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Stevebauman\Inventory\Commands;

use Illuminate\Console\Command;

class RunMigrationsCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'inventory:run-migrations';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Runs the inventory migrations';

public function fire()
{
$this->call('migrate', array('--env' => $this->option('env'), '--vendor' => 'stevebauman/maintenance' ) );
}

}
117 changes: 117 additions & 0 deletions src/Stevebauman/Inventory/Commands/SchemaCheckCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Stevebauman\Inventory\Commands;

use Illuminate\Support\Facades\Schema;
use Stevebauman\Maintenance\Exceptions\Commands\DatabaseTableReservedException;
use Stevebauman\Maintenance\Exceptions\Commands\DependencyNotFoundException;
use Illuminate\Console\Command;

class SchemaCheckCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'inventory:check-schema';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Checks the current database to make sure the required tables are present, and the reserved tables are not';

/*
* Holds the database tables that must be present before install
*/
protected $dependencies = array(
'users' => 'Sentry, Sentinel or Laravel Auth',
);

/*
* Holds the required database tables necessary to install
*/
protected $reserved = array(
'inventory',
'inventory_stocks',
'inventory_stock_movements',
);

/**
* Executes the console command
*
* @throws DatabaseTableReservedException
* @throws DependencyNotFoundException
*/
public function fire()
{
if($this->checkDependencies()) {
$this->info('Schema dependencies are all good!');
}

if($this->checkReserved()) {
$this->info('Schema reserved tables are all good!');
}
}

/**
* Checks the current database for dependencies
*
* @return bool
* @throws DependencyNotFoundException
*/
private function checkDependencies()
{
foreach($this->dependencies as $table => $suppliedBy) {

if(!$this->tableExists($table)) {

if (!$this->confirmInstallDependency($suppliedBy)) {

$message = sprintf('Table: %s does not exist, it is supplied by %s', $table, $suppliedBy);

throw new DependencyNotFoundException($message);

}

}

}

return true;
}

/**
* Checks the current database for reserved tables
*
* @return bool
* @throws DatabaseTableReservedException
*/
private function checkReserved()
{
foreach($this->reserved as $table) {

if($this->tableExists($table)) {

$message = sprintf('Table: %s already exists. This table is reserved. Please remove the database table to continue', $table);

throw new DatabaseTableReservedException($message);

}

}

return true;
}

/**
* @param string $table
* @return boolean
*/
private function tableExists($table)
{
return Schema::hasTable($table);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace Stevebauman\Inventory\Exceptions\Commands;


class DatabaseTableReservedException extends \Exception {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace Stevebauman\Maintenance\Exceptions\Commands;


class DependencyNotFoundException extends \Exception {}
18 changes: 18 additions & 0 deletions src/Stevebauman/Inventory/InventoryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ public function boot()
*/
public function register()
{
$this->app->bind('inventory:install', function(){
return new Commands\InstallCommand();
});

$this->app->bind('inventory:check-schema', function(){
return new Commands\SchemaCheckCommand();
});

$this->app->bind('inventory:run-migrations', function(){
return new Commands\RunMigrationsCommand();
});

$this->commands(array(
'inventory:install',
'inventory:check-schema',
'inventory:run-migrations',
));

include __DIR__ .'/../../helpers.php';
}

Expand Down
Empty file removed tests/.gitkeep
Empty file.

0 comments on commit 62ad0d4

Please sign in to comment.