Completed
Push — master ( 9dbddb...162972 )
by Sebastian
02:51
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 configure(Event $event) : void
36
    {
37 2
        $config = self::getCaptainHookConfig($event);
38
39 2
        if (file_exists($config)) {
40 1
            $event->getIO()->write(('  <info>Skipping configuration: config file exists</info>'));
41 1
            return;
42
        }
43
44 1
        $app           = self::createApplication($event, $config);
45 1
        $configuration = new Configuration();
46 1
        $configuration->setIO($app->getIO());
47 1
        $input         = new ArrayInput(
48 1
            ['command' => 'configure', '--configuration' => $config, '-f' => '-f', '-e' => '-e']
49
        );
50 1
        $app->add($configuration);
51 1
        $app->run($input);
52 1
    }
53
54
    /**
55
     * Installs the hooks to your local repository
56
     *
57
     * @param  \Composer\Script\Event $event
58
     * @return void
59
     * @throws \Exception
60
     */
61 1
    public static function install(Event $event) : void
62
    {
63 1
        $config  = self::getCaptainHookConfig($event);
64 1
        $app     = self::createApplication($event, $config);
65 1
        $install = new Install();
66 1
        $install->setIO($app->getIO());
67 1
        $input   = new ArrayInput(['command' => 'install', '--configuration' => $config, '-f' => '-f']);
68 1
        $app->add($install);
69 1
        $app->run($input);
70 1
    }
71
72
    /**
73
     * Return the path to the captainhook config file
74
     *
75
     * @param  \Composer\Script\Event $event
76
     * @return string
77
     */
78 3
    private static function getCaptainHookConfig(Event $event) : string
79
    {
80 3
        $config = $event->getComposer()->getConfig();
81 3
        $extra  = $config->get('extra');
82 3
        if ($extra === null || ! isset($extra['captainhookconfig'])) {
83 1
            return getcwd() . DIRECTORY_SEPARATOR . CH::CONFIG;
84
        }
85 2
        return $extra['captainhookconfig'];
86
    }
87
88
    /**
89
     * Create a CaptainHook Composer application
90
     *
91
     * @param  \Composer\Script\Event $event
92
     * @param  string                 $config
93
     * @return \CaptainHook\App\Composer\Application
94
     */
95 2
    private static function createApplication(Event $event, string $config) : Application
96
    {
97 2
        $app = new Application();
98 2
        $app->setAutoExit(false);
99 2
        $app->setConfigFile($config);
100 2
        $app->setProxyIO($event->getIO());
101 2
        return $app;
102
    }
103
}
104