LocationCsvBulkLoader   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 30
c 0
b 0
f 0
dl 0
loc 87
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getEscape() 0 3 1
A getCountryByName() 0 14 4
A getCategoryByName() 0 13 3
A processRecord() 0 6 1
1
<?php
2
3
namespace Dynamic\Locator\Bulkloader;
4
5
use Dynamic\Locator\Model\LocationCategory;
6
use phpDocumentor\Reflection\Location;
7
use SilverStripe\Dev\CsvBulkLoader;
8
use SilverStripe\Core\Convert;
9
use SilverStripe\i18n\Data\Intl\IntlLocales;
10
11
/**
12
 * Class LocationCsvBulkLoader
13
 * @package Dynamic\Locator
14
 */
15
class LocationCsvBulkLoader extends CsvBulkLoader
16
{
17
18
    /**
19
     * @var array
20
     */
21
    public $columnMap = array(
22
        'Name' => 'Title',
23
        'EmailAddress' => 'Email',
24
        'Categories' => '->getCategoryByName',
25
        'Import_ID' => 'Import_ID',
26
        'Country' => '->getCountryByName',
27
    );
28
29
    /**
30
     * @var array
31
     */
32
    public $duplicateChecks = array(
33
        'Import_ID' => 'Import_ID'
34
    );
35
36
    /**
37
     * @param $val
38
     * @return string|string[]|null
39
     */
40
    public function getEscape($val)
41
    {
42
        return preg_replace("/\r|\n/", "", $val);
43
    }
44
45
    /**
46
     * @param $obj
47
     * @param $val
48
     * @param $record
49
     * @throws \SilverStripe\ORM\ValidationException
50
     */
51
    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

51
    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...
52
    {
53
        $val = Convert::raw2sql($val);
54
        $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

54
        $parts = explode(', ', /** @scrutinizer ignore-type */ $val);
Loading history...
55
56
        foreach ($parts as $part) {
57
            $category = LocationCategory::get()->filter(array('Name' => $part))->first();
58
            if (!$category) {
59
                $category = LocationCategory::create();
60
                $category->Name = $part;
61
                $category->write();
62
            }
63
            $obj->Categories()->add($category);
64
        }
65
    }
66
67
    /**
68
     * @param $obj
69
     * @param $val
70
     * @param $record
71
     */
72
    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

72
    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...
73
    {
74
        $val = $this->getEscape($val);
75
        $countries = IntlLocales::singleton()->getCountries();
76
77
        if (strlen((string)$val) == 2) {
78
            $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

78
            $val = strtolower(/** @scrutinizer ignore-type */ $val);
Loading history...
79
            if (array_key_exists($val, $countries)) {
80
                $obj->Country = $val;
81
            }
82
        }
83
84
        if (in_array($val, $countries)) {
85
            $obj->Country = array_search($val, $countries);
86
        }
87
    }
88
89
    /**
90
     * @param array $record
91
     * @param array $columnMap
92
     * @param \SilverStripe\Dev\BulkLoader_Result $results
93
     * @param bool $preview
94
     * @return int|void
95
     */
96
    protected function processRecord($record, $columnMap, &$results, $preview = false)
97
    {
98
        $objID = parent::processRecord($record, $columnMap, $results, $preview = false);
99
100
        $location = Location::get()->byID($objID);
0 ignored issues
show
Bug introduced by
The method get() does not exist on phpDocumentor\Reflection\Location. ( Ignorable by Annotation )

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

100
        $location = Location::/** @scrutinizer ignore-call */ get()->byID($objID);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
        $location->publishSingle();
102
    }
103
}
104