getHeartBeat() public method

Retrieves the heartbeat class that offers the option to check if the VIES service is up-and-running.
public getHeartBeat ( ) : HeartBeat
return HeartBeat
 /**
  * @return JsonModel
  */
 public function checkVatAction()
 {
     $financialId = (int) $this->getEvent()->getRequest()->getPost()->get('financialId');
     /**
      * @var $financial Financial
      */
     $financial = $this->getOrganisationService()->findEntityById('financial', $financialId);
     if (is_null($financial->getVat()) && is_null($this->getEvent()->getRequest()->getPost()->get('vat'))) {
         return new JsonModel(['success' => 'error', 'result' => $this->translate("txt-vat-number-empty")]);
     }
     //Overrule the vat when a VAT number is sent via the URL
     if (!is_null($this->getEvent()->getRequest()->getPost()->get('vat'))) {
         $vat = $this->getEvent()->getRequest()->getPost()->get('vat');
     } else {
         $vat = $financial->getVat();
     }
     $vies = new Vies();
     if (false === $vies->getHeartBeat()->isAlive()) {
         return new JsonModel(['success' => 'error', 'result' => 'Service is not available at the moment, please try again later.']);
     } else {
         try {
             $result = $vies->validateVat($financial->getOrganisation()->getCountry()->getCd(), trim(str_replace($financial->getOrganisation()->getCountry()->getCd(), '', $vat)));
             if ($result->isValid()) {
                 //Update the financial
                 $financial->setVatStatus(Financial::VAT_STATUS_VALID);
                 $financial->setDateVat(new \DateTime());
                 $this->getOrganisationService()->updateEntity($financial);
                 return new JsonModel(['success' => 'success', 'result' => 'Valid', 'status' => Financial::VAT_STATUS_VALID]);
             } else {
                 //Update the financial
                 $financial->setVatStatus(Financial::VAT_STATUS_INVALID);
                 $financial->setDateVat(new \DateTime());
                 $this->getOrganisationService()->updateEntity($financial);
                 return new JsonModel(['success' => 'error', 'result' => 'Invalid', 'status' => Financial::VAT_STATUS_INVALID]);
             }
         } catch (\Exception $e) {
             return new JsonModel(['success' => 'error', 'result' => $e->getMessage(), 'status' => Financial::VAT_STATUS_UNDEFINED]);
         }
     }
 }
Beispiel #2
0
 /**
  * @covers \DragonBe\Vies\Vies::getHeartBeat
  */
 public function testGetDefaultHeartBeatWhenNoneSpecified()
 {
     $vies = new Vies();
     $hb = $vies->getHeartBeat();
     $this->assertInstanceOf('\\DragonBe\\Vies\\HeartBeat', $hb);
     $this->assertSame('tcp://' . Vies::VIES_DOMAIN, $hb->getHost());
     $this->assertSame(80, $hb->getPort());
 }
Beispiel #3
0
<?php

use DragonBe\Vies\Vies;
use DragonBe\Vies\ViesException;
use DragonBe\Vies\ViesServiceException;
require_once dirname(__DIR__) . '/vendor/autoload.php';
$vies = new Vies();
if (false === $vies->getHeartBeat()->isAlive()) {
    echo 'Back-end VIES service is not available at the moment, please try again later.' . PHP_EOL;
} else {
    $vatNumberProvider = [['BE' => '0811231190', 'HR' => '20649144807'], ['BE' => '1234567890', 'ES' => '9999999999'], ['AA' => '1234567890', 'NO' => '1234567890']];
    foreach ($vatNumberProvider as $vatNumbers) {
        foreach ($vatNumbers as $countryCode => $vatNumber) {
            echo 'Validating ' . $countryCode . $vatNumber . '... ';
            try {
                $result = $vies->validateVat($countryCode, $vatNumber);
                // - Validation routine worked as expected.
                echo $result->isValid() ? 'Valid' : 'Invalid';
                //
            } catch (ViesServiceException $e) {
                // - Recoverable exception.
                echo $e->getMessage();
                //   There is probably a temporary problem with back-end VIES service.
            } catch (ViesException $e) {
                // - Unrecoverable exception.
                echo $e->getMessage();
                //   Invalid country code etc.
            }
            echo PHP_EOL;
        }
    }