GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

WorkflowBulkLoader   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 86
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A preview() 0 4 1
A processAll() 0 12 2
A processRecord() 0 31 2
A createImport() 0 11 1
1
<?php
2
3
namespace Symbiote\AdvancedWorkflow\Dev;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Dev\BulkLoader;
8
use SilverStripe\Dev\BulkLoader_Result;
9
use SilverStripe\ORM\ValidationException;
10
use Symbiote\AdvancedWorkflow\Admin\WorkflowDefinitionExporter;
11
use Symbiote\AdvancedWorkflow\Admin\WorkflowDefinitionImporter;
12
use Symbiote\AdvancedWorkflow\DataObjects\ImportedWorkflowTemplate;
13
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowDefinition;
14
use Symbiote\AdvancedWorkflow\Services\WorkflowService;
15
use Symbiote\AdvancedWorkflow\Templates\WorkflowTemplate;
16
17
/**
18
 * Utility class to facilitate a simple YML-import via the standard CMS ImportForm() logic.
19
 *
20
 * @license BSD License (http://silverstripe.org/bsd-license/)
21
 * @package advancedworkflow
22
 */
23
class WorkflowBulkLoader extends BulkLoader
24
{
25
    /**
26
     * @inheritDoc
27
     */
28
    public function preview($filepath)
29
    {
30
        return $this->processAll($filepath, true);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->processAll($filepath, true); (SilverStripe\Dev\BulkLoader_Result) is incompatible with the return type declared by the abstract method SilverStripe\Dev\BulkLoader::preview of type array.

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...
31
    }
32
33
    /**
34
     * @param string $filepath
35
     * @param boolean $preview
36
     */
37
    protected function processAll($filepath, $preview = false)
38
    {
39
        $results = new BulkLoader_Result();
40
41
        try {
42
            $yml = singleton(WorkflowDefinitionImporter::class)->parseYAMLImport($filepath);
43
            $this->processRecord($yml, $this->columnMap, $results, $preview);
44
            return $results;
45
        } catch (ValidationException $e) {
46
            return new BulkLoader_Result();
47
        }
48
    }
49
50
    /**
51
     * @param array $record
52
     * @param array $columnMap
53
     * @param BulkLoader_Result $results
54
     * @param boolean $preview
55
     * @return number
56
     */
57
    protected function processRecord($record, $columnMap, &$results, $preview = false)
58
    {
59
        $posted = Controller::curr()->getRequest()->postVars();
60
        $default = WorkflowDefinitionExporter::$export_filename_prefix.'0.yml';
0 ignored issues
show
Bug introduced by
The property export_filename_prefix cannot be accessed from this context as it is declared private in class Symbiote\AdvancedWorkflo...kflowDefinitionExporter.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
61
        $filename = (isset($posted['_CsvFile']['name']) ? $posted['_CsvFile']['name'] : $default);
62
63
        // @todo is this the best way to extract records (nested array keys)??
64
        $struct = $record[Injector::class]['ExportedWorkflow'];
65
        $name = $struct['constructor'][0];
66
        $import = $this->createImport($name, $filename, $record);
67
68
        $template = Injector::inst()->createWithArgs(WorkflowTemplate::class, $struct['constructor']);
69
        $template->setStructure($struct['properties']['structure']);
70
71
        $def = WorkflowDefinition::create();
72
        $def->workflowService = singleton(WorkflowService::class);
73
        $def->Template = $template->getName();
0 ignored issues
show
Documentation introduced by
The property Template does not exist on object<Symbiote\Advanced...cts\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...
74
        $obj = $def->workflowService->defineFromTemplate($def, $def->Template);
0 ignored issues
show
Documentation introduced by
The property Template does not exist on object<Symbiote\Advanced...cts\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...
75
76
        $results->addCreated($obj, '');
77
        $objID = $obj->ID;
78
79
        // Update the import
80
        $import->DefinitionID = $objID;
0 ignored issues
show
Documentation introduced by
The property DefinitionID does not exist on object<Symbiote\Advanced...portedWorkflowTemplate>. 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...
81
        $import->write();
82
83
        $obj->destroy();
84
        unset($obj);
85
86
        return $objID;
87
    }
88
89
    /**
90
     * Create the ImportedWorkflowTemplate record for the uploaded YML file.
91
     *
92
     * @param string $name
93
     * @param string $filename
94
     * @param array $record
95
     * @return ImportedWorkflowTemplate $import
96
     */
97
    protected function createImport($name, $filename, $record)
98
    {
99
        // This is needed to feed WorkflowService#getNamedTemplate()
100
        $import = ImportedWorkflowTemplate::create();
101
        $import->Name = $name;
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<Symbiote\Advanced...portedWorkflowTemplate>. 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...
102
        $import->Filename = $filename;
0 ignored issues
show
Documentation introduced by
The property Filename does not exist on object<Symbiote\Advanced...portedWorkflowTemplate>. 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...
103
        $import->Content = serialize($record);
0 ignored issues
show
Documentation introduced by
The property Content does not exist on object<Symbiote\Advanced...portedWorkflowTemplate>. 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...
104
        $import->write();
105
106
        return $import;
107
    }
108
}
109