dev-think-one /
laravel-iprosoftware-sync
| 1 | <?php |
||
| 2 | |||
| 3 | namespace IproSync\Jobs\Contacts; |
||
| 4 | |||
| 5 | use Illuminate\Bus\Queueable; |
||
| 6 | use Illuminate\Contracts\Queue\ShouldQueue; |
||
| 7 | use Illuminate\Foundation\Bus\Dispatchable; |
||
| 8 | use Illuminate\Queue\InteractsWithQueue; |
||
| 9 | use Illuminate\Queue\SerializesModels; |
||
| 10 | use IproSync\Models\Contact; |
||
| 11 | use LaravelIproSoftwareApi\IproSoftwareFacade; |
||
| 12 | |||
| 13 | class ContactPull implements ShouldQueue |
||
| 14 | { |
||
| 15 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 16 | |||
| 17 | protected int $iproContactId; |
||
| 18 | protected array $requestParams; |
||
| 19 | |||
| 20 | public function __construct(int $iproContactId, array $requestParams = []) |
||
| 21 | { |
||
| 22 | $this->iproContactId = $iproContactId; |
||
| 23 | $this->requestParams = $requestParams; |
||
| 24 | } |
||
| 25 | |||
| 26 | |||
| 27 | public function handle() |
||
| 28 | { |
||
| 29 | $response = IproSoftwareFacade::getContact($this->iproContactId, [ |
||
| 30 | 'query' => $this->requestParams, |
||
| 31 | ])->onlySuccessful(); |
||
| 32 | |||
| 33 | $item = $response->json(); |
||
| 34 | self::createOrUpdateContact($item); |
||
| 35 | } |
||
| 36 | |||
| 37 | public static function createOrUpdateContact(array $item): ?Contact |
||
| 38 | { |
||
| 39 | if (isset($item['Id'])) { |
||
| 40 | $contact = Contact::firstOrNew(['id' => $item['Id']], ) |
||
| 41 | ->fill([ |
||
| 42 | 'type_id' => !empty($item['TypeId']) ? (int) $item['TypeId'] : null, |
||
| 43 | 'brand_id' => !empty($item['BrandId']) ? (int) $item['BrandId'] : null, |
||
| 44 | 'company_id' => !empty($item['CompanyId']) ? (int) $item['CompanyId'] : null, |
||
| 45 | 'company_name' => !empty($item['CompanyName']) ? (string) $item['CompanyName'] : null, |
||
| 46 | 'title' => !empty($item['Title']) ? (string) $item['Title'] : null, |
||
| 47 | 'first_name' => !empty($item['FirstName']) ? (string) $item['FirstName'] : null, |
||
| 48 | 'last_name' => !empty($item['LastName']) ? (string) $item['LastName'] : null, |
||
| 49 | 'email' => !empty($item['Email']) ? (string) $item['Email'] : null, |
||
| 50 | 'email_alt' => !empty($item['EmailAlt']) ? (string) $item['EmailAlt'] : null, |
||
| 51 | 'email_alt_1' => !empty($item['EmailAlt1']) ? (string) $item['EmailAlt1'] : null, |
||
| 52 | 'telephone' => !empty($item['Telephone']) ? (string) $item['Telephone'] : null, |
||
| 53 | 'telephone_alt' => !empty($item['TelephoneAlt']) ? (string) $item['TelephoneAlt'] : null, |
||
| 54 | 'mobile' => !empty($item['Mobile']) ? (string) $item['Mobile'] : null, |
||
| 55 | 'address' => !empty($item['Address']) ? (string) $item['Address'] : null, |
||
| 56 | 'street_name' => !empty($item['StreetName']) ? (string) $item['StreetName'] : null, |
||
| 57 | 'town_city' => !empty($item['TownCity']) ? (string) $item['TownCity'] : null, |
||
| 58 | 'county_area' => !empty($item['CountyArea']) ? (string) $item['CountyArea'] : null, |
||
| 59 | 'postcode' => !empty($item['Postcode']) ? (string) $item['Postcode'] : null, |
||
| 60 | 'country' => !empty($item['Country']) ? (string) $item['Country'] : null, |
||
| 61 | 'country_code' => !empty($item['CountryCode']) ? (string) $item['CountryCode'] : null, |
||
| 62 | 'language' => !empty($item['Language']) ? (string) $item['Language'] : null, |
||
| 63 | 'contact_by_post' => (bool) ($item['ContactByPost'] ?? false), |
||
| 64 | 'contact_by_email' => (bool) ($item['ContactByEmail'] ?? false), |
||
| 65 | 'contact_by_phone' => (bool) ($item['ContactByPhone'] ?? false), |
||
| 66 | 'contact_by_sms' => (bool) ($item['ContactBySms'] ?? false), |
||
| 67 | 'subscribed_to_mailing_list' => (bool) ($item['SubscribedToMailingList'] ?? false), |
||
| 68 | 'comments' => !empty($item['Comments']) ? (string) $item['Comments'] : null, |
||
| 69 | 'commission' => !empty($item['Commision']) ? round((float) $item['Commision'], 2) : null, |
||
| 70 | 'balance' => !empty($item['Balance']) ? round((float) $item['Balance'], 2) : null, |
||
| 71 | 'retainer' => !empty($item['Retainer']) ? (string) $item['Retainer'] : null, |
||
| 72 | ]) |
||
| 73 | ->fillPulled(); |
||
| 74 | $contact->save(); |
||
| 75 | |||
| 76 | return $contact; |
||
| 77 | } |
||
| 78 | |||
| 79 | return null; |
||
| 80 | } |
||
| 81 | } |
||
| 82 |