Completed
Push — master ( a1f3ed...74c577 )
by Kirill
02:49
created

Achieve   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 124
Duplicated Lines 10.48 %
Metric Value
wmc 2
dl 13
loc 124
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ toArray() 7 7 1
A boot() 0 18 3
A has() 0 7 1
A user() 0 4 1
A getAchieveAttribute() 0 8 2
A getTitleAttribute() 0 4 1
A getDescriptionAttribute() 0 4 1
A getImageAttribute() 0 4 1
A getCreatedAtAttribute() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 11.10.2015 06:03
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Domains;
12
13
use Carbon\Carbon;
14
use Interfaces\Gitter\Achieve\AbstractAchieve;
15
use Illuminate\Contracts\Support\Arrayable;
16
17
/**
18
 * Class Achieve
19
 *
20
 * @property-read int $id
21
 * @property string $name
22
 * @property int $user_id
23
 *
24
 * === Relations ===
25
 *
26
 * @property-read User $user
27
 *
28
 * === Accessors ===
29
 *
30
 * @property-read string $title
31
 * @property-read string $description
32
 * @property-read string $image
33
 * @property string $created_at
34
 * @property-read AbstractAchieve $achieve
35
 *
36
 */
37
class Achieve extends \Eloquent
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $table = 'achievements';
43
44
    /**
45
     * @var array
46
     */
47
    public $timestamps = false;
48
49
    /**
50
     * @var array
51
     */
52
    protected $guarded = ['id'];
53
54
    /**
55
     * @var array
56
     */
57
    protected $appends = ['title', 'description', 'image'];
58
59
    /**
60
     * @var AbstractAchieve
61
     */
62
    protected $cachedAchieve = null;
63
64
    /**
65
     * Boot
66
     */
67
    public static function boot()
68
    {
69
        parent::boot();
70
71
        static::creating(function (Achieve $achieve) {
72
            if (!$achieve->created_at) {
73
                $achieve->created_at = $achieve->freshTimestamp();
74
            }
75
76
            if (static::has($achieve->user, $achieve->name)) {
77
                return false;
78
            }
79
80
            \Event::fire('achieve.add', ['achieve' => $achieve]);
81
82
            return null;
83
        });
84
    }
85
86
    /**
87
     * @param User $user
88
     * @param string $name
89
     * @return bool
90
     */
91
    public static function has(User $user, $name)
92
    {
93
        return !!static::query()
94
            ->where('user_id', $user->id)
95
            ->where('name', $name)
96
            ->count();
97
    }
98
99
    /**
100
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
101
     */
102
    public function user()
103
    {
104
        return $this->belongsTo(User::class, 'user_id', 'id');
105
    }
106
107
    /**
108
     * @return AbstractAchieve
109
     */
110
    public function getAchieveAttribute()
111
    {
112
        if ($this->cachedAchieve === null) {
113
            $this->cachedAchieve = new $this->name;
114
        }
115
116
        return $this->cachedAchieve;
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getTitleAttribute()
123
    {
124
        return $this->achieve->title;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getDescriptionAttribute()
131
    {
132
        return $this->achieve->description;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getImageAttribute()
139
    {
140
        return $this->achieve->image;
141
    }
142
143
    /**
144
     * @param $time
145
     * @return Carbon
146
     */
147 View Code Duplication
    public function getCreatedAtAttribute($time)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        return new class($time) extends Carbon implements Arrayable
150
        {
151
            public function toArray()
152
            {
153
                return [
154
                    'date'     => $this->toIso8601String(),
155
                    'timezone' => $this->timezone,
156
                ];
157
            }
158
        };
159
    }
160
}