Completed
Branch 1.3 (ca63b5)
by Morven
03:44
created

OrdersMigrationTask::up()   F

Complexity

Conditions 16
Paths 720

Size

Total Lines 100
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 63
c 1
b 0
f 0
nc 720
nop 0
dl 0
loc 100
rs 1.7888

How to fix   Long Method    Complexity   

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 SilverCommerce\OrdersAdmin\Tasks;
4
5
use SilverStripe\ORM\DB;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Dev\MigrationTask;
8
use SilverStripe\ORM\DatabaseAdmin;
9
use SilverStripe\Control\Controller;
10
use SilverCommerce\GeoZones\Model\Zone;
11
use SilverStripe\SiteConfig\SiteConfig;
12
use SilverStripe\Subsites\Model\Subsite;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Subsites\Model\Subsite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SilverCommerce\OrdersAdmin\Model\Invoice;
14
use SilverCommerce\OrdersAdmin\Model\Estimate;
15
use SilverCommerce\OrdersAdmin\Model\LineItem;
16
use SilverCommerce\OrdersAdmin\Model\LineItemCustomisation;
17
18
/**
19
 * Task to handle migrating orders/items to newer versions
20
 */
21
class OrdersMigrationTask extends MigrationTask
22
{
23
24
    /**
25
     * Should this task be invoked automatically via dev/build?
26
     *
27
     * @config
28
     *
29
     * @var bool
30
     */
31
    private static $run_during_dev_build = true;
0 ignored issues
show
introduced by
The private property $run_during_dev_build is not used, and could be removed.
Loading history...
32
33
    private static $segment = 'OrdersMigrationTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
34
35
    protected $description = "Upgrade Orders/Items";
36
37
    /**
38
     * Run this task
39
     *
40
     * @param HTTPRequest $request The current request
0 ignored issues
show
Bug introduced by
The type SilverCommerce\OrdersAdmin\Tasks\HTTPRequest was not found. Did you mean HTTPRequest? If so, make sure to prefix the type with \.
Loading history...
41
     *
42
     * @return void
43
     */
44
    public function run($request)
45
    {
46
        if ($request->getVar('direction') == 'down') {
47
            $this->down();
48
        } else {
49
            $this->up();
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function up()
57
    {
58
        // Migrate estimate/invoice numbers to new ref field
59
        $this->log('- Migrating estimate/invoice numbers');
60
        $total = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $total is dead and can be removed.
Loading history...
61
62
        if (class_exists(Subsite::class)) {
63
            $items = Subsite::get_from_all_subsites(Estimate::class);
64
        } else {
65
            $items = Estimate::get();
66
        }
67
        
68
        $count = false;
69
        if ($items) {
70
            $this->log('- '.$items->count().' items to convert.');
71
            $count = $items->count();
72
        }
73
        $i = 0;
74
75
        foreach ($items as $item) {
76
            if ($item->Number !== null) {
77
                $config = SiteConfig::current_site_config();
78
                $inv_prefix = $config->InvoiceNumberPrefix;
79
                $est_prefix = $config->EstimateNumberPrefix;
80
                $number = $item->Number;
81
82
                // Strip off current prefix and convert to a ref
83
                if ($item instanceof Invoice) {
84
                    $ref = str_replace($inv_prefix . "-", "", $number);
85
                    $ref = str_replace('-', '', $ref);
86
                    $item->Ref = (int)$ref;
0 ignored issues
show
Bug Best Practice introduced by
The property Ref does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
87
                    $item->Prefix = $inv_prefix;
0 ignored issues
show
Bug Best Practice introduced by
The property Prefix does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
88
                } else {
89
                    $ref = str_replace($est_prefix . "-", "", $number);
90
                    $ref = str_replace('-', '', $ref);
91
                    $item->Ref = (int)$ref;
92
                    $item->Prefix = $est_prefix;
93
                }
94
95
                $item->Number = null;
0 ignored issues
show
Bug Best Practice introduced by
The property Number does not exist on SilverCommerce\OrdersAdmin\Model\Invoice. Since you implemented __set, consider adding a @property annotation.
Loading history...
96
                $item->write();
97
            }
98
            $i++;
99
            $this->log('- '.$i.'/'.$count.' items migrated.', true);
0 ignored issues
show
Unused Code introduced by
The call to SilverCommerce\OrdersAdm...ersMigrationTask::log() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
            $this->/** @scrutinizer ignore-call */ 
100
                   log('- '.$i.'/'.$count.' items migrated.', true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
100
        }
101
102
        unset($items);
103
104
        // Change price/tax on line items to use new fields from extension
105
        $items = LineItem::get();
106
        $count = $items->count();
107
        $this->log("Migrating {$count} Line Items");
108
        $i = 0;
109
110
        foreach ($items as $item) {
111
            $write = false;
112
113
            if ((int)$item->Price && (int)$item->BasePrice == 0) {
114
                $item->BasePrice = $item->Price;
115
                $write = true;
116
            }
117
118
            if ($item->TaxID != 0 && $item->TaxRateID == 0) {
119
                $item->TaxRateID = $item->TaxID;
120
                $write = true;
121
            }
122
123
            if ($write) {
124
                $item->write();
125
                $i++;
126
            }
127
        }
128
129
        unset($items);
130
        
131
        $this->log("Migrated {$i} Line Item Customisations");
132
133
        // Change price/tax on line items to use new fields from extension
134
        $items = LineItemCustomisation::get();
135
        $count = $items->count();
136
        $this->log("Migrating {$count} Line Items");
137
        $i = 0;
138
139
        foreach ($items as $item) {
140
            $write = false;
141
142
            if ((int)$item->Price && (int)$item->BasePrice == 0) {
143
                $item->BasePrice = $item->Price;
144
                $write = true;
145
            }
146
147
            if ($write) {
148
                $item->write();
149
                $i++;
150
            }
151
        }
152
153
        unset($items);
154
        
155
        $this->log("Migrated {$i} Line Items");
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function down()
162
    {
163
        $zones = Zone::get();
0 ignored issues
show
Unused Code introduced by
The assignment to $zones is dead and can be removed.
Loading history...
164
165
        $this->log('No Downgrade Required');
166
    }
167
168
    /**
169
     * @param string $text
170
     */
171
    protected function log($text)
172
    {
173
        if (Controller::curr() instanceof DatabaseAdmin) {
174
            DB::alteration_message($text, 'obsolete');
175
        } elseif (Director::is_cli()) {
176
            echo $text . "\n";
177
        } else {
178
            echo $text . "<br/>";
179
        }
180
    }
181
}
182