Test Failed
Push — feature-laravel-5.4 ( 556e60...89a18e )
by Kirill
03:48
created

Docs::getTitleAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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\Support\Str;
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Contracts\Support\Arrayable;
14
use Illuminate\Database\Eloquent\Relations\HasMany;
15
use Service\DocsImporter\DocsConnectionConfigInterface;
16
17
/**
18
 * Class Docs.
19
 */
20
class Docs extends Model
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $table = 'docs';
26
27
    /**
28
     * @var array
29
     */
30
    protected $casts = [
31
        'importer_config' => 'collection',
32
    ];
33
34
    /**
35
     * @return HasMany
36
     */
37
    public function pages(): HasMany
38
    {
39
        return $this->hasMany(DocsPage::class);
40
    }
41
42
    /**
43
     * @param DocsConnectionConfigInterface|array $config
44
     */
45
    public function setImporterConfigAttribute($config)
46
    {
47
        $this->attributes['importer_config'] = $config instanceof Arrayable
48
            ? $config->toArray()
49
            : json_encode($config);
50
    }
51
52
    /**
53
     * @param  string $data
54
     * @return array
55
     */
56
    public function getImporterConfigAttribute(string $data): array
57
    {
58
        if ($data) {
59
            return json_decode($data, true);
60
        }
61
62
        return [];
63
    }
64
}
65