Passed
Push — master ( bb2359...2e375e )
by
unknown
14:26
created

src/Taskman.php (1 issue)

Severity
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
    public static function createConfiguration($paths)
35 3
    {
36
        // Create a default configuration.
37 3
        $config = Robo::createConfiguration($paths);
38
39
        if (false !== $cwd = getcwd()) {
40 3
            $paths = \PhpTaskman\Core\Config\Config::findFilesToIncludeInConfiguration($cwd);
41
        }
42
43 3
        // Load the configuration.
44 1
        Robo::loadConfiguration(
45
            $paths,
46
            $config
47 3
        );
48
49 3
        return $config;
50
    }
51
52 3
    /**
53 3
     * Create and configure container.
54 3
     *
55
     * @param InputInterface $input
56
     * @param OutputInterface $output
57 3
     * @param Application $application
58
     * @param ConfigInterface $config
59
     * @param ClassLoader $classLoader
60
     *
61
     * @return Container|\League\Container\ContainerInterface
62
     */
63
    public static function createContainer(
64
        InputInterface $input,
65
        OutputInterface $output,
66
        Application $application,
67
        ConfigInterface $config,
68
        ClassLoader $classLoader
69
    ) {
70
        $container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader);
71 2
        $container->get('commandFactory')->setIncludeAllPublicMethods(false);
72
73
        return $container;
74
    }
75
76
    /**
77
     * @param null|string $appName
78 2
     * @param null|string $appVersion
79 2
     * @param null|string $workingDir
80
     *
81 2
     * @return Application
82
     */
83
    public static function createDefaultApplication($appName = null, $appVersion = null, $workingDir = null)
0 ignored issues
show
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
        $appName = $appName ?? self::APPLICATION_NAME;
86
        $appVersion = $appVersion ?? self::VERSION;
87
88
        $app = Robo::createDefaultApplication($appName, $appVersion);
89
90
        $app->setAutoExit(false);
91 2
92
        return $app;
93 2
    }
94 2
95
    /**
96 2
     * @param ContainerInterface $container
97
     *
98 2
     * @throws \Exception
99 2
     *
100
     * @return \Robo\Runner
101
     */
102
    public static function createDefaultRunner(ContainerInterface $container)
103 2
    {
104 2
        $cwd = getcwd();
105 2
106 2
        $workingDir = $container->get('input')->getParameterOption('--working-dir', $cwd);
107 2
108 2
        if (null === $workingDir) {
109 2
            $workingDir = $cwd;
110 2
        }
111
112
        if (false === realpath($workingDir)) {
113
            throw new \Exception(sprintf('Working directory "%s" does not exists.', $workingDir));
114 2
        }
115
116 2
        chdir($workingDir);
117
118
        return (new \Robo\Runner())
119
            ->setRelativePluginNamespace('Robo\Plugin')
120
            ->setContainer($container);
121
    }
122
123
    /**
124 1
     * @param string[] $paths
125
     *   Array of JSON filepaths.
126 1
     *
127 1
     * @return Config
128 1
     *   A config object.
129
     */
130
    public static function createJsonConfiguration(array $paths)
131
    {
132
        $config = new Config();
133
        self::loadJsonConfiguration($paths, $config);
134
135
        return $config;
136
    }
137
138 3
    /**
139
     * @param string $relativeNamespace
140 3
     *
141 3
     * @return array|string[]
142
     */
143 3
    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
    public static function loadJsonConfiguration(array $paths, ?Config $config): void
160
    {
161
        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 3
168
        $loader = new JsonConfigLoader();
169 3
        $processor = new ConfigProcessor();
170
        $processor->add($config->export());
171
172
        foreach ($paths as $path) {
173
            $processor->extend($loader->load($path));
174
        }
175
176 3
        $config->replace($processor->export());
177 3
    }
178
}
179