Docs::setImporterConfigAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\Models;
10
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Contracts\Support\Arrayable;
13
use Illuminate\Database\Eloquent\Relations\HasMany;
14
use Service\DocsImporter\DocsConnectionConfigInterface;
15
16
/**
17
 * Class Docs.
18
 */
19
class Docs extends Model
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $table = 'docs';
25
26
    /**
27
     * @var array
28
     */
29
    protected $casts = [
30
        'importer_config' => 'collection',
31
    ];
32
33
    /**
34
     * @return HasMany
35
     */
36
    public function pages(): HasMany
37
    {
38
        return $this->hasMany(DocsPage::class, 'docs_id', 'id');
39
    }
40
41
    /**
42
     * @param DocsConnectionConfigInterface|array $config
43
     */
44
    public function setImporterConfigAttribute($config)
45
    {
46
        $this->attributes['importer_config'] = $config instanceof Arrayable
47
            ? $config->toArray()
48
            : json_encode($config);
49
    }
50
51
    /**
52
     * @param  string $data
53
     * @return array
54
     */
55
    public function getImporterConfigAttribute(string $data): array
56
    {
57
        if ($data) {
58
            return json_decode($data, true);
59
        }
60
61
        return [];
62
    }
63
}
64