EmailAddressTask   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 17 3
1
<?php
2
3
namespace Dynamic\Locator\Tasks;
4
5
use Dynamic\Locator\Location;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Dev\BuildTask;
9
10
/**
11
 * Class EmailAddressTask
12
 * @package Dynamic\Locator\Tasks
13
 */
14
class EmailAddressTask extends BuildTask
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $title = 'Email Address Task'; // title of the script
20
21
    /**
22
     * @var string
23
     */
24
    protected $description = "Convert depreciated 'Email Address' field to new 'Email' field.";
25
26
    /**
27
     * @param HTTPRequest $request
28
     */
29
    public function run($request)
30
    {
31
        Config::modify()->set('DataObject', 'validation_enabled', false);
32
33
        $ct = 0;
34
        $updateEmail = function ($location) use (&$ct) {
35
            if (!$location->Email && $location->EmailAddress) {
36
                $location->Email = $location->EmailAddress;
37
                $location->write();
38
                ++$ct;
39
            }
40
        };
41
42
        Location::get()->each($updateEmail);
43
        Config::modify()->set('DataObject', 'validation_enabled', true);
44
45
        echo '<p>' . $ct . ' Locations updated</p>';
46
    }
47
}
48