Completed
Push — master ( 162972...b05c16 )
by Sebastian
04:19
created

Cmd::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
        $config = self::getCaptainHookConfig($event);
38 2
        $app    = self::createApplication($event, $config);
39
40 2
        self::configure($app, $config);
41 2
        self::install($app, $config);
42 2
    }
43
44
    /**
45
     * @param  \CaptainHook\App\Composer\Application $app
46
     * @param  string                                $config
47
     * @return void
48
     * @throws \Exception
49
     */
50 2
    private static function configure(Application $app, string $config)
51
    {
52 2
        if (file_exists($config)) {
53 1
            $app->getIO()->write(('  <info>Skipping configuration: config file exists</info>'));
54 1
            return;
55
        }
56 1
        $configuration = new Configuration();
57 1
        $configuration->setIO($app->getIO());
58 1
        $input         = new ArrayInput(
59 1
            ['command' => 'configure', '--configuration' => $config, '-f' => '-f', '-e' => '-e']
60
        );
61 1
        $app->add($configuration);
62 1
        $app->run($input);
63 1
    }
64
65
    /**
66
     * Installs the hooks to your local repository
67
     *
68
     * @param  \CaptainHook\App\Composer\Application $app
69
     * @param  string                                $config
70
     * @return void
71
     * @throws \Exception
72
     */
73 2
    private static function install(Application $app, string $config) : void
74
    {
75 2
        $install = new Install();
76 2
        $install->setIO($app->getIO());
77 2
        $input   = new ArrayInput(['command' => 'install', '--configuration' => $config, '-f' => '-f']);
78 2
        $app->add($install);
79 2
        $app->run($input);
80 2
    }
81
82
    /**
83
     * Return the path to the captainhook config file
84
     *
85
     * @param  \Composer\Script\Event $event
86
     * @return string
87
     */
88 2
    private static function getCaptainHookConfig(Event $event) : string
89
    {
90 2
        $config = $event->getComposer()->getConfig();
91 2
        $extra  = $config->get('extra');
92 2
        if ($extra === null || ! isset($extra['captainhookconfig'])) {
93 1
            return getcwd() . DIRECTORY_SEPARATOR . CH::CONFIG;
94
        }
95 1
        return $extra['captainhookconfig'];
96
    }
97
98
    /**
99
     * Create a CaptainHook Composer application
100
     *
101
     * @param  \Composer\Script\Event $event
102
     * @param  string                 $config
103
     * @return \CaptainHook\App\Composer\Application
104
     */
105 2
    private static function createApplication(Event $event, string $config) : Application
106
    {
107 2
        $app = new Application();
108 2
        $app->setAutoExit(false);
109 2
        $app->setConfigFile($config);
110 2
        $app->setProxyIO($event->getIO());
111 2
        return $app;
112
    }
113
}
114