Passed
Push — master ( 461acb...254778 )
by Pol
04:49
created

Taskman   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Test Coverage

Coverage 88.68%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 160
ccs 47
cts 53
cp 0.8868
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A loadJsonConfiguration() 0 15 3
A createConfiguration() 0 23 2
A createContainer() 0 11 1
A discoverTasksClasses() 0 8 1
A createJsonConfiguration() 0 6 1
A createDefaultRunner() 0 5 1
A createDefaultApplication() 0 26 3
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 Robo\Application;
13
use Robo\Config\Config;
14
use Robo\Robo;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
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 null|mixed $workingDir
31
     * @param mixed $paths
32
     *
33
     * @return Config
34
     */
35 3
    public static function createConfiguration($paths, $workingDir = null)
36
    {
37 3
        $workingDir = $workingDir ?? \getcwd();
38
39
        // Create a default configuration.
40 3
        $config = Robo::createConfiguration($paths);
41
42
        // Set the variable working_dir.
43 3
        if (false === $workingDir = \realpath($workingDir)) {
44 1
            return $config;
45
        }
46
47 3
        $config->set('taskman.working_dir', $workingDir);
48
49 3
        $paths = \PhpTaskman\Core\Config\Config::findFilesToIncludeInConfiguration($workingDir);
50
51
        // Load the configuration.
52 3
        Robo::loadConfiguration(
53 3
            $paths,
54 3
            $config
55
        );
56
57 3
        return $config;
58
    }
59
60
    /**
61
     * Create and configure container.
62
     *
63
     * @param InputInterface $input
64
     * @param OutputInterface $output
65
     * @param Application $application
66
     * @param Config $config
67
     * @param ClassLoader $classLoader
68
     *
69
     * @return Container|\League\Container\ContainerInterface
70
     */
71 2
    public static function createContainer(
72
        InputInterface $input,
73
        OutputInterface $output,
74
        Application $application,
75
        Config $config,
76
        ClassLoader $classLoader
77
    ) {
78 2
        $container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader);
79 2
        $container->get('commandFactory')->setIncludeAllPublicMethods(false);
80
81 2
        return $container;
82
    }
83
84
    /**
85
     * @param null|string $appName
86
     * @param null|string $appVersion
87
     * @param null|string $workingDir
88
     *
89
     * @return Application
90
     */
91 2
    public static function createDefaultApplication($appName = null, $appVersion = null, $workingDir = null)
92
    {
93 2
        $appName = $appName ?? self::APPLICATION_NAME;
94 2
        $appVersion = $appVersion ?? self::VERSION;
95
96 2
        $app = Robo::createDefaultApplication($appName, $appVersion);
97
98 2
        if (null === $workingDir || false === $workingDir = \realpath($workingDir)) {
99 1
            $workingDir = \getcwd();
100
        }
101
102
        $app
103 2
            ->getDefinition()
104 2
            ->addOption(
105 2
                new InputOption(
106 2
                    '--working-dir',
107 2
                    null,
108 2
                    InputOption::VALUE_REQUIRED,
109 2
                    'Working directory, defaults to current working directory.',
110 2
                    $workingDir
111
                )
112
            );
113
114 2
        $app->setAutoExit(false);
115
116 2
        return $app;
117
    }
118
119
    /**
120
     * @param ContainerInterface $container
121
     *
122
     * @return \Robo\Runner
123
     */
124 1
    public static function createDefaultRunner(ContainerInterface $container)
125
    {
126 1
        return (new \Robo\Runner())
127 1
            ->setRelativePluginNamespace('Robo\Plugin')
128 1
            ->setContainer($container);
129
    }
130
131
    /**
132
     * @param string[] $paths
133
     *   Array of JSON filepaths.
134
     *
135
     * @return Config
136
     *   A config object.
137
     */
138 3
    public static function createJsonConfiguration(array $paths)
139
    {
140 3
        $config = new Config();
141 3
        self::loadJsonConfiguration($paths, $config);
142
143 3
        return $config;
144
    }
145
146
    /**
147
     * @param string $relativeNamespace
148
     *
149
     * @return array|string[]
150
     */
151
    public static function discoverTasksClasses($relativeNamespace)
152
    {
153
        /** @var \Robo\ClassDiscovery\RelativeNamespaceDiscovery $discovery */
154
        $discovery = Robo::service('relativeNamespaceDiscovery');
155
        $discovery->setRelativeNamespace($relativeNamespace . '\Task')
156
            ->setSearchPattern('*Task.php');
157
158
        return $discovery->getClasses();
159
    }
160
161
    /**
162
     * @param string[] $paths
163
     *   Array of JSON filepaths.
164
     * @param null|Config $config
165
     *   A config object.
166
     */
167 3
    public static function loadJsonConfiguration(array $paths, ?Config $config)
168
    {
169 3
        if (null === $config) {
170
            $config = Robo::config();
171
        }
172
173 3
        $loader = new JsonConfigLoader();
174 3
        $processor = new ConfigProcessor();
175 3
        $processor->add($config->export());
176
177 3
        foreach ($paths as $path) {
178 3
            $processor->extend($loader->load($path));
179
        }
180
181 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

181
        /** @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...
182 3
    }
183
}
184