DownloadMyriadContact   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 50
ccs 33
cts 33
cp 1
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 34 8
A defaultCollectionFormat() 0 7 1
1
<?php
2
3
namespace MyriadDataStore\Actions;
4
5
use MyriadDataStore\MyriadDataDownloader;
6
use MyriadSoap\Exceptions\UnexpectedTypeException;
7
use MyriadSoap\MyriadSoap;
8
9
class DownloadMyriadContact
10
{
11
    use UsesCollectionFormat;
12
13
    public static ?array $collectionFormat = null;
14
15 5
    protected function defaultCollectionFormat(): array
16
    {
17 5
        return [
18 5
            'ContactCommunication_ID' => fn ($i) => (int) tap($i, fn () => throw_if(!is_numeric($i), UnexpectedTypeException::class)),
19 5
            'DespatchType_ID'         => fn ($i) => (int) tap($i, fn () => throw_if(!is_numeric($i), UnexpectedTypeException::class)),
20 5
            'ContactCommunication'    => fn ($i) => (string) $i,
21 5
            'PrimaryUse'              => fn ($i) => $i == 'Yes',
22 5
        ];
23
    }
24
25 7
    public function execute(int $myriadContactID)
26
    {
27 7
        $details = MyriadSoap::SOAP_getContactDetails(['Contact_ID' => $myriadContactID]);
28 6
        if (!is_array($details)
29 6
            || empty($details['Contact_ID'])
30 6
            || $details['Contact_ID'] != $myriadContactID) {
31 1
            throw new \Exception('Wrong details data returned from API ['.$myriadContactID.'|'.($details['Contact_ID'] ?? '-').']');
32
        }
33
34 5
        $formattedCommunications = MyriadSoap::SOAP_getContactCommunications_Collection(
35 5
            ['Contact_ID' => $myriadContactID],
36 5
            $this->collectionFormat()
37 5
        );
38
39 5
        $primaryEmail = ($formattedCommunications->filter(fn ($formattedCommunication) => filter_var($formattedCommunication['ContactCommunication'], FILTER_VALIDATE_EMAIL))
40 5
                                                 ->sortBy(function ($formattedCommunication) {
41 4
                                                     return $formattedCommunication['PrimaryUse'] ? -1 : 1;
42 5
                                                 })
43 5
                                                 ->first() ?? [])['ContactCommunication'] ?? null;
44
45 5
        $modelClass         = MyriadDataDownloader::$contactModel;
46 5
        $myriadContactModel = $modelClass::find($myriadContactID);
47 5
        if (!$myriadContactModel) {
48 5
            $myriadContactModel     = new $modelClass;
49 5
            $myriadContactModel->id = $myriadContactID;
50
        }
51
52 5
        $myriadContactModel->contact_type_id = (!empty($details['ContactType_ID']) && is_numeric($details['ContactType_ID'])) ? ((int) $details['ContactType_ID']) : null;
53 5
        $myriadContactModel->email           = $primaryEmail;
54 5
        $myriadContactModel->details->setData($details);
55 5
        $myriadContactModel->communications->setData($formattedCommunications->toArray());
56 5
        $myriadContactModel->save();
57
58 5
        return $myriadContactModel;
59
    }
60
}
61