Skip to content

Commit 62ad0d4

Browse files
committed
Added inventory commands
1 parent 446828f commit 62ad0d4

File tree

11 files changed

+239
-1
lines changed

11 files changed

+239
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
composer.phar
33
composer.lock
44
.DS_Store
5+
6+
tests/_output/*

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Here is a list of method's along with their exceptions that they can throw.
160160

161161
### NoUserLoggedInException
162162

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

165165
### StockAlreadyExistsException
166166

codeception.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
actor: Tester
2+
paths:
3+
tests: tests
4+
log: tests/_output
5+
data: tests/_data
6+
helpers: tests/_support
7+
settings:
8+
bootstrap: _bootstrap.php
9+
colors: false
10+
memory_limit: 1024M
11+
modules:
12+
config:
13+
Db:
14+
dsn: ''
15+
user: ''
16+
password: ''
17+
dump: tests/_data/dump.sql

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"illuminate/support": "4.*",
1515
"baum/baum": "~1.0"
1616
},
17+
"require-dev": {
18+
"codeception/codeception": "2.*"
19+
},
1720
"suggest": {
1821
"venturecraft/revisionable": "Allows the tracking of inventory stock location and category changes"
1922
},
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Stevebauman\Inventory\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class InstallCommand extends Command {
8+
9+
/**
10+
* The console command name.
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'inventory:install';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Runs the inventory migrations';
22+
23+
/**
24+
* Execute the console command.
25+
*
26+
* @return mixed
27+
*/
28+
public function fire()
29+
{
30+
$this->info('Checking Database Schema');
31+
32+
$this->call('inventory:check-schema');
33+
34+
$this->info('Running migrations');
35+
36+
$this->call('inventory:run-migrations');
37+
38+
$this->info('Inventory has been successfully installed');
39+
}
40+
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Stevebauman\Inventory\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class RunMigrationsCommand extends Command {
8+
9+
/**
10+
* The console command name.
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'inventory:run-migrations';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Runs the inventory migrations';
22+
23+
public function fire()
24+
{
25+
$this->call('migrate', array('--env' => $this->option('env'), '--vendor' => 'stevebauman/maintenance' ) );
26+
}
27+
28+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Stevebauman\Inventory\Commands;
4+
5+
use Illuminate\Support\Facades\Schema;
6+
use Stevebauman\Maintenance\Exceptions\Commands\DatabaseTableReservedException;
7+
use Stevebauman\Maintenance\Exceptions\Commands\DependencyNotFoundException;
8+
use Illuminate\Console\Command;
9+
10+
class SchemaCheckCommand extends Command {
11+
12+
/**
13+
* The console command name.
14+
*
15+
* @var string
16+
*/
17+
protected $name = 'inventory:check-schema';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Checks the current database to make sure the required tables are present, and the reserved tables are not';
25+
26+
/*
27+
* Holds the database tables that must be present before install
28+
*/
29+
protected $dependencies = array(
30+
'users' => 'Sentry, Sentinel or Laravel Auth',
31+
);
32+
33+
/*
34+
* Holds the required database tables necessary to install
35+
*/
36+
protected $reserved = array(
37+
'inventory',
38+
'inventory_stocks',
39+
'inventory_stock_movements',
40+
);
41+
42+
/**
43+
* Executes the console command
44+
*
45+
* @throws DatabaseTableReservedException
46+
* @throws DependencyNotFoundException
47+
*/
48+
public function fire()
49+
{
50+
if($this->checkDependencies()) {
51+
$this->info('Schema dependencies are all good!');
52+
}
53+
54+
if($this->checkReserved()) {
55+
$this->info('Schema reserved tables are all good!');
56+
}
57+
}
58+
59+
/**
60+
* Checks the current database for dependencies
61+
*
62+
* @return bool
63+
* @throws DependencyNotFoundException
64+
*/
65+
private function checkDependencies()
66+
{
67+
foreach($this->dependencies as $table => $suppliedBy) {
68+
69+
if(!$this->tableExists($table)) {
70+
71+
if (!$this->confirmInstallDependency($suppliedBy)) {
72+
73+
$message = sprintf('Table: %s does not exist, it is supplied by %s', $table, $suppliedBy);
74+
75+
throw new DependencyNotFoundException($message);
76+
77+
}
78+
79+
}
80+
81+
}
82+
83+
return true;
84+
}
85+
86+
/**
87+
* Checks the current database for reserved tables
88+
*
89+
* @return bool
90+
* @throws DatabaseTableReservedException
91+
*/
92+
private function checkReserved()
93+
{
94+
foreach($this->reserved as $table) {
95+
96+
if($this->tableExists($table)) {
97+
98+
$message = sprintf('Table: %s already exists. This table is reserved. Please remove the database table to continue', $table);
99+
100+
throw new DatabaseTableReservedException($message);
101+
102+
}
103+
104+
}
105+
106+
return true;
107+
}
108+
109+
/**
110+
* @param string $table
111+
* @return boolean
112+
*/
113+
private function tableExists($table)
114+
{
115+
return Schema::hasTable($table);
116+
}
117+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Stevebauman\Inventory\Exceptions\Commands;
4+
5+
6+
class DatabaseTableReservedException extends \Exception {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Stevebauman\Maintenance\Exceptions\Commands;
4+
5+
6+
class DependencyNotFoundException extends \Exception {}

src/Stevebauman/Inventory/InventoryServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ public function boot()
2828
*/
2929
public function register()
3030
{
31+
$this->app->bind('inventory:install', function(){
32+
return new Commands\InstallCommand();
33+
});
34+
35+
$this->app->bind('inventory:check-schema', function(){
36+
return new Commands\SchemaCheckCommand();
37+
});
38+
39+
$this->app->bind('inventory:run-migrations', function(){
40+
return new Commands\RunMigrationsCommand();
41+
});
42+
43+
$this->commands(array(
44+
'inventory:install',
45+
'inventory:check-schema',
46+
'inventory:run-migrations',
47+
));
48+
3149
include __DIR__ .'/../../helpers.php';
3250
}
3351

0 commit comments

Comments
 (0)