Passed
Pull Request — master (#223)
by Nic
03:06
created

EmailAddressTask::run()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 14
rs 9.9332
cc 3
nc 1
nop 1
1
<?php
2
3
namespace Dynamic\Locator\Tasks;
4
5
use Dynamic\Locator\Model\Location;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Dev\BuildTask;
8
9
class EmailAddressTask extends BuildTask
10
{
11
    protected $title = 'Email Address Task'; // title of the script
12
    protected $description = "Convert depreciated 'Email Address' field to new 'Email' field.";
13
14
    public function run($request)
15
    {
16
        Config::inst()->update('DataObject', 'validation_enabled', false);
0 ignored issues
show
Bug introduced by
The method update() does not exist on SilverStripe\Config\Coll...nfigCollectionInterface. It seems like you code against a sub-type of SilverStripe\Config\Coll...nfigCollectionInterface such as SilverStripe\Config\Coll...\MemoryConfigCollection. ( Ignorable by Annotation )

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

16
        Config::inst()->/** @scrutinizer ignore-call */ update('DataObject', 'validation_enabled', false);
Loading history...
17
        $ct = 0;
18
        $updateEmail = function ($location) use (&$ct) {
19
            if (!$location->Email && $location->EmailAddress) {
20
                $location->Email = $location->EmailAddress;
21
                $location->write();
22
                ++$ct;
23
            }
24
        };
25
        Location::get()->each($updateEmail);
26
        Config::inst()->update('DataObject', 'validation_enabled', true);
27
        echo '<p>'.$ct.' Locations updated</p>';
28
    }
29
}
30