Passed
Pull Request — master (#210)
by Jason
03:12
created

LocationCsvBulkLoader::processRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Dynamic\Locator;
4
5
use SilverStripe\Dev\CsvBulkLoader;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\i18n\Data\Intl\IntlLocales;
8
9
/**
10
 * Class LocationCsvBulkLoader
11
 * @package Dynamic\Locator
12
 */
13
class LocationCsvBulkLoader extends CsvBulkLoader
14
{
15
16
    /**
17
     * @var array
18
     */
19
    public $columnMap = array(
20
        'Name' => 'Title',
21
        'EmailAddress' => 'Email',
22
        'Categories' => '->getCategoryByName',
23
        'Import_ID' => 'Import_ID',
24
        'Country' => '->getCountryByName',
25
    );
26
27
    /**
28
     * @var array
29
     */
30
    public $duplicateChecks = array(
31
        'Import_ID' => 'Import_ID'
32
    );
33
34
    /**
35
     * @param $val
36
     * @return string|string[]|null
37
     */
38
    public function getEscape($val)
39
    {
40
        return preg_replace("/\r|\n/", "", $val);
41
    }
42
43
    /**
44
     * @param $obj
45
     * @param $val
46
     * @param $record
47
     * @throws \SilverStripe\ORM\ValidationException
48
     */
49
    public static function getCategoryByName(&$obj, $val, $record)
0 ignored issues
show
Unused Code introduced by
The parameter $record is not used and could be removed. ( Ignorable by Annotation )

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

49
    public static function getCategoryByName(&$obj, $val, /** @scrutinizer ignore-unused */ $record)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        $val = Convert::raw2sql($val);
52
        $parts = explode(', ', $val);
0 ignored issues
show
Bug introduced by
It seems like $val can also be of type array and array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

52
        $parts = explode(', ', /** @scrutinizer ignore-type */ $val);
Loading history...
53
54
        foreach ($parts as $part) {
55
            $category = LocationCategory::get()->filter(array('Name' => $part))->first();
56
            if (!$category) {
57
                $category = LocationCategory::create();
58
                $category->Name = $part;
59
                $category->write();
60
            }
61
            $obj->Categories()->add($category);
62
        }
63
    }
64
65
    /**
66
     * @param $obj
67
     * @param $val
68
     * @param $record
69
     */
70
    public function getCountryByName(&$obj, $val, $record)
0 ignored issues
show
Unused Code introduced by
The parameter $record is not used and could be removed. ( Ignorable by Annotation )

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

70
    public function getCountryByName(&$obj, $val, /** @scrutinizer ignore-unused */ $record)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        $val = $this->getEscape($val);
73
        $countries = IntlLocales::singleton()->getCountries();
74
75
        if (strlen((string)$val) == 2) {
76
            $val = strtolower($val);
0 ignored issues
show
Bug introduced by
It seems like $val can also be of type string[]; however, parameter $str of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

76
            $val = strtolower(/** @scrutinizer ignore-type */ $val);
Loading history...
77
            if (array_key_exists($val, $countries)) {
78
                $obj->Country = $val;
79
            }
80
        }
81
82
        if (in_array($val, $countries)) {
83
            $obj->Country = array_search($val, $countries);
84
        }
85
    }
86
87
    /**
88
     * @param array $record
89
     * @param array $columnMap
90
     * @param \SilverStripe\Dev\BulkLoader_Result $results
91
     * @param bool $preview
92
     * @return int|void
93
     */
94
    protected function processRecord($record, $columnMap, &$results, $preview = false)
95
    {
96
        $objID = parent::processRecord($record, $columnMap, $results, $preview = false);
97
98
        $location = Location::get()->byID($objID);
99
        $location->publishSingle();
100
    }
101
}
102