Completed
Push — master ( 8d6189...196a37 )
by Pol
04:33 queued 31s
created

Taskman::createContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 5
dl 0
loc 11
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
        // Load the configuration.
50 3
        Robo::loadConfiguration(
51 3
            \PhpTaskman\Core\Config\Config::findFilesToIncludeInConfiguration($workingDir),
52 3
            $config
53
        );
54
55 3
        return $config;
56
    }
57
58
    /**
59
     * Create and configure container.
60
     *
61
     * @param InputInterface $input
62
     * @param OutputInterface $output
63
     * @param Application $application
64
     * @param Config $config
65
     * @param ClassLoader $classLoader
66
     *
67
     * @return Container|\League\Container\ContainerInterface
68
     */
69 2
    public static function createContainer(
70
        InputInterface $input,
71
        OutputInterface $output,
72
        Application $application,
73
        Config $config,
74
        ClassLoader $classLoader
75
    ) {
76 2
        $container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader);
77 2
        $container->get('commandFactory')->setIncludeAllPublicMethods(false);
78
79 2
        return $container;
80
    }
81
82
    /**
83
     * @param null|string $appName
84
     * @param null|string $appVersion
85
     * @param null|string $workingDir
86
     *
87
     * @return Application
88
     */
89 2
    public static function createDefaultApplication($appName = null, $appVersion = null, $workingDir = null)
90
    {
91 2
        $appName = $appName ?? self::APPLICATION_NAME;
92 2
        $appVersion = $appVersion ?? self::VERSION;
93
94 2
        $app = Robo::createDefaultApplication($appName, $appVersion);
95
96 2
        if (null === $workingDir || false === $workingDir = \realpath($workingDir)) {
97 1
            $workingDir = \getcwd();
98
        }
99
100
        $app
101 2
            ->getDefinition()
102 2
            ->addOption(
103 2
                new InputOption(
104 2
                    '--working-dir',
105 2
                    null,
106 2
                    InputOption::VALUE_REQUIRED,
107 2
                    'Working directory, defaults to current working directory.',
108 2
                    $workingDir
109
                )
110
            );
111
112 2
        $app->setAutoExit(false);
113
114 2
        return $app;
115
    }
116
117
    /**
118
     * @param ContainerInterface $container
119
     *
120
     * @return \Robo\Runner
121
     */
122 1
    public static function createDefaultRunner(ContainerInterface $container)
123
    {
124 1
        return (new \Robo\Runner())
125 1
            ->setRelativePluginNamespace('Robo\Plugin')
126 1
            ->setContainer($container);
127
    }
128
129
    /**
130
     * @param string[] $paths
131
     *   Array of JSON filepaths.
132
     *
133
     * @return Config
134
     *   A config object.
135
     */
136 3
    public static function createJsonConfiguration(array $paths)
137
    {
138 3
        $config = new Config();
139 3
        self::loadJsonConfiguration($paths, $config);
140
141 3
        return $config;
142
    }
143
144
    /**
145
     * @param string[] $paths
146
     *   Array of JSON filepaths.
147
     * @param null|Config $config
148
     *   A config object.
149
     */
150 3
    public static function loadJsonConfiguration(array $paths, ?Config $config)
151
    {
152 3
        if (null === $config) {
153
            $config = Robo::config();
154
        }
155
156 3
        $loader = new JsonConfigLoader();
157 3
        $processor = new ConfigProcessor();
158 3
        $processor->add($config->export());
159
160 3
        foreach ($paths as $path) {
161 3
            $processor->extend($loader->load($path));
162
        }
163
164 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

164
        /** @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...
165 3
    }
166
}
167