Completed
Push — master ( 3a62ee...1057a2 )
by Sebastian
03:24
created

Cmd::getCaptainHookConfig()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.9666
cc 3
nc 2
nop 1
crap 3
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Composer;
11
12
use CaptainHook\App\CH;
13
use Composer\Script\Event;
14
use CaptainHook\App\Console\Command\Configuration;
15
use CaptainHook\App\Console\Command\Install;
16
use Symfony\Component\Console\Input\ArrayInput;
17
18
/**
19
 * Class Cmd
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/captainhookphp/captainhook
24
 * @since   Class available since Release 0.9.0
25
 */
26
abstract class Cmd
27
{
28
    /**
29
     * Gets called by composer after a successful package installation
30
     *
31
     * @param  \Composer\Script\Event $event
32
     * @return void
33
     * @throws \Exception
34
     */
35 2
    public static function setup(Event $event) : void
36
    {
37 2
        $extra  = self::getExtraConfig($event);
38 2
        $config = self::extract(CH::COMPOSER_CONFIG, $extra);
39 2
        $repo   = self::extract(CH::COMPOSER_GIT_DIR, $extra);
40 2
        $app    = self::createApplication($event, $config);
41
42 2
        self::configure($app, $config);
43 2
        self::install($app, $config, $repo);
44 2
    }
45
46
    /**
47
     * @param  \CaptainHook\App\Composer\Application $app
48
     * @param  string                                $config
49
     * @return void
50
     * @throws \Exception
51
     */
52 2
    private static function configure(Application $app, string $config)
53
    {
54 2
        if (file_exists($config)) {
55
            $app->getIO()->write(('  <info>Skipping configuration: config file exists</info>'));
56
            return;
57
        }
58 2
        $configuration = new Configuration();
59 2
        $configuration->setIO($app->getIO());
60 2
        $input         = new ArrayInput(
61 2
            ['command' => 'configure', '--configuration' => $config, '-f' => '-f', '-e' => '-e']
62
        );
63 2
        $app->add($configuration);
64 2
        $app->run($input);
65 2
    }
66
67
    /**
68
     * Installs the hooks to your local repository
69
     *
70
     * @param  \CaptainHook\App\Composer\Application $app
71
     * @param  string                                $config
72
     * @param  string                                $repo
73
     * @return void
74
     * @throws \Exception
75
     */
76 2
    private static function install(Application $app, string $config, string $repo) : void
77
    {
78 2
        $options = ['command' => 'install', '-f' => '-f'];
79 2
        self::appendNotEmpty($config, '--configuration', $options);
80 2
        self::appendNotEmpty($repo, '--git-directory', $options);
81
82 2
        $install = new Install();
83 2
        $install->setIO($app->getIO());
84 2
        $input   = new ArrayInput($options);
85 2
        $app->add($install);
86 2
        $app->run($input);
87 2
    }
88
89
    /**
90
     * Return Composer extra config, make sure it is an array
91
     *
92
     * @param \Composer\Script\Event $event
93
     * @return array
94
     */
95 2
    private static function getExtraConfig(Event $event) : array
96
    {
97 2
        $extra = $event->getComposer()->getPackage()->getExtra();
98 2
        return is_array($extra) ? $extra : [];
99
    }
100
101
    /**
102
     * Create a CaptainHook Composer application
103
     *
104
     * @param  \Composer\Script\Event $event
105
     * @param  string                 $config
106
     * @return \CaptainHook\App\Composer\Application
107
     */
108 2
    private static function createApplication(Event $event, string $config) : Application
109
    {
110 2
        $app = new Application();
111 2
        $app->setAutoExit(false);
112 2
        $app->setConfigFile($config);
113 2
        $app->setProxyIO($event->getIO());
114 2
        return $app;
115
    }
116
117
    /**
118
     * Extract a value from an array if not set it returns the given default
119
     *
120
     * @param  string $key
121
     * @param  array  $array
122
     * @param  string $default
123
     * @return string
124
     */
125 2
    private static function extract(string $key, array $array, string $default = '') : string
126
    {
127 2
        return isset($array[$key]) ? $array[$key] : $default;
128
    }
129
130
    /**
131
     * Adds a value to an array with a given key if the value is not empty
132
     *
133
     * @param  string $value
134
     * @param  string $key
135
     * @param  array  $array
136
     * @return void
137
     */
138 2
    private static function appendNotEmpty(string $value, string $key, array &$array) : void
139
    {
140 2
        if (!empty($value)) {
141
            $array[$key] = $value;
142
        }
143 2
    }
144
}
145