Taskman   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 87.76%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 44
c 3
b 1
f 0
dl 0
loc 156
ccs 43
cts 49
cp 0.8776
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A loadJsonConfiguration() 0 18 3
A createConfiguration() 0 19 2
A createContainer() 0 11 1
A discoverTasksClasses() 0 8 1
A createJsonConfiguration() 0 6 1
A createDefaultRunner() 0 17 3
A createDefaultApplication() 0 10 1
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
        [$scriptPath] = get_included_files();
50
        $config->set('options.bin', $scriptPath);
51
52 3
        return $config;
53 3
    }
54 3
55
    /**
56
     * Create and configure container.
57 3
     *
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
    public static function createContainer(
67
        InputInterface $input,
68
        OutputInterface $output,
69
        Application $application,
70
        ConfigInterface $config,
71 2
        ClassLoader $classLoader
72
    ) {
73
        $container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader);
74
        $container->get('commandFactory')->setIncludeAllPublicMethods(false);
75
76
        return $container;
77
    }
78 2
79 2
    /**
80
     * @param null|string $appName
81 2
     * @param null|string $appVersion
82
     * @param null|string $workingDir
83
     *
84
     * @return Application
85
     */
86
    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
        $appName = $appName ?? self::APPLICATION_NAME;
89
        $appVersion = $appVersion ?? self::VERSION;
90
91 2
        $app = Robo::createDefaultApplication($appName, $appVersion);
92
93 2
        $app->setAutoExit(false);
94 2
95
        return $app;
96 2
    }
97
98 2
    /**
99 2
     * @param ContainerInterface $container
100
     *
101
     * @throws \Exception
102
     *
103 2
     * @return \Robo\Runner
104 2
     */
105 2
    public static function createDefaultRunner(ContainerInterface $container)
106 2
    {
107 2
        $cwd = getcwd();
108 2
109 2
        $workingDir = $container->get('input')->getParameterOption('--working-dir', $cwd);
110 2
111
        if (null === $workingDir) {
112
            $workingDir = $cwd;
113
        }
114 2
115
        if (false === realpath($workingDir)) {
116 2
            throw new \Exception(sprintf('Working directory "%s" does not exists.', $workingDir));
117
        }
118
119
        return (new \Robo\Runner())
120
            ->setRelativePluginNamespace('Robo\Plugin')
121
            ->setContainer($container);
122
    }
123
124 1
    /**
125
     * @param string[] $paths
126 1
     *   Array of JSON filepaths.
127 1
     *
128 1
     * @return Config
129
     *   A config object.
130
     */
131
    public static function createJsonConfiguration(array $paths)
132
    {
133
        $config = new Config();
134
        self::loadJsonConfiguration($paths, $config);
135
136
        return $config;
137
    }
138 3
139
    /**
140 3
     * @param string $relativeNamespace
141 3
     *
142
     * @return array|string[]
143 3
     */
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
    public static function loadJsonConfiguration(array $paths, ?Config $config): void
161
    {
162
        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 3
        }
168
169 3
        $loader = new JsonConfigLoader();
170
        $processor = new ConfigProcessor();
171
        $processor->add($config->export());
172
173
        foreach ($paths as $path) {
174
            $processor->extend($loader->load($path));
175
        }
176 3
177 3
        $config->replace($processor->export());
178 3
    }
179
}
180