DefaultWorkflowFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 14
dl 0
loc 94
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A buildBaseWorkflow() 0 11 1
A buildPreviewWorkflow() 0 20 1
A buildRunWorkflow() 0 14 1
A buildDryrunWorkflow() 0 11 1
1
<?php
2
3
namespace Mathielen\ImportEngine\Import\Workflow;
4
5
use Mathielen\DataImport\EventDispatchableWorkflow;
6
use Mathielen\ImportEngine\Import\Import;
7
use Ddeboer\DataImport\Writer\ArrayWriter;
8
use Ddeboer\DataImport\Filter\OffsetFilter;
9
use Mathielen\DataImport\Filter\PriorityCallbackFilter;
10
use Symfony\Component\EventDispatcher\EventDispatcher;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Mathielen\ImportEngine\Import\Run\ImportRunEventSubscriber;
13
use Mathielen\ImportEngine\ValueObject\ImportRun;
14
15
class DefaultWorkflowFactory implements WorkflowFactoryInterface
16
{
17
    /**
18
     * @var EventDispatcherInterface
19
     */
20
    private $eventDispatcher;
21
22 14
    public function __construct(EventDispatcherInterface $eventDispatcher = null)
23
    {
24 14
        if (!$eventDispatcher) {
25 8
            $eventDispatcher = new EventDispatcher();
26
        }
27
28 14
        $this->eventDispatcher = $eventDispatcher;
29 14
    }
30
31
    /**
32
     * @return \Ddeboer\DataImport\Workflow
33
     */
34 14
    private function buildBaseWorkflow(Import $import)
35
    {
36 14
        $workflow = new EventDispatchableWorkflow($import->getSourceStorage()->reader());
37 14
        $workflow->setEventDispatcher($this->eventDispatcher);
38
39 14
        $import->importer()->filters()->apply($workflow);
40 14
        $import->mappings()->apply($workflow, $import->importer()->transformation()->converterProvider());
41 14
        $import->importer()->validation()->apply($workflow);
42
43 14
        return $workflow;
44
    }
45
46
    /**
47
     * (non-PHPdoc).
48
     *
49
     * @see \Mathielen\ImportEngine\Import\Workflow\WorkflowFactoryInterface::buildPreviewWorkflow()
50
     */
51 2
    public function buildPreviewWorkflow(Import $import, array &$previewResult, $offset = 0)
52
    {
53
        //build basics
54 2
        $workflow = $this->buildBaseWorkflow($import);
55
56
        //callback filter for getting the source-data
57 2
        $workflow->addFilter(new PriorityCallbackFilter(function (array $item) use (&$previewResult) {
58 2
            $previewResult['from'] = $item;
59
60 2
            return true;
61 2
        }, 96)); //before validation (64) but after offset (128)
62
63
        //output
64 2
        $workflow->addWriter(new ArrayWriter($previewResult['to']));
65
66
        //preview offset
67 2
        $workflow->addFilter(new OffsetFilter($offset, 1));
68
69 2
        return $workflow;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $workflow; (Mathielen\DataImport\EventDispatchableWorkflow) is incompatible with the return type declared by the interface Mathielen\ImportEngine\I...e::buildPreviewWorkflow of type Mathielen\DataImport\Workflow.

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...
70
    }
71
72
    /**
73
     * (non-PHPdoc).
74
     *
75
     * @see \Mathielen\ImportEngine\Import\Workflow\WorkflowFactoryInterface::buildRunWorkflow()
76
     */
77 11
    public function buildRunWorkflow(Import $import, ImportRun $importRun = null)
78
    {
79
        //build basics
80 11
        $workflow = $this->buildBaseWorkflow($import);
81
82
        //output
83 11
        $workflow->addWriter($import->getTargetStorage()->writer());
84
85
        //collect statistics by default
86 11
        $statisticsCollector = new ImportRunEventSubscriber($import);
87 11
        $this->eventDispatcher->addSubscriber($statisticsCollector);
88
89 11
        return $workflow;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $workflow; (Mathielen\DataImport\EventDispatchableWorkflow) is incompatible with the return type declared by the interface Mathielen\ImportEngine\I...rface::buildRunWorkflow of type Mathielen\DataImport\Workflow.

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...
90
    }
91
92
    /**
93
     * (non-PHPdoc).
94
     *
95
     * @see \Mathielen\ImportEngine\Import\Workflow\WorkflowFactoryInterface::buildDryrunWorkflow()
96
     */
97 3
    public function buildDryrunWorkflow(Import $import, ImportRun $importRun = null)
98
    {
99
        //build basics
100 3
        $workflow = $this->buildBaseWorkflow($import);
101
102
        //collect statistics by default
103 3
        $statisticsCollector = new ImportRunEventSubscriber($import, true);
104 3
        $this->eventDispatcher->addSubscriber($statisticsCollector);
105
106 3
        return $workflow;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $workflow; (Mathielen\DataImport\EventDispatchableWorkflow) is incompatible with the return type declared by the interface Mathielen\ImportEngine\I...ce::buildDryrunWorkflow of type Mathielen\DataImport\Workflow.

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...
107
    }
108
}
109