Contact::ownedProperties()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace IproSync\Models;
4
5
use Illuminate\Database\Eloquent\Casts\Attribute;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, IproSync\Models\Attribute. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
Bug introduced by
The property title does not seem to exist on IproSync\Models\Contact. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
30
            $this->first_name,
0 ignored issues
show
Bug introduced by
The property first_name does not seem to exist on IproSync\Models\Contact. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
31
            $this->last_name,
0 ignored issues
show
Bug introduced by
The property last_name does not seem to exist on IproSync\Models\Contact. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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