Passed
Push — ft/urls ( 5c631b...3903c5 )
by Ben
95:30 queued 50:10
created

Module::managedModelKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace Thinktomorrow\Chief\Modules;
4
5
use Illuminate\Support\Collection;
6
use Thinktomorrow\Chief\Concerns\Viewable\Viewable;
7
use Thinktomorrow\Chief\Concerns\Viewable\ViewableContract;
8
use Thinktomorrow\Chief\Management\ManagedModel;
9
use Thinktomorrow\Chief\Pages\Page;
10
use Illuminate\Database\Eloquent\Model;
11
use Spatie\MediaLibrary\HasMedia\HasMedia;
12
use Thinktomorrow\Chief\Management\Managers;
13
use Illuminate\Database\Eloquent\SoftDeletes;
14
use Thinktomorrow\Chief\Relations\ActsAsChild;
15
use Thinktomorrow\Chief\Snippets\WithSnippets;
16
use Thinktomorrow\Chief\Fields\Types\HtmlField;
17
use Thinktomorrow\Chief\Fields\Types\InputField;
18
use Thinktomorrow\Chief\Relations\ActingAsChild;
19
use Thinktomorrow\AssetLibrary\Traits\AssetTrait;
20
use Thinktomorrow\Chief\Concerns\Morphable\Morphable;
21
use Thinktomorrow\Chief\FlatReferences\FlatReference;
22
use Dimsav\Translatable\Translatable as BaseTranslatable;
23
use Thinktomorrow\Chief\Concerns\Translatable\Translatable;
24
use Thinktomorrow\Chief\Concerns\Morphable\MorphableContract;
25
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableContract;
26
27
class Module extends Model implements ManagedModel, TranslatableContract, HasMedia, ActsAsChild, MorphableContract, ViewableContract
28
{
29
    use Morphable,
0 ignored issues
show
introduced by
The trait Dimsav\Translatable\Translatable requires some properties which are not provided by Thinktomorrow\Chief\Modules\Module: $translations, $localeKey
Loading history...
Bug introduced by
The trait Thinktomorrow\Chief\Conc...anslatable\Translatable requires the property $translations which is not provided by Thinktomorrow\Chief\Modules\Module.
Loading history...
introduced by
The trait Thinktomorrow\Chief\Concerns\Morphable\Morphable requires some properties which are not provided by Thinktomorrow\Chief\Modules\Module: $translations, $morph_key
Loading history...
introduced by
The trait Thinktomorrow\Chief\Concerns\Viewable\Viewable requires some properties which are not provided by Thinktomorrow\Chief\Modules\Module: $content, $viewKey
Loading history...
introduced by
The trait Thinktomorrow\AssetLibrary\Traits\AssetTrait requires some properties which are not provided by Thinktomorrow\Chief\Modules\Module: $assets, $pivot, $each, $locale, $mediaConversionRegistrations, $media, $type, $collection_name
Loading history...
30
        AssetTrait,
31
        Translatable,
32
        BaseTranslatable,
33
        SoftDeletes,
34
        ActingAsChild,
35
        WithSnippets,
36
        Viewable;
37
38
    // Explicitly mention the translation model so on inheritance the child class uses the proper default translation model
39
    protected $translationModel = ModuleTranslation::class;
40
    protected $translationForeignKey = 'module_id';
41
    protected $translatedAttributes = [
42
        'title', 'content'
43
    ];
44
45
    public $useTranslationFallback = true;
46
    public $table = "modules";
47
    protected $guarded = [];
48
    protected $dates = ['deleted_at'];
49
    protected $with = ['translations'];
50
51
    protected $baseViewPath;
52
53 87
    public function __construct(array $attributes = [])
54
    {
55 87
        $this->constructWithSnippets();
56
57 87
        if (!isset($this->baseViewPath)) {
58 87
            $this->baseViewPath = config('thinktomorrow.chief.base-view-paths.modules', 'modules');
59
        }
60
61 87
        parent::__construct($attributes);
62 87
    }
63
64 433
    public static function managedModelKey(): string
65
    {
66 433
        if(isset(static::$managedModelKey)){
67 433
            return static::$managedModelKey;
0 ignored issues
show
Bug introduced by
The property managedModelKey does not seem to exist on Thinktomorrow\Chief\Modules\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
68
        }
69
70
        throw new \Exception('Missing required static property \'managedModelKey\' on ' . static::class. '.');
71
    }
72
73
    /**
74
     * Enlist all available managed modules.
75
     * @return Collection of ManagedModelDetails
76
     */
77 1
    public static function available(): Collection
78
    {
79 1
        return app(Managers::class)->findDetailsByTag('module');
80
    }
81
82 1
    public function page()
83
    {
84 1
        return $this->belongsTo(Page::class, 'page_id');
85
    }
86
87
    /**
88
     * The page specific ones are the text modules
89
     * which are added via the page builder
90
     *
91
     * @param $query
92
     */
93 1
    public function scopeWithoutPageSpecific($query)
94
    {
95 1
        $query->whereNull('page_id');
96 1
    }
97
98 26
    public function isPageSpecific(): bool
99
    {
100 26
        return !is_null($this->page_id);
101
    }
102
103
    /**
104
     * Each page / Module model can expose some custom fields. Add here the list of fields defined as name => Field where Field
105
     * is an instance of \Thinktomorrow\Chief\Fields\Types\Field
106
     *
107
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
108
     * @return array
109
     */
110
    public function customFields()
111
    {
112
        return [];
113
    }
114
115
    /**
116
     * Each module model can expose the managed translatable fields. These should be included as attributes just like the regular
117
     * translatable attributes. This method allows for easy installation of the form fields in chief.
118
     *
119
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
120
     * @return array
121
     */
122 8
    final public static function translatableFields($key = null)
123
    {
124 8
        $translatableFields = array_merge(static::defaultTranslatableFields(), static::customTranslatableFields());
125
126 8
        return $key ? array_pluck($translatableFields, $key) : $translatableFields;
0 ignored issues
show
introduced by
$key is of type null, thus it always evaluated to false.
Loading history...
Deprecated Code introduced by
The function array_pluck() has been deprecated: Arr::pluck() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

126
        return $key ? /** @scrutinizer ignore-deprecated */ array_pluck($translatableFields, $key) : $translatableFields;

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
127
    }
128
129
    /**
130
     * The custom addition of fields for a module model.
131
     *
132
     * To add a field, you should:
133
     * 1. override this method with your own and return the comprised list of fields.
134
     * 2. Setup the proper migrations and add the new field to the translatable values of the collection.
135
     *
136
     * @return array
137
     */
138 8
    public static function customTranslatableFields(): array
139
    {
140 8
        return [];
141
    }
142
143
    /**
144
     * The default set of fields for a module model.
145
     *
146
     * If you wish to remove any of these fields, you should:
147
     * 1. override this method with your own and return the comprised list of fields.
148
     * 2. Provide a migration to remove the column from database and remove the fields from the translatable values of the model.
149
     *
150
     * @return array
151
     */
152
    public static function defaultTranslatableFields(): array
153
    {
154
        return [
155
            InputField::make('title')->label('titel'),
0 ignored issues
show
Bug introduced by
The method label() does not exist on Thinktomorrow\Chief\Fields\Types\InputField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

155
            InputField::make('title')->/** @scrutinizer ignore-call */ label('titel'),
Loading history...
156
            HtmlField::make('content')->label('Inhoud'),
0 ignored issues
show
Bug introduced by
The method label() does not exist on Thinktomorrow\Chief\Fields\Types\HtmlField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
            HtmlField::make('content')->/** @scrutinizer ignore-call */ label('Inhoud'),
Loading history...
157
        ];
158
    }
159
160
    public function mediaUrls($type = null, $size = 'full'): Collection
161
    {
162
        return $this->getAllFiles($type)->map->getFileUrl($size);
163
    }
164
165
    public function mediaUrl($type = null, $size = 'full'): ?string
166
    {
167
        return $this->mediaUrls($type, $size)->first();
168
    }
169
170
    public static function mediaFields($key = null)
171
    {
172
        $types = [
173
//            MediaType::BACKGROUND => [
174
//                'type' => MediaType::BACKGROUND,
175
//                'label' => 'Achtergrond afbeelding',
176
//                'description' => '',
177
//            ]
178
        ];
179
180
        return $key ? array_pluck($types, $key) : $types;
0 ignored issues
show
Deprecated Code introduced by
The function array_pluck() has been deprecated: Arr::pluck() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

180
        return $key ? /** @scrutinizer ignore-deprecated */ array_pluck($types, $key) : $types;

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
181
    }
182
183 3
    public static function findBySlug($slug)
184
    {
185 3
        return static::where('slug', $slug)->first();
186
    }
187
188 16
    public function flatReference(): FlatReference
189
    {
190 16
        return new FlatReference(static::class, $this->id);
191
    }
192
193 26
    public function flatReferenceLabel(): string
194
    {
195 26
        return $this->slug ?? '';
196
    }
197
198 1
    public function flatReferenceGroup(): string
199
    {
200 1
        $classKey = get_class($this);
201 1
        $labelSingular = property_exists($this, 'labelSingular') ? $this->labelSingular : str_singular($classKey);
0 ignored issues
show
Bug introduced by
The property labelSingular does not seem to exist on Thinktomorrow\Chief\Modules\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

201
        $labelSingular = property_exists($this, 'labelSingular') ? $this->labelSingular : /** @scrutinizer ignore-deprecated */ str_singular($classKey);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
202
203 1
        return $labelSingular;
204
    }
205
}
206