EditProfile   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A submit() 0 26 1
A render() 0 6 1
A mount() 0 21 1
A uploadProfile() 0 8 2
1
<?php
2
3
namespace Pratiksh\Adminetic\Http\Livewire\Admin\Profile;
4
5
use Intervention\Image\Facades\Image;
6
use Livewire\Component;
7
use Livewire\WithFileUploads;
8
use Pratiksh\Adminetic\Models\Admin\Profile;
9
10
class EditProfile extends Component
11
{
12
    use WithFileUploads;
0 ignored issues
show
introduced by
The trait Livewire\WithFileUploads requires some properties which are not provided by Pratiksh\Adminetic\Http\...min\Profile\EditProfile: $map, $timestamp
Loading history...
13
14
    public $username;
15
    public $profile_pic;
16
    public $gender;
17
    public $martial_status;
18
    public $blood_group;
19
    public $country;
20
    public $address;
21
    public $phone_no = [];
22
    public $email;
23
    public $birthday;
24
    public $facebook;
25
    public $instagram;
26
    public $twitter;
27
    public $linkedin;
28
    public $father_name;
29
    public $mother_name;
30
31
    public $profile;
32
33
    public function mount(Profile $profile)
34
    {
35
        $this->profile = $profile;
36
        $this->user_id = $profile->user_id;
0 ignored issues
show
Bug Best Practice introduced by
The property user_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
        $this->username = $profile->username;
38
        $this->profile_pic = $profile->profile_pic;
39
        $this->status = $profile->getRawOriginal('status');
0 ignored issues
show
Bug Best Practice introduced by
The property status does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
        $this->gender = $profile->getRawOriginal('gender');
41
        $this->martial_status = $profile->getRawOriginal('martial_status');
42
        $this->blood_group = $profile->getRawOriginal('blood_group');
43
        $this->country = $profile->country ?? 'Nepal';
44
        $this->address = $profile->address;
45
        $this->phone_no = $profile->phone_no;
46
        $this->email = $profile->email;
47
        $this->birthday = $profile->birthday;
48
        $this->facebook = $profile->facebook;
49
        $this->instagram = $profile->instagram;
50
        $this->twitter = $profile->twitter;
51
        $this->linkedin = $profile->linkedin;
52
        $this->father_name = $profile->father_name;
53
        $this->mother_name = $profile->mother_name;
54
    }
55
56
    public function submit()
57
    {
58
        // Validate Data
59
        $validatedData = $this->validate([
60
            'username' => 'nullable|max:100',
61
            'profile_pic' => 'nullable|file|image|max:5000',
62
            'gender' => 'nullable|numeric|max:3',
63
            'martial_status' => 'nullable|numeric|max:8',
64
            'blood_group' => 'nullable|numeric|max:15',
65
            'country' => 'nullable|max:50',
66
            'address' => 'nullable|max:255',
67
            'phone_no' => 'nullable|max:10',
68
            'email' => 'nullable|max:255',
69
            'birthday' => 'nullable|nullable',
70
            'facebook' => 'nullable|max:255',
71
            'instagram' => 'nullable|max:255',
72
            'twitter' => 'nullable|max:255',
73
            'linkedin' => 'nullable|max:255',
74
            'father_name' => 'nullable|max:255',
75
            'mother_name' => 'nullable|max:255',
76
        ]);
77
        // Update Profile
78
        $this->profile->update($validatedData);
79
        $this->uploadProfile($this->profile);
80
        // Initialize Events
81
        $this->emit('profile_updated');
82
    }
83
84
    protected function uploadProfile($profile)
85
    {
86
        if (isset($this->profile_pic)) {
87
            $profile->update([
88
                'profile_pic' => $this->profile_pic->store('admin/user', 'public'),
89
            ]);
90
            $image = Image::make($this->profile_pic->getRealPath());
91
            $image->save(public_path('storage/'.$profile->profile_pic));
92
        }
93
    }
94
95
    public function render()
96
    {
97
        $this->emit('initializeProfile');
98
        $countries = ['Afghanistan', 'Albania', 'Algeria', 'American Samoa', 'Andorra', 'Angola', 'Anguilla', 'Antarctica', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Aruba', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bermuda', 'Bhutan', 'Bolivia', 'Bosnia and Herzegowina', 'Botswana', 'Bouvet Island', 'Brazil', 'British Indian Ocean Territory', 'Brunei Darussalam', 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Cayman Islands', 'Central African Republic', 'Chad', 'Chile', 'China', 'Christmas Island', 'Cocos (Keeling) Islands', 'Colombia', 'Comoros', 'Congo', 'Congo, the Democratic Republic of the', 'Cook Islands', 'Costa Rica', "Cote d'Ivoire", 'Croatia (Hrvatska)', 'Cuba', 'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'East Timor', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Falkland Islands (Malvinas)', 'Faroe Islands', 'Fiji', 'Finland', 'France', 'France Metropolitan', 'French Guiana', 'French Polynesia', 'French Southern Territories', 'Gabon', 'Gambia', 'Georgia', 'Germany', 'Ghana', 'Gibraltar', 'Greece', 'Greenland', 'Grenada', 'Guadeloupe', 'Guam', 'Guatemala', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Heard and Mc Donald Islands', 'Holy See (Vatican City State)', 'Honduras', 'Hong Kong', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran (Islamic Republic of)', 'Iraq', 'Ireland', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', "Korea, Democratic People's Republic of", 'Korea, Republic of', 'Kuwait', 'Kyrgyzstan', "Lao, People's Democratic Republic", 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libyan Arab Jamahiriya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Macau', 'Macedonia, The Former Yugoslav Republic of', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Martinique', 'Mauritania', 'Mauritius', 'Mayotte', 'Mexico', 'Micronesia, Federated States of', 'Moldova, Republic of', 'Monaco', 'Mongolia', 'Montserrat', 'Morocco', 'Mozambique', 'Myanmar', 'Namibia', 'Nauru', 'Nepal', 'Netherlands', 'Netherlands Antilles', 'New Caledonia', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Niue', 'Norfolk Island', 'Northern Mariana Islands', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Pitcairn', 'Poland', 'Portugal', 'Puerto Rico', 'Qatar', 'Reunion', 'Romania', 'Russian Federation', 'Rwanda', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Vincent and the Grenadines', 'Samoa', 'San Marino', 'Sao Tome and Principe', 'Saudi Arabia', 'Senegal', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia (Slovak Republic)', 'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa', 'South Georgia and the South Sandwich Islands', 'Spain', 'Sri Lanka', 'St. Helena', 'St. Pierre and Miquelon', 'Sudan', 'Suriname', 'Svalbard and Jan Mayen Islands', 'Swaziland', 'Sweden', 'Switzerland', 'Syrian Arab Republic', 'Taiwan, Province of China', 'Tajikistan', 'Tanzania, United Republic of', 'Thailand', 'Togo', 'Tokelau', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Turks and Caicos Islands', 'Tuvalu', 'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'United States Minor Outlying Islands', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Venezuela', 'Vietnam', 'Virgin Islands (British)', 'Virgin Islands (U.S.)', 'Wallis and Futuna Islands', 'Western Sahara', 'Yemen', 'Yugoslavia', 'Zambia', 'Zimbabwe'];
99
100
        return view('adminetic::livewire.admin.profile.edit-profile', compact('countries'));
101
    }
102
}
103