dev-think-one /
laravel-iprosoftware-sync
| 1 | <?php |
||
| 2 | |||
| 3 | namespace IproSync\Models; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Casts\Attribute; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use Illuminate\Database\Eloquent\Model; |
||
| 7 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
| 8 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||
| 9 | use Illuminate\Support\Arr; |
||
| 10 | use IproSync\Jobs\Contacts\ContactPull; |
||
| 11 | use LaravelIproSoftwareApi\IproSoftwareFacade; |
||
| 12 | |||
| 13 | class Contact extends Model |
||
| 14 | { |
||
| 15 | use HasTraitsWithCasts, HasPullAt; |
||
| 16 | |||
| 17 | public $incrementing = false; |
||
| 18 | |||
| 19 | protected $guarded = []; |
||
| 20 | |||
| 21 | public function getTable(): string |
||
| 22 | { |
||
| 23 | return config('iprosoftware-sync.tables.contacts'); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function name(): Attribute |
||
| 27 | { |
||
| 28 | return Attribute::get(fn () => implode(' ', array_filter([ |
||
| 29 | $this->title, |
||
|
0 ignored issues
–
show
|
|||
| 30 | $this->first_name, |
||
|
0 ignored issues
–
show
|
|||
| 31 | $this->last_name, |
||
|
0 ignored issues
–
show
|
|||
| 32 | ]))); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function updateInToIpro(array $only = []) |
||
| 36 | { |
||
| 37 | $dataMap = [ |
||
| 38 | 'TypeId' => 'type_id', |
||
| 39 | 'BrandId' => 'brand_id', |
||
| 40 | 'Title' => 'title', |
||
| 41 | 'FirstName' => 'first_name', |
||
| 42 | 'LastName' => 'last_name', |
||
| 43 | 'Email' => 'email', |
||
| 44 | 'EmailAlt' => 'email_alt', |
||
| 45 | 'EmailAlt1' => 'email_alt_1', |
||
| 46 | 'Telephone' => 'telephone', |
||
| 47 | 'TelephoneAlt' => 'telephone_alt', |
||
| 48 | 'Mobile' => 'mobile', |
||
| 49 | 'Address' => 'address', |
||
| 50 | 'StreetName' => 'street_name', |
||
| 51 | 'TownCity' => 'town_city', |
||
| 52 | 'CountyArea' => 'county_area', |
||
| 53 | 'Postcode' => 'postcode', |
||
| 54 | 'CountryCode' => 'country_code', |
||
| 55 | 'CompanyName' => 'company_name', |
||
| 56 | 'Comments' => 'comments', |
||
| 57 | 'Balance' => 'balance', |
||
| 58 | 'Retainer' => 'Retainer', |
||
| 59 | 'DoNotMail' => 'contact_by_post', |
||
| 60 | 'DoNotEmail' => 'contact_by_email', |
||
| 61 | 'DoNotPhone' => 'contact_by_phone', |
||
| 62 | 'OnEmailList' => 'subscribed_to_mailing_list', |
||
| 63 | ]; |
||
| 64 | |||
| 65 | if (!empty($only)) { |
||
| 66 | $dataMap = array_flip(Arr::only(array_flip($dataMap), $only)); |
||
| 67 | } |
||
| 68 | |||
| 69 | $formParams = []; |
||
| 70 | foreach ($dataMap as $iproKey => $dbKey) { |
||
| 71 | if (in_array($iproKey, [ |
||
| 72 | 'DoNotMail', |
||
| 73 | 'DoNotEmail', |
||
| 74 | 'DoNotPhone', |
||
| 75 | ])) { |
||
| 76 | $formParams[$iproKey] = !$this->$dbKey; |
||
| 77 | } else { |
||
| 78 | $formParams[$iproKey] = $this->$dbKey; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | IproSoftwareFacade::createOrUpdateContact([ |
||
| 84 | 'query' => [ |
||
| 85 | 'contactId' => $this->getKey(), |
||
| 86 | ], |
||
| 87 | 'form_params' => $formParams, |
||
| 88 | ])->onlySuccessful(); |
||
| 89 | |||
| 90 | ContactPull::dispatchSync($this->getKey()); |
||
| 91 | |||
| 92 | return $this->refresh(); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function type(): BelongsTo |
||
| 96 | { |
||
| 97 | return $this->belongsTo(ContactType::class, 'type_id', 'id'); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function ownedProperties(): HasMany |
||
| 101 | { |
||
| 102 | return $this->hasMany(Property::class, 'owner_contact_id', 'id'); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function bookings(): HasMany |
||
| 106 | { |
||
| 107 | return $this->hasMany(Booking::class, 'contact_id', 'id'); |
||
| 108 | } |
||
| 109 | } |
||
| 110 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: