/
Factory.php
74 lines (55 loc) · 1.59 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace App\Abstracts;
use App\Models\Auth\User;
use App\Models\Common\Company;
use App\Traits\Jobs;
use Closure;
use Illuminate\Database\Eloquent\Factories\Factory as BaseFactory;
use Illuminate\Database\Eloquent\Model as EloquentModel;
abstract class Factory extends BaseFactory
{
use Jobs;
/**
* @var Company
*/
protected $company;
/**
* @var User|EloquentModel|object|null
*/
protected $user;
public function __construct(...$arguments)
{
parent::__construct(...$arguments);
config(['mail.default' => 'array']);
// 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
{
return $this->state([
'company_id' => $id,
]);
}
public function setUser(): void
{
$this->user = User::first();
}
public function setCompany(): void
{
$company_id = $this->getRawAttribute('company_id');
$this->company = !empty($company_id) ? Company::find($company_id) : $this->user->companies()->first();
$this->company->makeCurrent();
app('url')->defaults(['company_id' => company_id()]);
}
public function getRawAttribute($key)
{
$raw = $this->state([])->getExpandedAttributes(null);
return $raw[$key] ?? null;
}
}