ConfigurationPublisher::getDefinition()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.392

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 16
cts 20
cp 0.8
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 10
nop 2
crap 7.392
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 */
12
13
namespace Rocketeer\Services\Config\Files;
14
15
use Rocketeer\Services\Config\ConfigurationDefinition;
16
use Rocketeer\Services\Container\Container;
17
use Rocketeer\Traits\ContainerAwareTrait;
18
use Symfony\Component\Config\Definition\ArrayNode;
19
use Symfony\Component\Config\Definition\ConfigurationInterface;
20
use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper;
21
use Symfony\Component\Config\Definition\Dumpers\JsonReferenceDumper;
22
use Symfony\Component\Config\Definition\Dumpers\PhpReferenceDumper;
23
24
/**
25
 * Publishes a ConfigurationDefinition to a folder in a specific format.
26
 */
27
class ConfigurationPublisher
28
{
29
    use ContainerAwareTrait;
30
31
    /**
32
     * @var array
33
     */
34
    public static $formats = [
35
        'php',
36
        'json',
37
        'yaml',
38
    ];
39
40
    /**
41
     * @var ConfigurationInterface
42
     */
43
    protected $definition;
44
45
    /**
46
     * @param Container $container
47
     */
48 4
    public function __construct(Container $container)
49
    {
50 4
        $this->container = $container;
51 4
        $this->definition = new ConfigurationDefinition();
52 4
    }
53
54
    /**
55
     * @param ConfigurationInterface $definition
56
     */
57 3
    public function setDefinition(ConfigurationInterface $definition)
58
    {
59 3
        $this->definition = $definition;
60 3
    }
61
62
    /**
63
     * @param string $format
64
     * @param bool   $consolidated
65
     *
66
     * @return string
67
     */
68 4
    public function publish($format = 'php', $consolidated = false)
69
    {
70 4
        $format = $this->sanitizeFormat($format);
71 4
        $destination = $this->paths->getConfigurationPath();
72
73
        // Create directory
74 4
        if (!$this->files->isDirectory($destination) && !$consolidated) {
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method isDirectory does not exist on object<Rocketeer\Services\Filesystem\Filesystem>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75 4
            $this->files->createDir($destination);
76 4
        }
77
78
        // Consolidate or not configuration
79 4
        if ($consolidated) {
80
            $destination = $this->paths->getRocketeerPath().'/rocketeer.'.$format;
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method getRocketeerPath does not exist on object<Rocketeer\Services\Environment\Pathfinder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
81
        }
82
83
        // Unzip configuration files
84 4
        $this->publishNode($destination, $format);
85
86 4
        return $destination;
87
    }
88
89
    /**
90
     * Publish the configuration somewhere.
91
     *
92
     * @param string      $path
93
     * @param string      $format
94
     * @param string|null $node
95
     */
96 4
    public function publishNode($path, $format = 'php', $node = null)
97
    {
98 4
        if ($this->files->isDirectory($path)) {
0 ignored issues
show
Documentation Bug introduced by Maxime FABRE
The method isDirectory does not exist on object<Rocketeer\Services\Filesystem\Filesystem>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
99 3
            foreach (['config', 'hooks', 'paths', 'remote', 'vcs', 'stages', 'strategies', 'plugins'] as $file) {
100 3
                $this->publishNode($path.'/'.$file.'.'.$format, $format, $file);
101 3
            }
102
103 3
            return;
104
        }
105
106
        // If a single file was passed, infer format from extension
107 4
        $format = $format ?: pathinfo($path, PATHINFO_EXTENSION);
108 4
        $configuration = $this->getDefinition($format, $node);
109
110 4
        $this->files->put($path, $configuration);
111 4
    }
112
113
    /**
114
     * Set the available options and their values.
115
     *
116
     * @param string      $format
117
     * @param string|null $node
118
     *
119
     * @return string
120
     */
121 4
    public function getDefinition($format = 'yml', $node = null)
122
    {
123 4
        switch (mb_strtolower($format)) {
124 4
            case 'json':
125
                $dumper = new JsonReferenceDumper();
126
                break;
127
128 4
            case 'yml':
129 4
            case 'yaml':
130
                $dumper = new YamlReferenceDumper();
131
                break;
132
133 4
            case 'php':
134 4
            default:
135 4
                $dumper = new PhpReferenceDumper();
136 4
                break;
137 4
        }
138
139 4
        $definition = $this->definition;
140 4
        $definition = $definition->getConfigTreeBuilder()->buildTree();
141 4
        if ($node && $definition instanceof ArrayNode) {
142 3
            $definition = $definition->getChildren()[$node];
143 3
        }
144
145 4
        return $dumper->dumpNode($definition);
146
    }
147
148
    /**
149
     * @param string $format
150
     *
151
     * @return string
152
     */
153 4
    protected function sanitizeFormat($format)
154
    {
155 4
        $format = mb_strtolower($format);
156 4
        $format = $format === 'yaml' ? 'yml' : $format;
157
158 4
        return $format;
159
    }
160
}
161