WorkflowBulkLoader::processAll()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 2
eloc 8
nc 3
nop 2
1
<?php
2
/**
3
 * Utility class to facilitate a simple YML-import via the standard CMS ImportForm() logic.
4
 *
5
 * @license BSD License (http://silverstripe.org/bsd-license/)
6
 * @package advancedworkflow
7
 */
8
class WorkflowBulkLoader extends BulkLoader {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
	
10
	/**
11
	 * @inheritDoc
12
	 */
13
	public function preview($filepath) {
14
		return $this->processAll($filepath, true);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->processAll($filepath, true); (BulkLoader_Result) is incompatible with the return type of the parent method BulkLoader::preview of type array|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
15
	}
16
	
17
	/**
18
	 * @param string $filepath
19
	 * @param boolean $preview
20
	 */
21
	protected function processAll($filepath, $preview = false) {
22
		$results = new BulkLoader_Result();
23
		
24
		try {
25
			$yml = singleton('WorkflowDefinitionImporter')->parseYAMLImport($filepath);
26
			$this->processRecord($yml, $this->columnMap, $results, $preview);
27
			return $results;			
28
		} catch(ValidationException $e) {
29
			return new BulkLoader_Result();
30
		}
31
	}
32
	
33
	/**
34
	 * @param array $record
35
	 * @param array $columnMap
36
	 * @param BulkLoader_Result $results
37
	 * @param boolean $preview
38
	 * @return number
39
	 */
40
	protected function processRecord($record, $columnMap, &$results, $preview = false) {
41
		$posted = Controller::curr()->getRequest()->postVars();
42
		$default = WorkflowDefinitionExporter::$export_filename_prefix.'0.yml';
43
		$filename = (isset($posted['_CsvFile']['name']) ? $posted['_CsvFile']['name'] : $default);
44
		
45
		// @todo is this the best way to extract records (nested array keys)??
46
		$struct = $record['Injector']['ExportedWorkflow'];
47
		$name = $struct['constructor'][0];		
48
		$import = $this->createImport($name, $filename, $record);
49
		
50
		$template = Injector::inst()->createWithArgs('WorkflowTemplate', $struct['constructor']);
51
		$template->setStructure($struct['properties']['structure']);
52
53
		$def = WorkflowDefinition::create();
54
		$def->workflowService = singleton('WorkflowService');
55
		$def->Template = $template->getName();
0 ignored issues
show
Documentation introduced by
The property Template does not exist on object<WorkflowDefinition>. 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...
56
		$obj = $def->workflowService->defineFromTemplate($def, $def->Template);
0 ignored issues
show
Documentation introduced by
The property Template does not exist on object<WorkflowDefinition>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
57
		
58
		$results->addCreated($obj, '');
59
		$objID = $obj->ID;
60
		
61
		// Update the import
62
		$import->DefinitionID = $objID;
0 ignored issues
show
Documentation introduced by
The property DefinitionID does not exist on object<ImportedWorkflowTemplate>. 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...
63
		$import->write();
64
		
65
		$obj->destroy();
66
		unset($obj);
67
		
68
		return $objID;
69
	}
70
	
71
	/**
72
	 * Create the ImportedWorkflowTemplate record for the uploaded YML file.
73
	 * 
74
	 * @param string $name
75
	 * @param string $filename
76
	 * @param array $record
77
	 * @return ImportedWorkflowTemplate $import
78
	 */
79
	protected function createImport($name, $filename, $record) {
80
		// This is needed to feed WorkflowService#getNamedTemplate()
81
		$import = ImportedWorkflowTemplate::create();
82
		$import->Name = $name;
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<ImportedWorkflowTemplate>. 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...
83
		$import->Filename = $filename;
0 ignored issues
show
Documentation introduced by
The property Filename does not exist on object<ImportedWorkflowTemplate>. 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...
84
		$import->Content = serialize($record);
0 ignored issues
show
Documentation introduced by
The property Content does not exist on object<ImportedWorkflowTemplate>. 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...
85
		$import->write();
86
		
87
		return $import;
88
	}
89
}
90