ScriptHandler::replaceViews()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 39
rs 8.8571
cc 3
eloc 24
nc 3
nop 1
1
<?php
2
/*
3
* This file is part of the ReCaptcha Validator Component.
4
*
5
* (c) Ilya Pokamestov
6
*
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
9
*/
10
11
namespace DS\Component\ReCaptchaValidator\Composer;
12
13
use Symfony\Component\Filesystem\Filesystem;
14
use Composer\Script\CommandEvent;
15
16
/**
17
 * Script handler for post install/update commands.
18
 *
19
 * @author Ilya Pokamestov <[email protected]>
20
 */
21
class ScriptHandler
22
{
23
    /** @var  Filesystem */
24
    protected static $fileSystem;
25
26
    public static function replaceViews(CommandEvent $event)
27
    {
28
        $event->getIO()->write('Copy ReCaptcha views to app/Resource/DS/ReCaptcha/views.');
29
30
        $directory = getcwd();
31
        self::$fileSystem = new Filesystem();
32
33
        $directory .= '/app/Resources';
34
        self::makeDirIfNOtExist($directory);
35
36
        $directory .= '/DS';
37
        self::makeDirIfNOtExist($directory);
38
39
        $directory .= '/ReCaptcha';
40
        self::makeDirIfNOtExist($directory);
41
42
        $directory .= '/views';
43
        self::makeDirIfNOtExist($directory);
44
45
        if (self::$fileSystem->exists($directory.'/form_div_layout.html.twig')) {
46
            if (!$event->getIO()->askConfirmation(
47
                sprintf(
48
                    'form_div_layout.html.twig already exist in %s. Would you like to rewrite this file? [y/N]  ',
49
                    $directory
50
                ),
51
                false
52
            )) {
53
                return;
54
            }
55
        }
56
57
        self::$fileSystem->copy(
58
            __DIR__.'/../Resources/views/form_div_layout.html.twig',
59
            $directory.'/form_div_layout.html.twig',
60
            true
61
        );
62
63
        $event->getIO()->write('Files creation operation completed successfully.');
64
    }
65
66
    /**
67
     * @param string $dir
68
     * @throws \RuntimeException
69
     */
70
    protected static function makeDirIfNOtExist($dir)
71
    {
72
        if (!self::isDirectoryExist($dir)) {
73
            self::$fileSystem->mkdir($dir, 775);
74
        }
75
76
        if (!self::isDirectoryExist($dir)) {
77
            throw new \RuntimeException(sprintf('Can\'t create directory: "%s"', $dir));
78
        }
79
    }
80
81
    /**
82
     * @param string $dir
83
     * @return bool
84
     */
85
    protected static function isDirectoryExist($dir)
86
    {
87
        return self::$fileSystem->exists($dir);
88
    }
89
}
90