Completed
Pull Request — master (#82)
by Greg
02:18
created

determineStatusCode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 8

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 20
loc 20
rs 9.2
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
3
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
4
5
use Consolidation\AnnotatedCommand\ExitCodeInterface;
6
use Consolidation\AnnotatedCommand\Hooks\HookManager;
7
use Consolidation\AnnotatedCommand\Hooks\StatusDeterminerInterface;
8
9
/**
10
 * Call hooks
11
 */
12 View Code Duplication
class StatusDeterminerHookDispatcher extends HookDispatcher implements StatusDeterminerInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    /**
15
     * Call all status determiners, and see if any of them
16
     * know how to convert to a status code.
17
     */
18
    public function determineStatusCode($result)
19
    {
20
        // If the result (post-processing) is an object that
21
        // implements ExitCodeInterface, then we will ask it
22
        // to give us the status code.
23
        if ($result instanceof ExitCodeInterface) {
24
            return $result->getExitCode();
25
        }
26
27
        // If the result does not implement ExitCodeInterface,
28
        // then we'll see if there is a determiner that can
29
        // extract a status code from the result.
30
        $determiners = $this->getStatusDeterminers();
31
        foreach ($determiners as $determiner) {
32
            $status = $this->callDeterminer($determiner, $result);
33
            if (isset($status)) {
34
                return $status;
35
            }
36
        }
37
    }
38
39
    protected function callDeterminer($determiner, $result)
40
    {
41
        if ($determiner instanceof StatusDeterminerInterface) {
42
            return $determiner->determineStatusCode($result);
43
        }
44
        if (is_callable($determiner)) {
45
            return $determiner($result);
46
        }
47
    }
48
49
    protected function getStatusDeterminers()
50
    {
51
        return $this->getHooks(
52
            [
53
                HookManager::STATUS_DETERMINER,
54
            ]
55
        );
56
    }
57
}
58