Comment   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 164
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A commentable() 0 4 1
A boot() 0 10 1
A newEloquentBuilder() 0 4 1
A newQuery() 0 4 1
A setContentAttribute() 0 6 1
A toEntity() 0 9 1
A registerCommentForProject() 0 44 4
A references() 0 4 1
A registerComents() 0 10 3
1
<?php
2
3
namespace Transmissor\Models;
4
5
use App\Contants\Tables;
6
use Carbon\Carbon;
7
use Finder\Models\Reference;
8
use Illuminate\Database\Eloquent\Collection;
9
use Population\Manipule\Builders\CommentBuilder;
10
use Population\Manipule\Entities\CommentEntity;
11
use Pedreiro\Models\Base;
12
use Transmissor\Models\Post;
13
14
/**
15
 * Class Comment.
16
 *
17
 * @property int id
18
 * @property string content
19
 * @property Collection posts
20
 * @package  App\Models
21
 */
22
class Comment extends Base
23
{
24
    public static $classeBuilder = CommentBuilder::class;
25
    /**
26
     * @inheritdoc
27
     */
28
    public $timestamps = false;
29
30
    /**
31
     * @inheritdoc
32
     */
33
    protected $fillable = [
34
        'commentable_id',
35
        'commentable_type',
36
        'content',
37
    ];
38
39
40
    protected $mappingProperties = array(
41
        /**
42
         * User Info
43
         */
44
        'content' => [
45
            'type' => 'string',
46
            "analyzer" => "standard",
47
        ],
48
    );
49
    
50
    /**
51
     * Get the owning commentable model.
52
     */
53
    public function commentable()
54
    {
55
        return $this->morphTo();
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    protected static function boot()
62
    {
63
        parent::boot();
64
65
        static::deleting(
66
            function (self $comment) {
67
                $comment->posts()->detach();
68
            }
69
        );
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function newEloquentBuilder($query): CommentBuilder
76
    {
77
        return new CommentBuilder($query);
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function newQuery(): CommentBuilder
84
    {
85
        return parent::newQuery();
86
    }
87
88
    // @todo Carregar Modelo Post para Blog
89
    // /**
90
    //  * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
91
    //  */
92
    // public function posts()
93
    // {
94
    //     return $this->belongsToMany(Post::class, Tables::TABLE_POSTS_TAGS);
95
    // }
96
97
    /**
98
     * Setter for the 'content' attribute.
99
     *
100
     * @param  string $content
101
     * @return $this
102
     */
103
    public function setContentAttribute(string $content)
104
    {
105
        $this->attributes['content'] = trim(str_replace(' ', '_', strtolower($content)));
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return CommentEntity
112
     */
113
    public function toEntity(): CommentEntity
114
    {
115
        return new CommentEntity(
116
            [
117
            'id' => $this->id,
118
            'content' => $this->content,
119
            ]
120
        );
121
    }
122
    
123
    // @todo fazer
124
    public static function registerCommentForProject($comment, $id, $type, $projectUrl = false)
125
    {
126
        $comment =  self::firstOrCreate(
127
            [
128
            'content' => $comment->body,
129
            'commentable_id' => $id,
130
            'commentable_type' => $type,
131
            'created_at' => $comment->created,
132
            'updated_at' => $comment->updated
133
            ]
134
        );
135
136
        if ($projectUrl) {
137
            if (!$reference = Reference::where(
138
                [
139
                'code' => $projectUrl
140
                ]
141
            )->first()
142
            ) {
143
                $reference = Reference::create(
144
                    [
145
                    'code' => $projectUrl,
146
                    'name' => $projectUrl,
147
                    ]
148
                );
149
            }
150
            if (!$comment->references()->where('reference_id', $reference->id)->first()) {
151
                $comment->references()->save(
152
                    $reference,
153
                    [
154
                        'identify' => $comment->id,
155
                    ]
156
                );
157
            }
158
        }
159
        return $comment;
160
161
        // foreach($comments as $comment) {
162
        //     var_dump($comment);
163
        //     Coment::firstOrCreate([
164
        //         'name' => $comment->name
165
        //     ]);
166
        // }
167
    }
168
    
169
    public function references()
170
    {
171
        return $this->morphToMany(Reference::class, 'referenceable');
172
    }
173
    
174
    public static function registerComents($data, $id, $type, $reference)
175
    {
176
        if ($data->total<=0) {
177
            return false;
178
        }
179
180
        foreach ($data->comments as $comment) {
181
            static::registerCommentForProject($comment, $id, $type, $reference);
182
        }
183
    }
184
    
185
}
186