akaunting/app/Abstracts/Factory.php

80 lines
1.8 KiB
PHP
Raw Normal View History

2020-10-14 17:07:59 +03:00
<?php
namespace App\Abstracts;
use App\Models\Auth\User;
2020-12-24 01:28:38 +03:00
use App\Models\Common\Company;
2020-10-14 17:07:59 +03:00
use App\Traits\Jobs;
2022-06-03 17:18:01 +03:00
use App\Utilities\Date;
2020-10-14 17:07:59 +03:00
use Illuminate\Database\Eloquent\Factories\Factory as BaseFactory;
2020-12-24 01:28:38 +03:00
use Illuminate\Database\Eloquent\Model as EloquentModel;
2022-06-03 17:18:01 +03:00
use Illuminate\Support\Facades\Cache;
2020-10-14 17:07:59 +03:00
abstract class Factory extends BaseFactory
{
use Jobs;
2020-12-24 01:28:38 +03:00
/**
* @var Company
*/
protected $company;
/**
* @var User|EloquentModel|object|null
*/
protected $user;
2020-10-16 18:28:10 +03:00
public function __construct(...$arguments)
2020-10-14 17:07:59 +03:00
{
2020-10-16 18:28:10 +03:00
parent::__construct(...$arguments);
2020-10-15 00:27:40 +03:00
2021-05-28 11:21:41 +03:00
config(['mail.default' => 'array']);
2022-06-01 10:15:55 +03:00
// TODO: this location was put to make US | for "gmail.co.uk" issue
$this->faker = \Faker\Factory::create();
$this->setUser();
$this->setCompany();
}
public function getCompanyUsers(): array
{
return $this->company->users()->enabled()->get()->pluck('id')->toArray();
}
public function company(int $id): static
{
2022-06-03 17:18:01 +03:00
Cache::put('factory_company_id', $id, Date::now()->addHour(6));
2022-06-01 10:15:55 +03:00
return $this->state([
'company_id' => $id,
]);
}
public function setUser(): void
{
2020-10-14 17:07:59 +03:00
$this->user = User::first();
2022-06-01 10:15:55 +03:00
}
2020-10-14 17:07:59 +03:00
2022-06-01 10:15:55 +03:00
public function setCompany(): void
{
2022-06-03 17:18:01 +03:00
if (is_null(Cache::get('factory_company_id'))) {
$this->company = $this->user->companies()->first();
} else {
$this->company = company(Cache::get('factory_company_id'));
}
2022-06-01 10:15:55 +03:00
$this->company->makeCurrent();
2021-05-28 11:21:41 +03:00
app('url')->defaults(['company_id' => company_id()]);
2020-10-14 17:07:59 +03:00
}
2022-06-01 10:15:55 +03:00
public function getRawAttribute($key)
2020-10-14 17:07:59 +03:00
{
2022-06-01 10:15:55 +03:00
$raw = $this->state([])->getExpandedAttributes(null);
return $raw[$key] ?? null;
2020-10-14 17:07:59 +03:00
}
}