ExtracterHookDispatcher::extractOutput()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
4
5
use Consolidation\AnnotatedCommand\Hooks\ExtractOutputInterface;
6
use Consolidation\AnnotatedCommand\Hooks\HookManager;
7
use Consolidation\AnnotatedCommand\OutputDataInterface;
8
9
/**
10
 * Call hooks
11
 */
12 View Code Duplication
class ExtracterHookDispatcher extends HookDispatcher implements ExtractOutputInterface
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
     * Convert the result object to printable output in
16
     * structured form.
17
     */
18
    public function extractOutput($result)
19
    {
20
        if ($result instanceof OutputDataInterface) {
21
            return $result->getOutputData();
22
        }
23
24
        $hooks = [
25
            HookManager::EXTRACT_OUTPUT,
26
        ];
27
        $extractors = $this->getHooks($hooks);
28
        foreach ($extractors as $extractor) {
29
            $structuredOutput = $this->callExtractor($extractor, $result);
30
            if (isset($structuredOutput)) {
31
                return $structuredOutput;
32
            }
33
        }
34
35
        return $result;
36
    }
37
38
    protected function callExtractor($extractor, $result)
39
    {
40
        if ($extractor instanceof ExtractOutputInterface) {
41
            return $extractor->extractOutput($result);
42
        }
43
        if (is_callable($extractor)) {
44
            return $extractor($result);
45
        }
46
    }
47
}
48