Completed
Push — develop ( eeebfd...e7b4cf )
by Andrea Marco
06:26
created

Workflow   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 7
c 6
b 1
f 1
lcom 1
cbo 0
dl 0
loc 129
ccs 25
cts 25
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __call() 0 8 1
A failIfNotExists() 0 8 2
A dispatchWorkflow() 0 12 1
A resolveRequest() 0 8 2
1
<?php
2
3
namespace Cerbero\Workflow;
4
5
use Cerbero\Workflow\Inflectors\InflectorInterface;
6
use Cerbero\Workflow\Repositories\PipelineRepositoryInterface;
7
use Cerbero\Workflow\Wrappers\DispatcherInterface;
8
use Illuminate\Contracts\Container\Container;
9
10
/**
11
 * Hub of the pipelines.
12
 *
13
 * @author	Andrea Marco Sartori
14
 */
15
class Workflow
16
{
17
    /**
18
     * @author	Andrea Marco Sartori
19
     *
20
     * @var Cerbero\Workflow\Repositories\PipelineRepositoryInterface $pipelines	Pipelines repository.
21
     */
22
    protected $pipelines;
23
24
    /**
25
     * @author	Andrea Marco Sartori
26
     *
27
     * @var Cerbero\Workflow\Inflectors\InflectorInterface $inflector	Inflector.
28
     */
29
    protected $inflector;
30
31
    /**
32
     * @author	Andrea Marco Sartori
33
     *
34
     * @var Illuminate\Contracts\Container\Container $container	Service container.
35
     */
36
    protected $container;
37
38
    /**
39
     * @author	Andrea Marco Sartori
40
     *
41
     * @var Cerbero\Workflow\Wrappers\DispatcherInterface $dispatcher	Bus dispatcher.
42
     */
43
    protected $dispatcher;
44
45
    /**
46
     * Set the dependencies.
47
     *
48
     * @author	Andrea Marco Sartori
49
     *
50
     * @param Cerbero\Workflow\Repositories\PipelineRepositoryInterface $pipelines
51
     * @param Cerbero\Workflow\Wrappers\DispatcherInterface             $dispatcher
52
     * @param Cerbero\Workflow\Inflectors\InflectorInterface            $inflector
53
     * @param Illuminate\Contracts\Container\Container                  $container
54
     *
55
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
56
     */
57 12
    public function __construct(
58
        PipelineRepositoryInterface $pipelines,
59
        InflectorInterface $inflector,
60
        Container $container,
61
        DispatcherInterface $dispatcher
62
    ) {
63 12
        $this->pipelines = $pipelines;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pipelines of type object<Cerbero\Workflow\...ineRepositoryInterface> is incompatible with the declared type object<Cerbero\Workflow\...ineRepositoryInterface> of property $pipelines.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64 12
        $this->inflector = $inflector;
0 ignored issues
show
Documentation Bug introduced by
It seems like $inflector of type object<Cerbero\Workflow\...ors\InflectorInterface> is incompatible with the declared type object<Cerbero\Workflow\...ors\InflectorInterface> of property $inflector.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65 12
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container of type object<Illuminate\Contracts\Container\Container> is incompatible with the declared type object<Cerbero\Workflow\...ts\Container\Container> of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66 12
        $this->dispatcher = $dispatcher;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dispatcher of type object<Cerbero\Workflow\...rs\DispatcherInterface> is incompatible with the declared type object<Cerbero\Workflow\...rs\DispatcherInterface> of property $dispatcher.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
67 12
    }
68
69
    /**
70
     * Dynamically call pipelines.
71
     *
72
     * @author	Andrea Marco Sartori
73
     *
74
     * @param string $name
75
     * @param array  $arguments
76
     *
77
     * @return mixed
78
     */
79 9
    public function __call($name, $arguments)
80
    {
81 9
        $this->failIfNotExists($name);
82
83 6
        $this->inflector->of($name);
84
85 6
        return $this->dispatchWorkflow($name);
86
    }
87
88
    /**
89
     * Throw an exception if the given workflow does not exist.
90
     *
91
     * @author	Andrea Marco Sartori
92
     *
93
     * @param string $workflow
94
     *
95
     * @return void
96
     */
97 9
    protected function failIfNotExists($workflow)
98
    {
99 9
        if (!$this->pipelines->exists($workflow)) {
100 3
            $error = "The workflow [$workflow] does not exist.";
101
102 3
            throw new \BadFunctionCallException($error);
103
        }
104 6
    }
105
106
    /**
107
     * Dispatch the given workflow.
108
     *
109
     * @author	Andrea Marco Sartori
110
     *
111
     * @param string $workflow
112
     *
113
     * @return mixed
114
     */
115 6
    protected function dispatchWorkflow($workflow)
116
    {
117 6
        $job = $this->inflector->getJob();
118
119 6
        $request = $this->resolveRequest();
120
121 6
        $pipes = $this->pipelines->getPipesByPipeline($workflow);
122
123 6
        $parameters = $this->container->make('router')->current()->parameters();
124
125 6
        return $this->dispatcher->pipeThrough($pipes)->dispatchFrom($job, $request, $parameters);
126
    }
127
128
    /**
129
     * Resolve the apter request.
130
     *
131
     * @author	Andrea Marco Sartori
132
     *
133
     * @return Illuminate\Http\Request
134
     */
135 6
    protected function resolveRequest()
136
    {
137 6
        if (class_exists($request = $this->inflector->getRequest())) {
138 3
            return $this->container->make($request);
139
        }
140
141 3
        return $this->container->make('Illuminate\Http\Request');
142
    }
143
}
144