Completed
Push — master ( 20efb0...a2cc06 )
by Hamish
29s
created

GroupCsvBulkLoader::processRecord()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 20
c 1
b 0
f 0
nc 12
nop 4
dl 0
loc 38
rs 4.909
1
<?php
2
3
namespace SilverStripe\Security;
4
5
6
use SilverStripe\ORM\DataObject;
7
use CsvBulkLoader;
8
9
/**
10
 * @todo Migrate Permission->Arg and Permission->Type values
11
 *
12
 * @package framework
13
 * @subpackage security
14
 */
15
class GroupCsvBulkLoader extends CsvBulkLoader {
16
17
	public $duplicateChecks = array(
18
		'Code' => 'Code',
19
	);
20
21
	public function __construct($objectClass = null) {
22
		if(!$objectClass) $objectClass = 'SilverStripe\\Security\\Group';
23
24
		parent::__construct($objectClass);
25
	}
26
27
	public function processRecord($record, $columnMap, &$results, $preview = false) {
28
		// We match by 'Code', the ID property is confusing the importer
29
		if(isset($record['ID'])) unset($record['ID']);
30
31
		$objID = parent::processRecord($record, $columnMap, $results, $preview);
32
33
		$group = DataObject::get_by_id($this->objectClass, $objID);
34
		// set group hierarchies - we need to do this after all records
35
		// are imported to avoid missing "early" references to parents
36
		// which are imported later on in the CSV file.
37
		if(isset($record['ParentCode']) && $record['ParentCode']) {
38
			$parentGroup = DataObject::get_one('SilverStripe\\Security\\Group', array(
39
				'"Group"."Code"' => $record['ParentCode']
40
			));
41
			if($parentGroup) {
42
				$group->ParentID = $parentGroup->ID;
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<SilverStripe\ORM\DataObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
				$group->write();
44
			}
45
		}
46
47
		// set permission codes - these are all additive, meaning
48
		// existing permissions arent cleared.
49
		if(isset($record['PermissionCodes']) && $record['PermissionCodes']) {
50
			foreach(explode(',', $record['PermissionCodes']) as $code) {
51
				$p = DataObject::get_one('SilverStripe\\Security\\Permission', array(
52
					'"Permission"."Code"' => $code,
53
					'"Permission"."GroupID"' => $group->ID
54
				));
55
				if(!$p) {
56
					$p = new Permission(array('Code' => $code));
57
					$p->write();
58
				}
59
				$group->Permissions()->add($p);
60
			}
61
		}
62
63
		return $objID;
64
	}
65
66
}
67