Post   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 19
dl 0
loc 48
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTimeAgoAttribute() 0 3 1
A scopeGetTranslated() 0 8 2
A getLanguageAttribute() 0 3 1
A translate() 0 13 3
A wpml() 0 3 1
1
<?php
2
3
namespace Presspack\Framework;
4
5
use Carbon\Carbon;
6
use Corcel\Model\Post as BasePost;
7
use Illuminate\Support\Facades\App;
8
use Presspack\Framework\Support\Translation\Models\IclTranslation;
9
10
class Post extends BasePost
11
{
12
    protected $postType = 'post';
13
14
    public function __construct(array $attributes = [])
15
    {
16
        $this->append(['time_ago']);
17
        parent::__construct($attributes);
18
    }
19
20
    public function wpml()
21
    {
22
        return $this->hasOne(IclTranslation::class, 'element_id');
23
    }
24
25
    public function getTimeAgoAttribute()
26
    {
27
        return Carbon::parse($this->attributes['post_date'])->diffForHumans();
28
    }
29
30
    public function getLanguageAttribute()
31
    {
32
        return $this->wpml->language_code;
0 ignored issues
show
Bug introduced by
The property language_code does not seem to exist on Presspack\Framework\Supp...n\Models\IclTranslation. 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...
33
    }
34
35
    public function scopeGetTranslated($query, $lang = null)
36
    {
37
        return $query->leftJoin('icl_translations AS tr', function ($join) {
38
            $join->on('posts.ID', '=', 'tr.element_id')
39
                ->where('tr.element_type', '=', "post_{$this->postType}");
40
        })
41
            ->where('tr.language_code', '=', $lang ?: App::getLocale())
42
            ->get();
43
    }
44
45
    public function translate($lang = null)
46
    {
47
        $element = IclTranslation::where('element_id', $this->attributes['ID'])->first();
48
49
        $translations = IclTranslation::where('trid', $element->trid)
0 ignored issues
show
Bug introduced by
The property trid does not seem to exist on Presspack\Framework\Supp...n\Models\IclTranslation. 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...
50
            ->where('language_code', $lang ?: App::getLocale())
51
            ->first();
52
53
        if (empty($translations)) {
54
            $translations = IclTranslation::where('trid', $element->trid)->where('source_language_code', null)->first();
55
        }
56
        // Getting Post Object
57
        return self::find($translations->element_id);
0 ignored issues
show
Bug introduced by
The property element_id does not seem to exist on Presspack\Framework\Supp...n\Models\IclTranslation. 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...
58
    }
59
}
60