Completed
Branch job-vuejs (8df734)
by Adam
17:32
created

Firm::__sleep()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 6
nop 0
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote;
4
5
use Coyote\Services\Media\Factory as MediaFactory;
6
use Coyote\Services\Media\Logo;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\SoftDeletes;
9
10
/**
11
 * @property int $id
12
 * @property int $is_agency
13
 * @property int $user_id
14
 * @property bool $is_private
15
 * @property string $name
16
 * @property string $city
17
 * @property string $street
18
 * @property string $house
19
 * @property string $postcode
20
 * @property string $website
21
 * @property string $description
22
 * @property \Coyote\Firm\Benefit[] $benefits
23
 * @property Logo $logo
24
 */
25
class Firm extends Model
26
{
27
    use SoftDeletes;
28
29
    /**
30
     * The attributes that are mass assignable.
31
     *
32
     * @var array
33
     */
34
    protected $fillable = [
35
        'name',
36
        'logo',
37
        'website',
38
        'headline',
39
        'description',
40
        'employees',
41
        'founded',
42
        'is_agency',
43
        'country_id',
44
        'city',
45
        'street',
46
        'house',
47
        'postcode',
48
        'latitude',
49
        'longitude',
50
        'is_private'
51
    ];
52
53
    /**
54
     * @var string
55
     */
56
    protected $dateFormat = 'Y-m-d H:i:se';
57
58
    /**
59
     * Default fields values. Important for vue.js
60
     *
61
     * @var array
62
     */
63
    protected $attributes = [
64
        'is_agency' => false
65
    ];
66
67
    /**
68
     * Do not change default value. It is set to FALSE on purpose.
69
     *
70
     * @var bool
71
     */
72
    protected $isPrivate = false;
73
74
    public static function boot()
75
    {
76
        parent::boot();
77
78
        static::saving(function ($model) {
79
            foreach (['latitude', 'longitude', 'founded', 'employees', 'headline', 'description', 'latitude', 'longitude', 'street', 'city', 'house', 'postcode'] as $column) {
80
                if (empty($model->{$column})) {
81
                    $model->{$column} = null;
82
                }
83
            }
84
        });
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public static function getEmployeesList()
91
    {
92
        return [
93
            1 => '1-5',
94
            2 => '6-10',
95
            3 => '11-20',
96
            4 => '21-30',
97
            5 => '31-50',
98
            6 => '51-100',
99
            7 => '101-200',
100
            8 => '201-500',
101
            9 => '501-1000',
102
            10 => '1001-5000',
103
            11 => '5000+'
104
        ];
105
    }
106
107
    /**
108
     * @return array
109
     */
110 View Code Duplication
    public static function getFoundedList()
1 ignored issue
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...
111
    {
112
        $result = [];
113
114
        for ($i = 1900, $year = date('Y'); $i <= $year; $i++) {
115
            $result[$i] = $i;
116
        }
117
118
        return $result;
119
    }
120
121
    /**
122
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
123
     */
124
    public function benefits()
125
    {
126
        return $this->hasMany('Coyote\Firm\Benefit');
127
    }
128
129
    /**
130
     * @param string $name
131
     */
132
    public function setNameAttribute($name)
133
    {
134
        $name = trim($name);
135
136
        $this->attributes['name'] = $name;
137
    }
138
139
    /**
140
     * @param string $value
141
     * @return \Coyote\Services\Media\MediaInterface
142
     */
143 View Code Duplication
    public function getLogoAttribute($value)
1 ignored issue
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...
144
    {
145
        if (!($value instanceof Logo)) {
146
            $logo = app(MediaFactory::class)->make('logo', ['file_name' => $value]);
147
            $this->attributes['logo'] = $logo;
148
        }
149
150
        return $this->attributes['logo'];
151
    }
152
153
    /**
154
     * @param bool $flag
155
     */
156
    public function setIsPrivateAttribute($flag)
157
    {
158
        $this->isPrivate = $flag;
159
    }
160
161
    /**
162
     * @return bool
163
     */
164
    public function getIsPrivateAttribute()
165
    {
166
        return $this->isPrivate;
167
    }
168
169
    /**
170
     * @param int $userId
171
     */
172
    public function setDefaultUserId($userId)
173
    {
174
        if (empty($this->user_id)) {
175
            $this->user_id = $userId;
176
        }
177
    }
178
179
    public function __sleep()
180
    {
181
        if ($this->logo instanceof Logo) {
182
            $this->attributes['logo'] = $this->logo->getFilename();
183
        }
184
185
        $properties = (new \ReflectionClass($this))->getProperties();
186
187
        $result = [];
188
189
        foreach ($properties as $property) {
190
            if (!$property->isStatic()) {
191
                $result[] = $property->getName();
192
            }
193
        }
194
195
        return $result;
196
    }
197
}
198