Use factories to create default models

This commit is contained in:
David Bomba 2019-01-28 09:13:12 +11:00
parent b5687c91af
commit ce16c4d4c0
4 changed files with 118 additions and 14 deletions

View File

@ -0,0 +1,19 @@
<?php
namespace App\Factory;
use App\Models\ClientContact;
class ClientContactFactory
{
public static function create(int $company_id, int $user_id) :ClientContact
{
$client_contact = new ClientContact;
$client_contact->first_name = "";
$client_contact->user_id = $user_id;
$client_contact->company_id = $company_id;
$client_contact->id = 0;
return $client_contact;
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Factory;
use App\Models\Client;
class ClientFactory
{
public static function create(int $company_id, int $user_id) :Client
{
$client = new Client;
$client->company_id = $company_id;
$client->user_id = $user_id;
$client->name = '';
$client->website = '';
$client->private_notes = '';
$client->balance = 0;
$client->paid_to_date = 0;
$client->country_id = 4;
$client->is_deleted = 0;
$client_contact = ClientContactFactory::create($company_id, $user_id);
$client->contacts->add($client_contact);
return $client;
}
}

View File

@ -19,10 +19,7 @@ use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesMenu;
use App\Utils\Traits\UserSessionAttributes;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Yajra\DataTables\Facades\DataTables;
use Yajra\DataTables\Html\Builder;
use App\Factory\ClientFactory;
/**
* Class ClientController
@ -80,16 +77,7 @@ class ClientController extends Controller
*/
public function create(CreateClientRequest $request)
{
$client = new Client;
$client->name = '';
$client->company_id = $this->getCurrentCompanyId();
$client_contact = new ClientContact;
$client_contact->first_name = "";
$client_contact->user_id = auth()->user()->id;
$client_contact->company_id = $this->getCurrentCompanyId();
$client_contact->id = 0;
$client->contacts->add($client_contact);
$client = ClientFactory::create($this->getCurrentCompanyId(), auth()->user()->id);
$data = [
'client' => $client,

View File

@ -0,0 +1,70 @@
<?php
namespace App\Providers;
use Laravel\Telescope\Telescope;
use Illuminate\Support\Facades\Gate;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\TelescopeApplicationServiceProvider;
class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// Telescope::night();
$this->hideSensitiveRequestDetails();
Telescope::filter(function (IncomingEntry $entry) {
if ($this->app->isLocal()) {
return true;
}
return $entry->isReportableException() ||
$entry->isFailedJob() ||
$entry->isScheduledTask() ||
$entry->hasMonitoredTag();
});
}
/**
* Prevent sensitive request details from being logged by Telescope.
*
* @return void
*/
protected function hideSensitiveRequestDetails()
{
if ($this->app->isLocal()) {
return;
}
Telescope::hideRequestParameters(['_token']);
Telescope::hideRequestHeaders([
'cookie',
'x-csrf-token',
'x-xsrf-token',
]);
}
/**
* Register the Telescope gate.
*
* This gate determines who can access Telescope in non-local environments.
*
* @return void
*/
protected function gate()
{
Gate::define('viewTelescope', function ($user) {
return in_array($user->email, [
//
]);
});
}
}