CompanyProfile   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 40
c 1
b 0
f 0
dl 0
loc 97
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A company() 0 3 1
A getFrontAttribute() 0 8 2
A getFilesAttribute() 0 9 2
A getBackAttribute() 0 8 2
1
<?php
2
3
namespace SimpleCMS\Company\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Collection;
8
use SimpleCMS\Framework\Contracts\SimpleMedia;
9
use SimpleCMS\Framework\Traits\MediaAttributeTrait;
10
use Illuminate\Database\Eloquent\Factories\HasFactory;
11
use Spatie\MediaLibrary\MediaCollections\Models\Media;
12
13
/**
14
 * CompanyProfile Model
15
 * @author Dennis Lui <[email protected]>
16
 *
17
 * @property string $id 主键
18
 * @property string $company_id 公司ID
19
 * @property string $id_number 企业注册号/身份证号码
20
 * @property string $name 企业名称
21
 * @property string $legal 法人/注册人
22
 * @property ?string $phone 联系电话
23
 * @property ?string $email 联系邮箱
24
 * @property ?string $address 注册地址
25
 * @property-read ?Carbon $created_at 创建时间
26
 * @property-read ?Carbon $updated_at 更新时间
27
 * @property-read ?Company $company 公司
28
 * @property-read ?Collection<Media> $media 附件
29
 * @property-read ?array<array<string,string>> $files 辅助资料
30
 * @property-read ?array<string,string> $front 证件正面
31
 * @property-read ?array<string,string> $back 证件反面
32
 */
33
class CompanyProfile extends Model implements SimpleMedia
34
{
35
    use HasFactory, MediaAttributeTrait;
36
    /**
37
     * Media Key
38
     */
39
    const MEDIA_FILE = 'file';
40
41
    /**
42
     * 证件正面
43
     * @var string
44
     */
45
    const MEDIA_FRONT = 'front';
46
47
    /**
48
     * 证件背面
49
     * @var string
50
     */
51
    const MEDIA_BACK = 'back';
52
53
    const HAS_ONE_MEDIA = ['front', 'back'];
54
55
    /**
56
     * 可输入字段
57
     */
58
    protected $fillable = [
59
        'id',
60
        'company_id',
61
        'id_number',
62
        'name',
63
        'legal',
64
        'phone',
65
        'email',
66
        'address',
67
    ];
68
69
    /**
70
     * 显示字段类型
71
     */
72
    public $casts = [
73
        'created_at' => 'datetime',
74
        'updated_at' => 'datetime'
75
    ];
76
77
    /**
78
     * 追加字段
79
     */
80
    public $appends = ['front', 'back', 'files'];
81
82
    /**
83
     * 隐藏关系
84
     */
85
    public $hidden = ['media', 'password'];
86
87
88
    public function getFilesAttribute()
89
    {
90
        if (!$medias = $this->getMedia(self::MEDIA_FILE))
91
            return [];
92
        return $medias->map(function ($item) {
93
            return [
94
                'name' => $item->file_name,
95
                'url' => $item->original_url,
96
                'uuid' => $item->uuid
97
            ];
98
        });
99
    }
100
101
    public function getBackAttribute()
102
    {
103
        if (!$media = $this->getFirstMedia(self::MEDIA_BACK))
104
            return [];
105
        return [
106
            'name' => $media->file_name,
107
            'url' => $media->original_url,
108
            'uuid' => $media->uuid
109
        ];
110
    }
111
112
    public function getFrontAttribute()
113
    {
114
        if (!$media = $this->getFirstMedia(self::MEDIA_FRONT))
115
            return [];
116
        return [
117
            'name' => $media->file_name,
118
            'url' => $media->original_url,
119
            'uuid' => $media->uuid
120
        ];
121
    }
122
123
    /**
124
     * 公司信息
125
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
126
     */
127
    public function company()
128
    {
129
        return $this->belongsTo(Company::class);
130
    }
131
}
132