Skip to content

Commit

Permalink
Company observer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelnakamura committed Jul 21, 2022
1 parent febe2bd commit 5e81b12
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/IssSupernova.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bildvitta\IssSupernova;

use Bildvitta\IssSupernova\Resources\Companies;
use Bildvitta\IssSupernova\Resources\CustomerBankAccounts;
use Bildvitta\IssSupernova\Resources\CustomerCreditCards;
use Bildvitta\IssSupernova\Resources\CustomerDependents;
Expand Down Expand Up @@ -218,6 +219,11 @@ public function users()
return new Users($this);
}

public function companies()
{
return new Companies($this);
}

public function sales()
{
return new Sales($this);
Expand Down
74 changes: 74 additions & 0 deletions src/Observers/CompanyObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Bildvitta\IssSupernova\Observers;

use Bildvitta\IssSupernova\IssSupernova;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;

class CompanyObserver
{
public function created($company)
{
if (!Config::get('iss-supernova.base_uri')) {
return;
}

$company->loadMissing(
'domains',
'main_company'
);
if ($company->main_company) {
$company->main_company->loadMissing(
'domains'
);
}
$data = $company->toArray();
$data['sync_to'] = 'sys';

try {
$issSupernova = new IssSupernova();
$response = $issSupernova->companies()->create($data);
} catch (\Throwable $exception) {
Log::error($exception->getMessage());
throw $exception;
}

return $response;
}

public function updated($company)
{
if (!Config::get('iss-supernova.base_uri')) {
return;
}

$company->loadMissing(
'domains',
'main_company'
);
if ($company->main_company) {
$company->main_company->loadMissing(
'domains'
);
}
$data = $company->toArray();
$data['sync_to'] = 'sys';

try {
$issSupernova = new IssSupernova();
$response = $issSupernova->companies()->update($data);
} catch (\Throwable $exception) {
Log::error($exception->getMessage());
throw $exception;
}

return $response;
}

public function deleted($company)
{
$this->updated($company);
}
}
31 changes: 31 additions & 0 deletions src/Resources/Companies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bildvitta\IssSupernova\Resources;

use Bildvitta\IssSupernova\IssSupernova;

class Companies
{
private IssSupernova $issSupernova;

public function __construct(IssSupernova $issSupernova)
{
$this->issSupernova = $issSupernova;
}

public function create($data)
{
return $this->issSupernova->request->post(
'/companies',
$data
)->throw()->object();
}

public function update($data)
{
return $this->issSupernova->request->put(
'/companies',
$data
)->throw()->object();
}
}

0 comments on commit 5e81b12

Please sign in to comment.