Total Complexity | 42 |
Total Lines | 259 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 2 | Features | 0 |
Complex classes like Page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Page, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Page extends Model implements ManagedModel, TranslatableContract, HasAsset, ActsAsParent, ActsAsChild, ActsAsMenuItem, MorphableContract, ViewableContract, ProvidesUrl, StatefulContract |
||
41 | { |
||
42 | use BaseTranslatable { |
||
|
|||
43 | getAttribute as getTranslatableAttribute; |
||
44 | } |
||
45 | |||
46 | use Morphable; |
||
47 | use AssetTrait; |
||
48 | use Translatable; |
||
49 | use SoftDeletes; |
||
50 | use Publishable; |
||
51 | use Featurable; |
||
52 | use Archivable; |
||
53 | use ActingAsParent; |
||
54 | use ActingAsChild; |
||
55 | use WithSnippets; |
||
56 | use ResolvingRoute; |
||
57 | use Viewable; |
||
58 | use HasFragments; |
||
59 | |||
60 | // Explicitly mention the translation model so on inheritance the child class uses the proper default translation model |
||
61 | protected $translationModel = PageTranslation::class; |
||
62 | protected $translationForeignKey = 'page_id'; |
||
63 | protected $translatedAttributes = [ |
||
64 | 'title', |
||
65 | 'content', |
||
66 | 'short', |
||
67 | 'seo_title', |
||
68 | 'seo_description', |
||
69 | 'seo_keywords', |
||
70 | 'seo_image', |
||
71 | ]; |
||
72 | |||
73 | public $table = "pages"; |
||
74 | protected $guarded = []; |
||
75 | protected $with = ['translations']; |
||
76 | |||
77 | protected $baseViewPath; |
||
78 | protected static $baseUrlSegment = '/'; |
||
79 | |||
80 | protected static $cachedUrls = []; |
||
81 | |||
82 | public static function clearCachedUrls() |
||
83 | { |
||
84 | static::$cachedUrls = null; |
||
85 | } |
||
86 | |||
87 | final public function __construct(array $attributes = []) |
||
88 | { |
||
89 | $this->constructWithSnippets(); |
||
90 | |||
91 | if (!isset($this->baseViewPath)) { |
||
92 | $this->baseViewPath = config('thinktomorrow.chief.base-view-paths.pages', 'pages'); |
||
93 | } |
||
94 | |||
95 | parent::__construct($attributes); |
||
96 | } |
||
97 | |||
98 | public static function managedModelKey(): string |
||
99 | { |
||
100 | if (isset(static::$managedModelKey)) { |
||
101 | return static::$managedModelKey; |
||
102 | } |
||
103 | |||
104 | throw new \Exception('Missing required static property \'managedModelKey\' on ' . static::class . '.'); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Parse and render any found snippets in custom |
||
109 | * or translatable attribute values. |
||
110 | * |
||
111 | * @param string $value |
||
112 | * @return mixed|null|string|string[] |
||
113 | */ |
||
114 | public function getAttribute($value) |
||
115 | { |
||
116 | $value = $this->getTranslatableAttribute($value); |
||
117 | |||
118 | if ($this->shouldParseWithSnippets($value)) { |
||
119 | $value = $this->parseWithSnippets($value); |
||
120 | } |
||
121 | |||
122 | return $value; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Page specific modules. We exclude text modules since they are modules in pure |
||
127 | * technical terms and not so much as behavioural elements for the admin. |
||
128 | * |
||
129 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
130 | */ |
||
131 | public function modules() |
||
132 | { |
||
133 | return $this->hasMany(Module::class, 'page_id')->where('morph_key', '<>', 'text'); |
||
134 | } |
||
135 | |||
136 | public function flatReference(): FlatReference |
||
137 | { |
||
138 | return new FlatReference(static::class, $this->id); |
||
139 | } |
||
140 | |||
141 | public function flatReferenceLabel(): string |
||
142 | { |
||
143 | if ($this->exists) { |
||
144 | $status = !$this->isPublished() ? ' [' . $this->statusAsPlainLabel() . ']' : null; |
||
145 | |||
146 | return $this->title ? $this->title . $status : ''; |
||
147 | } |
||
148 | |||
149 | return ''; |
||
150 | } |
||
151 | |||
152 | public function flatReferenceGroup(): string |
||
153 | { |
||
154 | $classKey = get_class($this); |
||
155 | if (property_exists($this, 'labelSingular')) { |
||
156 | $labelSingular = $this->labelSingular; |
||
157 | } else { |
||
158 | $labelSingular = Str::singular($classKey); |
||
159 | } |
||
160 | |||
161 | return $labelSingular; |
||
162 | } |
||
163 | |||
164 | public function mediaUrls($type = null, $size = 'full'): Collection |
||
173 | } |
||
174 | |||
175 | public function mediaUrl($type = null, $size = 'full'): ?string |
||
176 | { |
||
177 | return $this->mediaUrls($type, $size)->first(); |
||
178 | } |
||
179 | |||
180 | public static function findPublished($id) |
||
183 | } |
||
184 | |||
185 | public function scopeSortedByCreated($query) |
||
186 | { |
||
187 | $query->orderBy('created_at', 'DESC'); |
||
188 | } |
||
189 | |||
190 | /** @inheritdoc */ |
||
191 | public function url(string $locale = null): string |
||
192 | { |
||
193 | if (!$locale) { |
||
194 | $locale = app()->getLocale(); |
||
195 | } |
||
196 | try { |
||
197 | $memoizedKey = $this->getMorphClass() . '-' . $this->id . '-' . $locale; |
||
198 | |||
199 | if (isset(static::$cachedUrls[$memoizedKey])) { |
||
200 | return static::$cachedUrls[$memoizedKey]; |
||
201 | } |
||
202 | |||
203 | $slug = MemoizedUrlRecord::findByModel($this, $locale)->slug; |
||
204 | |||
205 | return static::$cachedUrls[$memoizedKey] = $this->resolveUrl($locale, [$slug]); |
||
206 | } catch (UrlRecordNotFound $e) { |
||
207 | return ''; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | public function resolveUrl(string $locale = null, $parameters = null): string |
||
216 | } |
||
217 | |||
218 | /** @inheritdoc */ |
||
219 | public function baseUrlSegment(string $locale = null): string |
||
220 | { |
||
221 | if (!isset(static::$baseUrlSegment)) { |
||
222 | return '/'; |
||
223 | } |
||
224 | |||
243 | } |
||
244 | |||
245 | public function menuLabel(): string |
||
246 | { |
||
247 | return $this->title ?? ''; |
||
248 | } |
||
249 | |||
250 | public function statusAsLabel() |
||
251 | { |
||
252 | if ($this->isPublished()) { |
||
253 | return '<a href="' . $this->url() . '" target="_blank"><em>online</em></a>'; |
||
254 | } |
||
255 | |||
256 | if ($this->isDraft()) { |
||
257 | return '<a href="' . $this->url() . '" target="_blank" class="text-error"><em>offline</em></a>'; |
||
258 | } |
||
259 | |||
260 | if ($this->isArchived()) { |
||
261 | return '<span><em>gearchiveerd</em></span>'; |
||
262 | } |
||
263 | |||
264 | return '-'; |
||
265 | } |
||
266 | |||
267 | public function statusAsPlainLabel() |
||
268 | { |
||
269 | if ($this->isPublished()) { |
||
270 | return 'online'; |
||
271 | } |
||
272 | |||
273 | if ($this->isDraft()) { |
||
274 | return 'offline'; |
||
275 | } |
||
276 | |||
277 | if ($this->isArchived()) { |
||
278 | return 'gearchiveerd'; |
||
279 | } |
||
280 | |||
281 | return '-'; |
||
282 | } |
||
283 | |||
284 | public function stateOf($key): string |
||
285 | { |
||
286 | return $this->$key ?? PageState::DRAFT; |
||
287 | } |
||
288 | |||
289 | public function changeStateOf($key, $state) |
||
299 | } |
||
300 | } |
||
301 |