Passed
Push — develop ( e34a97...d968c2 )
by Septianata
14:57
created

Customer::createByBotManUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 16
cp 0
rs 9.7666
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace App\Models;
4
5
use App\Enum\Gender;
6
use BotMan\BotMan\Interfaces\UserInterface;
7
use Illuminate\Database\Eloquent\Factories\HasFactory;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Collection;
10
use Propaganistas\LaravelPhone\Casts\E164PhoneNumberCast;
11
12
class Customer extends Model
13
{
14
    use HasFactory,
15
        Concerns\Customer\Attribute,
16
        Concerns\Customer\Event;
17
18
    /**
19
     * Path value for identitycard_image storage.
20
     *
21
     * @var string
22
     */
23
    const IDENTITYCARD_IMAGE_PATH = 'identity_card';
24
25
    /**
26
     * {@inheritDoc}
27
     */
28
    protected $fillable = [
29
        'telegram_chat_id',
30
        'username',
31
        'fullname',
32
        'gender',
33
        'email',
34
        'phone_country',
35
        'phone',
36
        'whatsapp_phone_country',
37
        'whatsapp_phone',
38
        'account_number',
39
        'identitycard_number',
40
        'identitycard_image',
41
        'location_latitude',
42
        'location_longitude',
43
    ];
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    protected $casts = [
49
        'gender' => Gender::class,
50
        'phone' => E164PhoneNumberCast::class,
51
        'whatsapp_phone' => E164PhoneNumberCast::class,
52
        'location_latitude' => 'float',
53
        'location_longitude' => 'float',
54
    ];
55
56
    /**
57
     * Retrieve model data by the given BotMan user data.
58
     *
59
     * @param  \BotMan\BotMan\Interfaces\UserInterface  $user
60
     * @return static
61
     */
62
    public static function retrieveByBotManUser(UserInterface $user)
63
    {
64
        return static::firstWhere([
65
            ['telegram_chat_id', $user->getId()],
66
            ['username', $user->getUsername()],
67
        ]);
68
    }
69
70
    /**
71
     * Retrieve model data by the given credentials ("username", "email").
72
     *
73
     * @param  array  $credentials
74
     * @return static
75
     */
76
    public static function retrieveByUsernameAndEmail(array $credentials)
77
    {
78
        return static::firstWhere([
79
            ['username', $credentials['username']],
80
            ['email', $credentials['email']],
81
        ]);
82
    }
83
84
    /**
85
     * Create model data and store to the storage based on the given BotMan user data.
86
     *
87
     * @param  \BotMan\BotMan\Interfaces\UserInterface  $user
88
     * @param  \Illuminate\Support\Collection  $storage
89
     * @return static
90
     */
91
    public static function updateOrCreateByBotManUser(UserInterface $user, Collection $storage)
92
    {
93
        return static::updateOrCreate([
94
            'telegram_chat_id' => $user->getId(),
95
            'username' => $user->getUsername(),
96
        ], [
97
            'telegram_chat_id' => $user->getId(),
98
            'username' => $user->getUsername(),
99
            'fullname' => $storage->get('fullname'),
100
            'gender' => $storage->get('gender'),
101
            'email' => $storage->get('email'),
102
            'phone_country' => $storage->get('phone_country', env('PHONE_COUNTRY')),
103
            'phone' => $storage->get('phone'),
104
            'whatsapp_phone_country' => $storage->get('whatsapp_phone_country', env('PHONE_COUNTRY')),
105
            'whatsapp_phone' => $storage->get('whatsapp_phone'),
106
            'account_number' => $storage->get('account_number'),
107
            'identitycard_number' => $storage->get('identitycard_number'),
108
            'identitycard_image' => $storage->get('identitycard_image'),
109
            'location_latitude' => $storage->get('location_latitude'),
110
            'location_longitude' => $storage->get('location_longitude'),
111
        ]);
112
    }
113
}
114