Completed
Pull Request — master (#60)
by Greg
02:03
created

HelpDocument   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 57
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getDomData() 0 4 1
A generateBaseHelpDom() 0 7 1
A alterHelpDocument() 0 7 2
1
<?php
2
namespace Consolidation\AnnotatedCommand\Help;
3
4
use Consolidation\OutputFormatters\StructuredData\Xml\DomDataInterface;
5
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Descriptor\XmlDescriptor;
8
9
class HelpDocument implements DomDataInterface
10
{
11
    /** var Command */
12
    protected $command;
13
14
    /** var \DOMDocument */
15
    protected $dom;
16
17
    /**
18
     * Create a help document from a Symfony Console command
19
     */
20
    public function __construct(Command $command)
21
    {
22
        $dom = self::generateBaseHelpDom($command);
23
        $dom = self::alterHelpDocument($command, $dom);
24
25
        $this->command = $command;
26
        $this->dom = $dom;
27
    }
28
29
    /**
30
     * Convert data into a \DomDocument.
31
     *
32
     * @return \DomDocument
33
     */
34
    public function getDomData()
35
    {
36
        return $this->dom;
37
    }
38
39
    /**
40
     * Create the base help DOM prior to alteration by the Command object.
41
     * @param Command $command
42
     * @return \DomDocument
43
     */
44
    private static function generateBaseHelpDom(Command $command)
45
    {
46
        // Use Symfony to generate xml text. If other formats are
47
        // requested, convert from xml to the desired form.
48
        $descriptor = new XmlDescriptor();
49
        return $descriptor->getCommandDocument($command);
50
    }
51
52
    /**
53
     * Alter the DOM document per the command object
54
     * @param Command $command
55
     * @param \DomDocument $dom
56
     * @return \DomDocument
57
     */
58
    private static function alterHelpDocument(Command $command, \DomDocument $dom)
59
    {
60
        if ($command instanceof HelpDocumentAlter) {
61
            $dom = $command->helpAlter($dom);
62
        }
63
        return $dom;
64
    }
65
}
66