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.
Completed
Push — develop ( a5be90...e1de2b )
by David
11:29
created

WorkflowManager::handle()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
rs 8.6845
c 1
b 0
f 0
cc 4
eloc 14
nc 3
nop 3
1
<?php
2
3
/**
4
 * Workflow library.
5
 *
6
 * @package    workflow
7
 * @author     David Molineus <[email protected]>
8
 * @copyright  2014-2017 netzmacht David Molineus
9
 * @license    LGPL 3.0 https://github.com/netzmacht/workflow
10
 * @filesource
11
 */
12
13
namespace Netzmacht\Workflow\Manager;
14
15
use Assert\Assertion;
16
use Netzmacht\Workflow\Data\EntityId;
17
use Netzmacht\Workflow\Data\StateRepository;
18
use Netzmacht\Workflow\Exception\WorkflowNotFound;
19
use Netzmacht\Workflow\Flow\Exception\FlowException;
20
use Netzmacht\Workflow\Flow\Item;
21
use Netzmacht\Workflow\Flow\Workflow;
22
use Netzmacht\Workflow\Handler\TransitionHandler;
23
use Netzmacht\Workflow\Handler\TransitionHandlerFactory;
24
25
/**
26
 * Class Manager handles a set of workflows.
27
 *
28
 * Usually there will a different workflow manager for different workflow types. The manager is the API entry point
29
 * when using the workflow API.
30
 *
31
 * @package Netzmacht\Workflow
32
 */
33
class WorkflowManager implements Manager
34
{
35
    /**
36
     * The state repository.
37
     *
38
     * @var StateRepository
39
     */
40
    private $stateRepository;
41
42
    /**
43
     * A set of workflows.
44
     *
45
     * @var Workflow[]
46
     */
47
    private $workflows;
48
49
    /**
50
     * A Transition handler factory.
51
     *
52
     * @var TransitionHandlerFactory
53
     */
54
    private $handlerFactory;
55
56
    /**
57
     * Construct.
58
     *
59
     * @param TransitionHandlerFactory $handlerFactory  The transition handler factory.
60
     * @param StateRepository          $stateRepository The state repository.
61
     * @param Workflow[]               $workflows       The set of managed workflows.
62
     */
63
    public function __construct(
64
        TransitionHandlerFactory $handlerFactory,
65
        StateRepository $stateRepository,
66
        $workflows = []
67
    ) {
68
        Assertion::allIsInstanceOf($workflows, Workflow::class);
69
70
        $this->workflows       = $workflows;
71
        $this->handlerFactory  = $handlerFactory;
72
        $this->stateRepository = $stateRepository;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function handle(Item $item, string $transitionName = null, bool $changeWorkflow = false): ?TransitionHandler
79
    {
80
        $entity = $item->getEntity();
81
82
        if (!$this->hasWorkflow($item->getEntityId(), $entity)) {
83
            return null;
84
        }
85
86
        $workflow = $this->getWorkflow($item->getEntityId(), $entity);
87
88
        if ($this->hasWorkflowChanged($item, $workflow, !$changeWorkflow) && $changeWorkflow) {
89
            $item->detach();
90
        }
91
92
        $handler = $this->handlerFactory->createTransitionHandler(
93
            $item,
94
            $workflow,
95
            $transitionName,
96
            $item->getEntityId()->getProviderName(),
97
            $this->stateRepository
98
        );
99
100
        return $handler;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function addWorkflow(Workflow $workflow): Manager
107
    {
108
        $this->workflows[] = $workflow;
109
110
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Netzmacht\Workflow\Manager\WorkflowManager) is incompatible with the return type declared by the interface Netzmacht\Workflow\Manager\Manager::addWorkflow of type self.

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...
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getWorkflow(EntityId $entityId, $entity): Workflow
117
    {
118
        foreach ($this->workflows as $workflow) {
119
            if ($workflow->match($entityId, $entity)) {
120
                return $workflow;
121
            }
122
        }
123
124
        throw WorkflowNotFound::forEntity($entityId);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function getWorkflowByName(string $name): Workflow
131
    {
132
        foreach ($this->workflows as $workflow) {
133
            if ($workflow->getName() == $name) {
134
                return $workflow;
135
            }
136
        }
137
138
        throw WorkflowNotFound::withName($name);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getWorkflowByItem(Item $item): Workflow
145
    {
146
        return $this->getWorkflow($item->getEntityId(), $item->getEntity());
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function hasWorkflow(EntityId $entityId, $entity): bool
153
    {
154
        foreach ($this->workflows as $workflow) {
155
            if ($workflow->match($entityId, $entity)) {
156
                return true;
157
            }
158
        }
159
160
        return false;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getWorkflows(): iterable
167
    {
168
        return $this->workflows;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function createItem(EntityId $entityId, $entity): Item
175
    {
176
        $stateHistory = $this->stateRepository->find($entityId);
177
178
        return Item::reconstitute($entityId, $entity, $stateHistory);
179
    }
180
181
    /**
182
     * Guard that already started workflow is the same which is tried to be ran now.
183
     *
184
     * @param Item     $item     Current workflow item.
185
     * @param Workflow $workflow Selected workflow.
186
     * @param bool     $throw    If true an error is thrown
187
     *
188
     * @throws FlowException If item workflow is not the same as current workflow.
189
     *
190
     * @return bool
191
     */
192
    private function hasWorkflowChanged(Item $item, Workflow $workflow, bool $throw = true): bool
193
    {
194
        if ($item->isWorkflowStarted() && $item->getWorkflowName() != $workflow->getName()) {
195
            $message = sprintf(
196
                'Item "%s" already process workflow "%s" and cannot be handled by "%s"',
197
                $item->getEntityId(),
198
                $item->getWorkflowName(),
199
                $workflow->getName()
200
            );
201
202
            if ($throw) {
203
                throw new FlowException($message);
204
            }
205
206
            return true;
207
        }
208
209
        return false;
210
    }
211
}
212