Completed
Push — develop ( 73bd0a...ccb498 )
by Tom
05:04
created

DiFileWriter::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Developer\Console\Util\Config;
4
5
use N98\Magento\Command\Developer\Console\Util\Xml;
6
7
class DiFileWriter extends FileWriter
8
{
9
    /**
10
     * @var string
11
     */
12
    protected static $defaultXml = <<<'XML'
13
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
14
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
15
</config>
16
XML;
17
18
    /**
19
     * @param string $commandName
20
     * @param string $commandClass
21
     *
22
     * @return DiFileWriter
0 ignored issues
show
Documentation introduced by
Should the return type not be DiFileWriter|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
23
     */
24
    public function addConsoleCommand($commandName, $commandClass)
25
    {
26
        /*<type name="">
27
            <arguments>
28
                <argument name="commands" xsi:type="array">
29
                    <item name="$name$Command" xsi:type="object">$class$</item>
30
                </argument>
31
            </arguments>
32
        </type>*/
33
34
        $xpath = new \DOMXPath($this);
35
36
        $commandAlreadyExistsQuery = $xpath->query("//item[text()='$commandClass']");
37
        if ($commandAlreadyExistsQuery->length > 0) {
38
            return;
39
        }
40
41
        $argumentElementQuery = $xpath->query('//type[@name="Magento\Framework\Console\CommandList"]/arguments/argument');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
42
        if ($argumentElementQuery->length > 0) {
43
            $argumentElement = $argumentElementQuery->item(0);
44
        } else {
45
            $typeElement = $this->createElement('type');
46
            $typeElement->setAttribute('name', 'Magento\Framework\Console\CommandList');
47
48
            $argumentsElement = $this->createElement('arguments');
49
            $typeElement->appendChild($argumentsElement);
50
51
            $argumentElement = $this->createElement('argument');
52
            $argumentElement->setAttribute('name', 'commands');
53
            $argumentElement->setAttribute('xsi:type', 'array');
54
            $argumentsElement->appendChild($argumentElement);
55
56
            $this->documentElement->appendChild($typeElement);
57
        }
58
59
        $itemElement = $this->createElement('item', $commandClass);
60
        $itemElement->setAttribute('name', $commandName);
61
        $itemElement->setAttribute('xsi:type', 'object');
62
        $argumentElement->appendChild($itemElement);
63
64
        return $this;
65
    }
66
67
    public function save($filename, $options = null)
68
    {
69
        if (parent::save($filename, $options)) {
70
            // Reformat
71
            $xml = file_get_contents($filename);
72
            $xml = Xml::formatString($xml);
73
74
            \file_put_contents($filename, $xml);
75
        }
76
    }
77
}
78