Passed
Pull Request — master (#9)
by Pol
11:11 queued 09:16
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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
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
        [$scriptPath] = get_included_files();
50 3
        $config->set('options.bin', $scriptPath);
51
52 3
        return $config;
53
    }
54
55
    /**
56
     * Create and configure container.
57
     *
58
     * @param InputInterface $input
59
     * @param OutputInterface $output
60
     * @param Application $application
61
     * @param ConfigInterface $config
62
     * @param ClassLoader $classLoader
63
     *
64
     * @return Container|\League\Container\ContainerInterface
65
     */
66 2
    public static function createContainer(
67
        InputInterface $input,
68
        OutputInterface $output,
69
        Application $application,
70
        ConfigInterface $config,
71
        ClassLoader $classLoader
72
    ) {
73 2
        $container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader);
74 2
        $container->get('commandFactory')->setIncludeAllPublicMethods(false);
75
76 2
        return $container;
77
    }
78
79
    /**
80
     * @param null|string $appName
81
     * @param null|string $appVersion
82
     * @param null|string $workingDir
83
     *
84
     * @return Application
85
     */
86 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

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