| Total Complexity | 42 |
| Total Lines | 249 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | 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 |
||
| 35 | class Page extends Model implements ManagedModel, TranslatableContract, HasMedia, ActsAsParent, ActsAsChild, ActsAsMenuItem, MorphableContract, ViewableContract, ProvidesUrl |
||
| 36 | { |
||
| 37 | use BaseTranslatable { |
||
|
|
|||
| 38 | getAttribute as getTranslatableAttribute; |
||
| 39 | } |
||
| 40 | |||
| 41 | use Morphable, |
||
| 42 | AssetTrait, |
||
| 43 | Translatable, |
||
| 44 | SoftDeletes, |
||
| 45 | Publishable, |
||
| 46 | Featurable, |
||
| 47 | Archivable, |
||
| 48 | AuditTrait, |
||
| 49 | ActingAsParent, |
||
| 50 | ActingAsChild, |
||
| 51 | WithSnippets, |
||
| 52 | ResolvingRoute, |
||
| 53 | Viewable; |
||
| 54 | |||
| 55 | // Explicitly mention the translation model so on inheritance the child class uses the proper default translation model |
||
| 56 | protected $translationModel = PageTranslation::class; |
||
| 57 | protected $translationForeignKey = 'page_id'; |
||
| 58 | protected $translatedAttributes = [ |
||
| 59 | 'title', 'content', 'short', 'seo_title', 'seo_description', 'seo_keywords', 'seo_image' |
||
| 60 | ]; |
||
| 61 | |||
| 62 | public $table = "pages"; |
||
| 63 | protected $guarded = []; |
||
| 64 | protected $dates = ['deleted_at', 'archived_at']; |
||
| 65 | protected $with = ['translations']; |
||
| 66 | |||
| 67 | protected $baseViewPath; |
||
| 68 | protected static $baseUrlSegment = '/'; |
||
| 69 | |||
| 70 | public function __construct(array $attributes = []) |
||
| 71 | { |
||
| 72 | $this->constructWithSnippets(); |
||
| 73 | |||
| 74 | if (!isset($this->baseViewPath)) { |
||
| 75 | $this->baseViewPath = config('thinktomorrow.chief.base-view-paths.pages', 'pages'); |
||
| 76 | } |
||
| 77 | |||
| 78 | parent::__construct($attributes); |
||
| 79 | } |
||
| 80 | |||
| 81 | public static function managedModelKey(): string |
||
| 82 | { |
||
| 83 | if(isset(static::$managedModelKey)){ |
||
| 84 | return static::$managedModelKey; |
||
| 85 | } |
||
| 86 | |||
| 87 | throw new \Exception('Missing required static property \'managedModelKey\' on ' . static::class. '.'); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Parse and render any found snippets in custom |
||
| 92 | * or translatable attribute values. |
||
| 93 | * |
||
| 94 | * @param string $value |
||
| 95 | * @return mixed|null|string|string[] |
||
| 96 | */ |
||
| 97 | public function getAttribute($value) |
||
| 98 | { |
||
| 99 | $value = $this->getTranslatableAttribute($value); |
||
| 100 | |||
| 101 | if ($this->shouldParseWithSnippets($value)) { |
||
| 102 | $value = $this->parseWithSnippets($value); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $value; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Page specific modules. We exclude text modules since they are modules in pure |
||
| 110 | * technical terms and not so much as behavioural elements for the admin. |
||
| 111 | * |
||
| 112 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 113 | */ |
||
| 114 | public function modules() |
||
| 115 | { |
||
| 116 | return $this->hasMany(Module::class, 'page_id')->where('morph_key', '<>', 'text'); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function flatReference(): FlatReference |
||
| 120 | { |
||
| 121 | return new FlatReference(static::class, $this->id); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function flatReferenceLabel(): string |
||
| 125 | { |
||
| 126 | if ($this->exists) { |
||
| 127 | $status = ! $this->isPublished() ? ' [' . $this->statusAsPlainLabel().']' : null; |
||
| 128 | |||
| 129 | return $this->title ? $this->title . $status : ''; |
||
| 130 | } |
||
| 131 | |||
| 132 | return ''; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function flatReferenceGroup(): string |
||
| 136 | { |
||
| 137 | $classKey = get_class($this); |
||
| 138 | $labelSingular = property_exists($this, 'labelSingular') ? $this->labelSingular : str_singular($classKey); |
||
| 139 | |||
| 140 | return $labelSingular; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function mediaUrls($type = null): Collection |
||
| 144 | { |
||
| 145 | return $this->getAllFiles($type)->map->getFileUrl(); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function mediaUrl($type = null): ?string |
||
| 149 | { |
||
| 150 | return $this->mediaUrls($type)->first(); |
||
| 151 | } |
||
| 152 | |||
| 153 | public static function findPublished($id) |
||
| 156 | } |
||
| 157 | |||
| 158 | public function scopeSortedByCreated($query) |
||
| 159 | { |
||
| 160 | $query->orderBy('created_at', 'DESC'); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** @inheritdoc */ |
||
| 164 | public function url(string $locale = null): string |
||
| 165 | { |
||
| 166 | if (!$locale) { |
||
| 167 | $locale = app()->getLocale(); |
||
| 168 | } |
||
| 169 | |||
| 170 | try { |
||
| 171 | $slug = MemoizedUrlRecord::findByModel($this, $locale)->slug; |
||
| 172 | |||
| 173 | return $this->resolveUrl($locale, [$slug]); |
||
| 174 | } catch (UrlRecordNotFound $e) { |
||
| 175 | return ''; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | public function resolveUrl(string $locale = null, $parameters = null): string |
||
| 180 | { |
||
| 181 | $routeName = config('thinktomorrow.chief.route.name'); |
||
| 182 | |||
| 183 | return $this->resolveRoute($routeName, $parameters, $locale); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** @inheritdoc */ |
||
| 187 | public function previewUrl(string $locale = null): string |
||
| 188 | { |
||
| 189 | return $this->url($locale).'?preview-mode'; |
||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | /** @inheritdoc */ |
||
| 194 | public static function baseUrlSegment(string $locale = null): string |
||
| 195 | { |
||
| 196 | if (!isset(static::$baseUrlSegment)) { |
||
| 197 | return '/'; |
||
| 198 | } |
||
| 199 | |||
| 200 | if (!is_array(static::$baseUrlSegment)) { |
||
| 201 | return static::$baseUrlSegment; |
||
| 202 | } |
||
| 203 | |||
| 204 | // When an array, we try to locate the expected segment by locale |
||
| 205 | $key = $locale ?? app()->getlocale(); |
||
| 206 | |||
| 207 | if (isset(static::$baseUrlSegment[$key])) { |
||
| 208 | return static::$baseUrlSegment[$key]; |
||
| 209 | } |
||
| 210 | |||
| 211 | // Fall back to first entry in case no match is found |
||
| 212 | return reset(static::$baseUrlSegment); |
||
| 213 | } |
||
| 214 | |||
| 215 | public function menuLabel(): string |
||
| 216 | { |
||
| 217 | return $this->title ?? ''; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * We override the publishable trait defaults because Page needs |
||
| 222 | * to be concerned with the archived state as well. |
||
| 223 | * |
||
| 224 | * TODO: IMPROVEMENT SHOULD BE TO MANAGE THE PAGE STATES IN ONE LOCATION. eg state machine |
||
| 225 | */ |
||
| 226 | public function isPublished() |
||
| 227 | { |
||
| 228 | return (!!$this->published && is_null($this->archived_at)); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function isDraft() |
||
| 232 | { |
||
| 233 | return (!$this->published && is_null($this->archived_at)); |
||
| 234 | } |
||
| 235 | |||
| 236 | public function publish() |
||
| 237 | { |
||
| 238 | $this->published = 1; |
||
| 239 | $this->archived_at = null; |
||
| 240 | |||
| 241 | $this->save(); |
||
| 242 | } |
||
| 243 | |||
| 244 | public function draft() |
||
| 250 | } |
||
| 251 | |||
| 252 | public function statusAsLabel() |
||
| 253 | { |
||
| 254 | if ($this->isPublished()) { |
||
| 255 | return '<a href="'.$this->url().'" target="_blank"><em>online</em></a>'; |
||
| 256 | } |
||
| 257 | |||
| 258 | if ($this->isDraft()) { |
||
| 259 | return '<a href="'.$this->previewUrl().'" target="_blank" class="text-error"><em>offline</em></a>'; |
||
| 260 | } |
||
| 261 | |||
| 262 | if ($this->isArchived()) { |
||
| 263 | return '<span><em>gearchiveerd</em></span>'; |
||
| 264 | } |
||
| 265 | |||
| 266 | return '-'; |
||
| 267 | } |
||
| 268 | |||
| 269 | public function statusAsPlainLabel() |
||
| 284 | } |
||
| 285 | } |
||
| 286 |