Passed
Push — master ( bf93db...a9da14 )
by Pol
02:24
created

Taskman::createJsonConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core;
6
7
use Composer\Autoload\ClassLoader;
8
use Consolidation\Config\Loader\ConfigProcessor;
9
use League\Container\Container;
10
use League\Container\ContainerInterface;
11
use PhpTaskman\Core\Config\Loader\JsonConfigLoader;
12
use PhpTaskman\Core\Services\Composer;
0 ignored issues
show
Bug introduced by
The type PhpTaskman\Core\Services\Composer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Robo\Application;
14
use Robo\Config\Config;
15
use Robo\Robo;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Class Taskman.
22
 */
23
final class Taskman
24
{
25
    public const APPLICATION_NAME = 'Taskman';
26
    public const VERSION = 'dev-master';
27
28
    /**
29
     * Create default configuration.
30
     *
31
     * @param null|mixed $workingDir
32
     * @param mixed $paths
33
     *
34
     * @return Config
35
     */
36 3
    public static function createConfiguration($paths, $workingDir = null)
37
    {
38 3
        $workingDir = $workingDir ?? \getcwd();
39
40
        // Create a default configuration.
41 3
        $config = Robo::createConfiguration($paths);
42
43
        // Set the variable working_dir.
44 3
        if (false === $workingDir = \realpath($workingDir)) {
45 1
            return $config;
46
        }
47
48 3
        $config->set('taskman.working_dir', \realpath($workingDir));
49
50
        // Load the configuration.
51 3
        Robo::loadConfiguration(
52 3
            \PhpTaskman\Core\Config\Config::findFilesToIncludeInConfiguration($workingDir),
53 3
            $config
54
        );
55
56 3
        return $config;
57
    }
58
59
    /**
60
     * Create and configure container.
61
     *
62
     * @param InputInterface $input
63
     * @param OutputInterface $output
64
     * @param Application $application
65
     * @param Config $config
66
     * @param ClassLoader $classLoader
67
     *
68
     * @return Container|\League\Container\ContainerInterface
69
     */
70 2
    public static function createContainer(
71
        InputInterface $input,
72
        OutputInterface $output,
73
        Application $application,
74
        Config $config,
75
        ClassLoader $classLoader
76
    ) {
77 2
        $container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader);
78 2
        $container->get('commandFactory')->setIncludeAllPublicMethods(false);
79
80 2
        return $container;
81
    }
82
83
    /**
84
     * @param null|string $appName
85
     * @param null|string $appVersion
86
     * @param null|string $workingDir
87
     *
88
     * @return Application
89
     */
90 2
    public static function createDefaultApplication($appName = null, $appVersion = null, $workingDir = null)
91
    {
92 2
        $workingDir = $workingDir ?? \getcwd();
93 2
        $appName = $appName ?? self::APPLICATION_NAME;
94 2
        $appVersion = $appVersion ?? self::VERSION;
95
96 2
        $app = Robo::createDefaultApplication($appName, $appVersion);
97
98
        $app
99 2
            ->getDefinition()
100 2
            ->addOption(
101 2
                new InputOption(
102 2
                    '--working-dir',
103 2
                    null,
104 2
                    InputOption::VALUE_REQUIRED,
105 2
                    'Working directory, defaults to current working directory.',
106 2
                    \realpath($workingDir)
107
                )
108
            );
109
110 2
        $app->setAutoExit(false);
111
112 2
        return $app;
113
    }
114
115
    /**
116
     * @param ContainerInterface $container
117
     *
118
     * @return \Robo\Runner
119
     */
120 1
    public static function createDefaultRunner(ContainerInterface $container)
121
    {
122 1
        return (new \Robo\Runner())
123 1
            ->setRelativePluginNamespace('Robo\Plugin')
124 1
            ->setContainer($container);
125
    }
126
127
    /**
128
     * @param $paths
129
     *
130
     * @return \Robo\Config\Config
131
     */
132 3
    public static function createJsonConfiguration($paths)
133
    {
134 3
        $config = new Config();
135 3
        self::loadJsonConfiguration($paths, $config);
136
137 3
        return $config;
138
    }
139
140
    /**
141
     * @param string $path
142
     *   The directory
143
     *
144
     * @return \Robo\Config\Config
145
     *   The composer object.
146
     */
147 3
    public static function getComposerFromDirectory($path): ?Config
148
    {
149 3
        $composerFile = \realpath($path . '/composer.json');
150
151 3
        if (false === $composerFile) {
152
            return null;
153
        }
154
155 3
        return self::getConfigFromArray(
156 3
            \json_decode(
157 3
                \file_get_contents($composerFile),
158 3
                true
159
            )
160
        );
161
    }
162
163
    /**
164
     * @param array $data
165
     *
166
     * @return \Robo\Config\Config
167
     *   The composer object.
168
     */
169 3
    public static function getConfigFromArray(array $data): Config
170
    {
171 3
        return new Config($data);
172
    }
173
174
    /**
175
     * @param $paths
176
     * @param null $config
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $config is correct as it would always require null to be passed?
Loading history...
177
     */
178 3
    public static function loadJsonConfiguration($paths, $config = null)
179
    {
180 3
        if (null === $config) {
0 ignored issues
show
introduced by
The condition null === $config is always true.
Loading history...
181
            $config = Robo::config();
182
        }
183
184 3
        $loader = new JsonConfigLoader();
185 3
        $processor = new ConfigProcessor();
186 3
        $processor->add($config->export());
187
188 3
        foreach ($paths as $path) {
189 3
            $processor->extend($loader->load($path));
190
        }
191
192 3
        $config->import($processor->export());
0 ignored issues
show
Deprecated Code introduced by
The function Consolidation\Config\ConfigInterface::import() has been deprecated: Use 'replace'. Dflydev\DotAccessData\Data::import() merges, which is confusing, since this method replaces. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

192
        /** @scrutinizer ignore-deprecated */ $config->import($processor->export());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
193 3
    }
194
}
195