Passed
Pull Request — master (#7)
by Pol
04:26 queued 02:28
created

Taskman::loadJsonConfiguration()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 18
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core;
6
7
use Composer\Autoload\ClassLoader;
8
use Consolidation\Config\ConfigInterface;
9
use Consolidation\Config\Loader\ConfigProcessor;
10
use League\Container\Container;
11
use League\Container\ContainerInterface;
12
use PhpTaskman\Core\Config\Loader\JsonConfigLoader;
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\Output\OutputInterface;
18
19
/**
20
 * Class Taskman.
21
 */
22
final class Taskman
23
{
24
    public const APPLICATION_NAME = 'Taskman';
25
    public const VERSION = 'dev-master';
26
27
    /**
28
     * Create default configuration.
29
     *
30
     * @param mixed $paths
31
     *
32
     * @return \Consolidation\Config\ConfigInterface
33
     */
34 3
    public static function createConfiguration($paths)
35
    {
36
        // Create a default configuration.
37 3
        $config = Robo::createConfiguration($paths);
38
39 3
        if (false !== $cwd = getcwd()) {
40 3
            $paths = \PhpTaskman\Core\Config\Config::findFilesToIncludeInConfiguration($cwd);
41
        }
42
43
        // Load the configuration.
44 3
        Robo::loadConfiguration(
45 3
            $paths,
46 3
            $config
47
        );
48
49 3
        return $config;
50
    }
51
52
    /**
53
     * Create and configure container.
54
     *
55
     * @param InputInterface $input
56
     * @param OutputInterface $output
57
     * @param Application $application
58
     * @param ConfigInterface $config
59
     * @param ClassLoader $classLoader
60
     *
61
     * @return Container|\League\Container\ContainerInterface
62
     */
63 2
    public static function createContainer(
64
        InputInterface $input,
65
        OutputInterface $output,
66
        Application $application,
67
        ConfigInterface $config,
68
        ClassLoader $classLoader
69
    ) {
70 2
        $container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader);
71 2
        $container->get('commandFactory')->setIncludeAllPublicMethods(false);
72
73 2
        return $container;
74
    }
75
76
    /**
77
     * @param null|string $appName
78
     * @param null|string $appVersion
79
     * @param null|string $workingDir
80
     *
81
     * @return Application
82
     */
83 2
    public static function createDefaultApplication($appName = null, $appVersion = null, $workingDir = null)
0 ignored issues
show
Unused Code introduced by
The parameter $workingDir is not used and could be removed. ( Ignorable by Annotation )

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

83
    public static function createDefaultApplication($appName = null, $appVersion = null, /** @scrutinizer ignore-unused */ $workingDir = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85 2
        $appName = $appName ?? self::APPLICATION_NAME;
86 2
        $appVersion = $appVersion ?? self::VERSION;
87
88 2
        $app = Robo::createDefaultApplication($appName, $appVersion);
89
90 2
        $app->setAutoExit(false);
91
92 2
        return $app;
93
    }
94
95
    /**
96
     * @param ContainerInterface $container
97
     *
98
     * @throws \Exception
99
     *
100
     * @return \Robo\Runner
101
     */
102 1
    public static function createDefaultRunner(ContainerInterface $container)
103
    {
104 1
        $cwd = getcwd();
105
106 1
        $workingDir = $container->get('input')->getParameterOption('--working-dir', $cwd);
107
108 1
        if (null === $workingDir) {
109
            $workingDir = $cwd;
110
        }
111
112 1
        if (false === realpath($workingDir)) {
113
            throw new \Exception(sprintf('Working directory "%s" does not exists.', $workingDir));
114
        }
115
116 1
        chdir($workingDir);
117
118 1
        return (new \Robo\Runner())
119 1
            ->setRelativePluginNamespace('Robo\Plugin')
120 1
            ->setContainer($container);
121
    }
122
123
    /**
124
     * @param string[] $paths
125
     *   Array of JSON filepaths.
126
     *
127
     * @return Config
128
     *   A config object.
129
     */
130 3
    public static function createJsonConfiguration(array $paths)
131
    {
132 3
        $config = new Config();
133 3
        self::loadJsonConfiguration($paths, $config);
134
135 3
        return $config;
136
    }
137
138
    /**
139
     * @param string $relativeNamespace
140
     *
141
     * @return array|string[]
142
     */
143
    public static function discoverTasksClasses($relativeNamespace)
144
    {
145
        /** @var \Robo\ClassDiscovery\RelativeNamespaceDiscovery $discovery */
146
        $discovery = Robo::service('relativeNamespaceDiscovery');
147
        $discovery->setRelativeNamespace($relativeNamespace . '\Task')
148
            ->setSearchPattern('*Task.php');
149
150
        return $discovery->getClasses();
151
    }
152
153
    /**
154
     * @param string[] $paths
155
     *   Array of JSON filepaths.
156
     * @param null|Config $config
157
     *   A config object.
158
     */
159 3
    public static function loadJsonConfiguration(array $paths, ?Config $config): void
160
    {
161 3
        if (null === $config) {
162
            // This needs to be removed when Robo will have the method replace()
163
            // in the ConfigInterface interface.
164
            /** @var Config $config */
165
            $config = Robo::config();
166
        }
167
168 3
        $loader = new JsonConfigLoader();
169 3
        $processor = new ConfigProcessor();
170 3
        $processor->add($config->export());
171
172 3
        foreach ($paths as $path) {
173 3
            $processor->extend($loader->load($path));
174
        }
175
176 3
        $config->replace($processor->export());
177 3
    }
178
}
179