Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Model/Front/Profile/FormSettings.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Apps\Model\Front\Profile;
4
5
use Apps\ActiveRecord\ProfileField;
6
use Apps\ActiveRecord\User;
7
use Ffcms\Core\Arch\Model;
8
use Ffcms\Core\Helper\Date;
9
use Ffcms\Core\Helper\Type\Str;
10
use Ffcms\Core\Interfaces\iUser;
11
12
/**
13
 * Class FormSettings. Business logic of user personal settings form
14
 * @package Apps\Model\Front\Profile
15
 */
16
class FormSettings extends Model
17
{
18
    public $nick;
19
    public $sex;
20
    public $birthday;
21
    public $city;
22
    public $hobby;
23
    public $phone;
24
    public $url;
25
26
    public $custom_data = [];
27
28
    private $_user;
29
30
    /**
31
     * FormSettings constructor. Pass user object inside
32
     * @param iUser|User $user
33
     */
34
    public function __construct(iUser $user)
35
    {
36
        $this->_user = $user;
37
        parent::__construct(true);
38
    }
39
40
    /**
41
     * Set default data
42
     */
43
    public function before()
44
    {
45
        $profile = $this->_user->profile->toArray(); // object to array (property's is protected of access)
0 ignored issues
show
Accessing profile on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
        foreach ($profile as $property => $value) {
47
            // if property exist - lets pass data to model
48
            if (property_exists($this, $property)) {
49
                if ($property === 'birthday') {
50
                    if (Str::likeEmpty($value)) {
51
                        $this->birthday = null;
52
                    } else {
53
                        $this->birthday = Date::convertToDatetime($value, Date::FORMAT_TO_DAY);
54
                    }
55
                    continue;
56
                }
57
                $this->{$property} = $value;
58
            }
59
        }
60
    }
61
62
    /**
63
     * Form display labels
64
     * @return array
65
     */
66
    public function labels(): array
67
    {
68
        $labels = [
69
            'nick' => __('Nickname'),
70
            'sex' => __('Sex'),
71
            'birthday' => __('Birthday'),
72
            'city' => __('City'),
73
            'hobby' => __('Hobby'),
74
            'phone' => __('Phone'),
75
            'url' => __('Website')
76
        ];
77
78
        // labels for custom fields
79
        foreach (ProfileField::all() as $custom) {
80
            $labels['custom_data.' . $custom->id] = $custom->getLocaled('name');
81
        }
82
83
        return $labels;
84
    }
85
86
    /**
87
     * Rules for validation
88
     * @return array
89
     */
90
    public function rules(): array
91
    {
92
        $rules = [
93
            ['sex', 'required'],
94
            [['city', 'hobby', 'phone', 'url', 'nick', 'birthday'], 'used'],
95
            ['nick', 'length_max', '50'],
96
            ['city', 'length_max', '50'],
97
            ['sex', 'in', [0, 1, 2]],
98
            ['hobby', 'length_max', '50'],
99
            ['phone', 'phone'],
100
            ['url', 'url'],
101
            ['nick', 'notequal', $this->_user->login]
102
        ];
103
104
        // custom profile fields
105
        foreach (ProfileField::all() as $custom) {
106
            $rules[] = [
107
                'custom_data.' . $custom->id,
108
                'used'
109
            ];
110
            $rules[] = [
111
                'custom_data.' . $custom->id,
112
                (int)$custom->reg_cond === 1 ? 'direct_match' : 'reverse_match',
113
                $custom->reg_exp
114
            ];
115
        }
116
117
        return $rules;
118
    }
119
120
    /**
121
     * Save data after validation
122
     */
123
    public function save()
124
    {
125
        $profile = $this->_user->profile;
0 ignored issues
show
Accessing profile on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
126
        $profile->nick = $this->nick;
127
        $profile->sex = $this->sex;
128
        $profile->birthday = (Str::likeEmpty($this->birthday) ? null : Date::convertToDatetime($this->birthday, Date::FORMAT_SQL_DATE));
129
        $profile->city = $this->city;
130
        $profile->hobby = $this->hobby;
131
        $profile->phone = $this->phone;
132
        $profile->url = $this->url;
133
        $profile->custom_data = $this->custom_data;
134
135
        $profile->save();
136
    }
137
}
138