Completed
Push — 0.3 ( da43ab...625b56 )
by Ben
96:17 queued 52:29
created

Module::viewkey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\Modules;
4
5
use Illuminate\Support\Collection;
6
use Thinktomorrow\Chief\Concerns\Viewable\NotFoundView;
7
use Thinktomorrow\Chief\Concerns\Viewable\Viewable;
8
use Thinktomorrow\Chief\Concerns\Viewable\ViewableContract;
9
use Thinktomorrow\Chief\Concerns\Viewable\ViewPath;
10
use Thinktomorrow\Chief\Pages\Page;
11
use Illuminate\Database\Eloquent\Model;
12
use Spatie\MediaLibrary\HasMedia\HasMedia;
13
use Thinktomorrow\Chief\Management\Managers;
14
use Illuminate\Database\Eloquent\SoftDeletes;
15
use Thinktomorrow\Chief\Relations\ActsAsChild;
16
use Thinktomorrow\Chief\Snippets\WithSnippets;
17
use Thinktomorrow\Chief\Fields\Types\HtmlField;
18
use Thinktomorrow\Chief\Relations\ActsAsParent;
19
use Thinktomorrow\Chief\Fields\Types\InputField;
20
use Thinktomorrow\Chief\Relations\ActingAsChild;
21
use Thinktomorrow\AssetLibrary\Traits\AssetTrait;
22
use Thinktomorrow\Chief\Concerns\Morphable\Morphable;
23
use Thinktomorrow\Chief\FlatReferences\FlatReference;
24
use Dimsav\Translatable\Translatable as BaseTranslatable;
25
use Thinktomorrow\Chief\Concerns\Translatable\Translatable;
26
use Thinktomorrow\Chief\Concerns\Morphable\MorphableContract;
27
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableContract;
28
29
class Module extends Model implements TranslatableContract, HasMedia, ActsAsChild, MorphableContract, ViewableContract
30
{
31
    use Morphable,
0 ignored issues
show
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 Dimsav\Translatable\Translatable requires some properties which are not provided by Thinktomorrow\Chief\Modules\Module: $translations, $localeKey
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...
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...
32
        AssetTrait,
33
        Translatable,
34
        BaseTranslatable,
35
        SoftDeletes,
36
        ActingAsChild,
37
        WithSnippets,
38
        Viewable;
39
40
    // Explicitly mention the translation model so on inheritance the child class uses the proper default translation model
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
41
    protected $translationModel = ModuleTranslation::class;
42
    protected $translationForeignKey = 'module_id';
43
    protected $translatedAttributes = [
44
        'title', 'content'
45
    ];
46
47
    public $useTranslationFallback = true;
48
    public $table = "modules";
49
    protected $guarded = [];
50
    protected $dates = ['deleted_at'];
51
    protected $with = ['translations'];
52
53
    protected $baseViewPath;
54
55 87
    public function __construct(array $attributes = [])
56
    {
57 87
        $this->constructWithSnippets();
58
59 87
        if (!isset($this->baseViewPath)) {
60 87
            $this->baseViewPath = config('thinktomorrow.chief.base-view-paths.modules', 'modules');
61
        }
62
63 87
        parent::__construct($attributes);
64 87
    }
65
66
    /**
67
     * Enlist all available managed modules.
68
     * @return Collection of ManagedModelDetails
69
     */
70 1
    public static function available(): Collection
71
    {
72 1
        return app(Managers::class)->findDetailsByTag('module');
73
    }
74
75 1
    public function page()
76
    {
77 1
        return $this->belongsTo(Page::class, 'page_id');
78
    }
79
80
    /**
81
     * The page specific ones are the text modules
82
     * which are added via the page builder
83
     *
84
     * @param $query
85
     */
86 1
    public function scopeWithoutPageSpecific($query)
87
    {
88 1
        $query->whereNull('page_id');
89 1
    }
90
91 26
    public function isPageSpecific(): bool
92
    {
93 26
        return !is_null($this->page_id);
94
    }
95
96
    /**
97
     * Each page / Module model can expose some custom fields. Add here the list of fields defined as name => Field where Field
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
98
     * is an instance of \Thinktomorrow\Chief\Fields\Types\Field
99
     *
100
     * @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...
101
     * @return array
102
     */
103
    public function customFields()
104
    {
105
        return [];
106
    }
107
108
    /**
109
     * Each module model can expose the managed translatable fields. These should be included as attributes just like the regular
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
110
     * translatable attributes. This method allows for easy installation of the form fields in chief.
111
     *
112
     * @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...
113
     * @return array
114
     */
115 8
    final public static function translatableFields($key = null)
116
    {
117 8
        $translatableFields = array_merge(static::defaultTranslatableFields(), static::customTranslatableFields());
118
119 8
        return $key ? array_pluck($translatableFields, $key) : $translatableFields;
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

119
        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...
introduced by
$key is of type null, thus it always evaluated to false.
Loading history...
120
    }
121
122
    /**
123
     * The custom addition of fields for a module model.
124
     *
125
     * To add a field, you should:
126
     * 1. override this method with your own and return the comprised list of fields.
127
     * 2. Setup the proper migrations and add the new field to the translatable values of the collection.
128
     *
129
     * @return array
130
     */
131 8
    public static function customTranslatableFields(): array
132
    {
133 8
        return [];
134
    }
135
136
    /**
137
     * The default set of fields for a module model.
138
     *
139
     * If you wish to remove any of these fields, you should:
140
     * 1. override this method with your own and return the comprised list of fields.
141
     * 2. Provide a migration to remove the column from database and remove the fields from the translatable values of the model.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
142
     *
143
     * @return array
144
     */
145
    public static function defaultTranslatableFields(): array
146
    {
147
        return [
148
            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

148
            InputField::make('title')->/** @scrutinizer ignore-call */ label('titel'),
Loading history...
149
            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

149
            HtmlField::make('content')->/** @scrutinizer ignore-call */ label('Inhoud'),
Loading history...
150
        ];
151
    }
152
153
    public function mediaUrls($type = null, $size = 'full'): Collection
154
    {
155
        return $this->getAllFiles($type)->map->getFileUrl($size);
156
    }
157
158
    public function mediaUrl($type = null, $size = 'full'): ?string
159
    {
160
        return $this->mediaUrls($type, $size)->first();
161
    }
162
163
    public static function mediaFields($key = null)
164
    {
165
        $types = [
166
//            MediaType::BACKGROUND => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
167
//                'type' => MediaType::BACKGROUND,
168
//                'label' => 'Achtergrond afbeelding',
169
//                'description' => '',
170
//            ]
171
        ];
172
173
        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

173
        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...
174
    }
175
176 3
    public static function findBySlug($slug)
177
    {
178 3
        return static::where('slug', $slug)->first();
179
    }
180
181 16
    public function flatReference(): FlatReference
182
    {
183 16
        return new FlatReference(static::class, $this->id);
184
    }
185
186 26
    public function flatReferenceLabel(): string
187
    {
188 26
        return $this->slug ?? '';
189
    }
190
191 1
    public function flatReferenceGroup(): string
192
    {
193 1
        $classKey = get_class($this);
194 1
        $labelSingular = property_exists($this, 'labelSingular') ? $this->labelSingular : str_singular($classKey);
0 ignored issues
show
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

194
        $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...
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...
195
196 1
        return $labelSingular;
197
    }
198
}
199