DownloadMyriadContactOrdersBasic::execute()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 58
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 43
c 2
b 0
f 0
nc 5
nop 1
dl 0
loc 58
ccs 48
cts 48
cp 1
crap 5
rs 8.9208

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MyriadDataStore\Actions;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Str;
7
use MyriadDataStore\Models\MyriadOrder;
8
use MyriadSoap\Exceptions\UnexpectedTypeException;
9
use MyriadSoap\MyriadSoap;
10
11
class DownloadMyriadContactOrdersBasic
12
{
13
    use UsesCollectionFormat;
14
15
    public static ?array $collectionFormat = null;
16
17 4
    protected function defaultCollectionFormat(): array
18
    {
19 4
        return [
20 4
            'OrderNumber'         => fn ($i) => (int) tap($i, fn () => throw_if(!is_numeric($i), UnexpectedTypeException::class)),
21 4
            'SubsPromotion'       => fn ($i) => (string) $i,
22 4
            'Currency'            => fn ($i) => (string) $i,
23 4
            'PaymentType'         => fn ($i) => (string) $i,
24 4
            'StatusType'          => fn ($i) => (string) $i,
25 4
            'Amount'              => fn ($i) => (string) $i,
26 4
            'CollectionFrequency' => fn ($i) => (string) $i,
27 4
            'Invoice_Contact_ID'  => fn ($i) => (int) $i,
28 4
            'Invoice_Contact'     => fn ($i) => (string) $i,
29 4
            'Despatch_Contact_ID' => fn ($i) => (int) $i,
30 4
            'Despatch_Contact'    => fn ($i) => (string) $i,
31 4
            'Agent_Contact_ID'    => fn ($i) => (int) $i,
32 4
            'Agent_Contact'       => fn ($i) => (string) $i,
33 4
            'CreationDate'        => fn ($i) => Carbon::createFromFormat('Y-m-d', $i),
34 4
            'Packages'            => function ($i) {
35 3
                if (!isset($i['Package'])
36 3
                    || !is_array($i['Package'])) {
37
                    // Debug!; TODO: delete it
38
                    throw new \Exception('Package key not exists in array!!!! DEBUG');
39
40
                    return [];
0 ignored issues
show
Unused Code introduced by
return array() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
41
                }
42
43 3
                if (isset($i['Package']['OrderPackageType_ID'])) {
44 3
                    return [$i['Package']];
45
                }
46
47 1
                return $i['Package'];
48 4
            },
49 4
        ];
50
    }
51
52 4
    public function execute(int $myriadContactID)
53
    {
54 4
        $formattedCollection = MyriadSoap::SOAP_getOrdersBasic_AssocCollection(
55 4
            ['Contact_ID' => $myriadContactID],
56 4
            $this->collectionFormat(),
57 4
            'Order'
58 4
        );
59
60 3
        $count = 0;
61
62 3
        foreach ($formattedCollection as $item) {
63 3
            $myriadModel = MyriadOrder::find($item['OrderNumber']);
64 3
            if (!$myriadModel) {
65 3
                $myriadModel     = new MyriadOrder;
66 3
                $myriadModel->id = $item['OrderNumber'];
67
            }
68 3
            $myriadModel->status              = Str::limit($item['StatusType'], 252);
0 ignored issues
show
Bug introduced by
The property status does not seem to exist on MyriadDataStore\Models\MyriadOrder. 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...
69 3
            $myriadModel->invoice_contact_id  = $item['Invoice_Contact_ID'];
0 ignored issues
show
Bug introduced by
The property invoice_contact_id does not seem to exist on MyriadDataStore\Models\MyriadOrder. 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...
70 3
            $myriadModel->despatch_contact_id = $item['Despatch_Contact_ID'];
0 ignored issues
show
Bug introduced by
The property despatch_contact_id does not seem to exist on MyriadDataStore\Models\MyriadOrder. 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...
71 3
            $myriadModel->agent_contact_id    = $item['Agent_Contact_ID'];
0 ignored issues
show
Bug introduced by
The property agent_contact_id does not seem to exist on MyriadDataStore\Models\MyriadOrder. 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...
72 3
            $myriadModel->order_date          = $item['CreationDate'];
0 ignored issues
show
Bug introduced by
The property order_date does not seem to exist on MyriadDataStore\Models\MyriadOrder. 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...
73 3
            $myriadModel->details->setData([
74 3
                'SubsPromotion'       => $item['SubsPromotion'],
75 3
                'Currency'            => $item['Currency'],
76 3
                'PaymentType'         => $item['PaymentType'],
77 3
                'Amount'              => $item['Amount'],
78 3
                'CollectionFrequency' => $item['CollectionFrequency'],
79 3
                'Invoice_Contact'     => $item['Invoice_Contact'],
80 3
                'Despatch_Contact'    => $item['Despatch_Contact'],
81 3
                'Agent_Contact'       => $item['Agent_Contact'],
82 3
            ]);
83 3
            $myriadModel->save();
84
85 3
            $myriadModel->packages()->delete();
86
87 3
            foreach ($item['Packages'] as $package) {
88 3
                $myriadModel->packages()->create([
89 3
                    'order_package_type_id' => (int) ($package['OrderPackageType_ID'] ?? 0),
90 3
                    'title_id'              => (int) ($package['Title_ID']            ?? 0),
91 3
                    'start_issue'           => (string) ($package['StartIssue']       ?? ''),
92 3
                    'end_issue'             => (string) ($package['EndIssue']         ?? ''),
93 3
                    'status'                => (string) ($package['StatusType']       ?? ''),
94 3
                    'stopcode'              => (string) ($package['StopCode']         ?? ''),
95 3
                    'myriad_package_id'     => !empty($package['MyriadPackage_ID']) ? ((int) $package['MyriadPackage_ID']) : null,
96 3
                    'remaining_issues'      => (int) ($package['RemainingIssues'] ?? 0),
97 3
                    'copies'                => (int) ($package['Copies']          ?? 0),
98 3
                    'details'               => [
99 3
                        'Amount'           => (string) ($package['Amount']           ?? ''),
100 3
                        'OrderPackageType' => (string) ($package['OrderPackageType'] ?? ''),
101 3
                        'Title'            => (string) ($package['Title']            ?? ''),
102 3
                    ],
103 3
                ]);
104
            }
105
106 3
            $count++;
107
        }
108
109 3
        return $count;
110
    }
111
}
112