Skip to content

Commit

Permalink
Add console command
Browse files Browse the repository at this point in the history
  • Loading branch information
David Vandemaele committed Mar 30, 2018
1 parent 995d393 commit ee97ac6
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
60 changes: 60 additions & 0 deletions console/validate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* validate-check:validate command for Skeleton Console
*
* @author David Vandemaele <[email protected]>
*/

namespace Skeleton\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Vat_Check_Validate extends \Skeleton\Console\Command {

/**
* Configure the Create command
*
* @access protected
*/
protected function configure() {
$this->setName('vat-check:validate');
$this->setDescription('Validate a specific VAT number and country vat code');
$this->addArgument('vat_code', InputArgument::REQUIRED, 'Country vat code');
$this->addArgument('vat_number', InputArgument::REQUIRED, 'VAT number');
$this->addOption('ignore_cache');
}

/**
* Execute the Command
*
* @access protected
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output) {
try {
$country = \Country::get_by_vat_code($input->getArgument('vat_code'));
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
return 1;
}

$ignore_cache = false;
if ($input->getOption('ignore_cache')) {
$ignore_cache = true;
}

$is_valid = \Skeleton\Vat\Check\Check::validate($input->getArgument('vat_number'), $country, $reason, $ignore_cache);
if ($is_valid) {
$output->writeln($input->getArgument('vat_code') . ' ' . $input->getArgument('vat_number') . " is valid \t" . ' <info>ok</info>');
} else {
$output->writeln($input->getArgument('vat_code') . ' ' . $input->getArgument('vat_number') . " is NOT valid (" . $reason . ") \t" . ' <error>nok</error>');
}

return 0;
}

}
34 changes: 25 additions & 9 deletions lib/Skeleton/Vat/Check/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,53 @@ class Check {
* @access public
* @param string $vat_number
* @param Country $country
* @param boolean $ignore_cache
* @return boolean $valid
*/
public static function validate($vat_number, \Country $country) {
public static function validate($vat_number, \Country $country, &$reason, $ignore_cache) {
// 1. Check syntax
if (!self::validate_syntax($vat_number, $country)) {
$reason = 'invalid syntax';
return false;
}

// 2. Check if the vat is a european vat-number
if ($country->european != 1) {
$reason = 'country is not european';
return true;
}

//3. Check if vat-number is in cache
try {
$vat_cache = Cache::get_by_vat_number_country($vat_number, $country);
if ($vat_cache->valid == 1) {
return true;
} else {
return false;
}
} catch (\Exception $e) {}
if ($ignore_cache === false) {
try {
$vat_cache = Cache::get_by_vat_number_country($vat_number, $country);
if ($vat_cache->valid == 1) {
$reason = 'from cache';
return true;
} else {
$reason = 'from cache';
return false;
}
} catch (\Exception $e) {}
}

//4. Check online (VIES-server)
$result = self::validate_online($vat_number, $country);
if ($result['save'] === true) {
try {
$vat_cache = Cache::get_by_vat_number_country($vat_number, $country);
$vat_cache->delete();
} catch (\Exception $e) {}

$vat_cache = new Cache();
$vat_cache->vat_number = $vat_number;
$vat_cache->country_id = $country->id;
$vat_cache->valid = $result['result'];
$vat_cache->save();
} elseif ($result['reachable'] === false) {
$reason = 'VIES service not reachable';
} else {
$reason = 'invalid vat number';
}

return $result['result'];
Expand Down

0 comments on commit ee97ac6

Please sign in to comment.