PhpQualityTools::copyFile()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DanielWerner\PhpQualityTools;
4
5
use \stdClass;
6
use \Exception;
7
8
class PhpQualityTools
9
{
10
    /**
11
     * @var string $srcDirectory
12
     */
13
    protected $srcDirectory;
14
15
    /**
16
     * PhpQualityTools constructor.
17
     * @param string $srcDirectory
18
     */
19
    public function __construct(string $srcDirectory)
20
    {
21
        echo sprintf('Using source directory: "%s"', $srcDirectory) . PHP_EOL;
22
        $this->srcDirectory = $srcDirectory;
23
    }
24
25
    /**
26
     * @param $destination
27
     * @throws Exception
28
     */
29
    public function install($destination)
30
    {
31
        $this->copyStubs($destination);
32
        $this->setUpComposerJson($destination);
33
34
        echo 'Done.' . PHP_EOL;
35
    }
36
37
    /**
38
     * @param string $destination
39
     * @throws Exception
40
     */
41
    protected function copyStubs(string $destination)
42
    {
43
        echo 'Copying configuration files.' . PHP_EOL;
44
        $this->createDestinationIfNotExist($destination);
45
        $this->copyFile('phpmd.xml', $destination);
46
        $this->copyFile('phpcs.xml', $destination);
47
    }
48
49
    /**
50
     * @param string $destination
51
     * @throws Exception
52
     */
53
    protected function setUpComposerJson(string $destination)
54
    {
55
        echo 'Setting up composer.json scripts.' . PHP_EOL;
56
57
        $composerJson = $destination . '/composer.json';
58
        if (!file_exists($composerJson)) {
59
            throw new Exception('File composer.json is missed! Please ensure that you are in root folder.');
60
        }
61
        $composerSettings = $this->readComposerJson($composerJson);
62
        
63
        if (is_null($composerSettings)) {
64
            throw new Exception('File composer.json is corrupted!');
65
        }
66
        
67
        if (empty($composerSettings->scripts)) {
68
            $composerSettings->scripts = new stdClass();
69
        }
70
71
        $composerSettings->scripts = (object) array_merge(
72
            (array) $composerSettings->scripts,
73
            (array) $this->getComposerScripts()
74
        );
75
76
        if (!$this->writeComposerJson($composerJson, $composerSettings)) {
77
            throw new Exception('Cannot write new composer.json!');
78
        }
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    protected function getComposerScripts(): array
85
    {
86
        return [
87
            "inspect" => [
88
                sprintf("phpcs %s", $this->srcDirectory),
89
                sprintf("phpstan analyze %s", $this->srcDirectory)
90
            ],
91
            "inspect-fix" => [
92
                sprintf("php-cs-fixer fix %s", $this->srcDirectory),
93
                sprintf("phpcbf %s", $this->srcDirectory)
94
            ],
95
            "insights" => sprintf("phpmd %s text phpmd.xml", $this->srcDirectory)
96
        ];
97
    }
98
99
    /**
100
     * @param string $composerJson
101
     * @return stdClass
102
     * @throws Exception
103
     */
104
    protected function readComposerJson(string $composerJson): \stdClass
105
    {
106
        $content = file_get_contents($composerJson);
107
        if (!$content) {
108
            throw new Exception('File composer.json is empty!');
109
        }
110
        return json_decode($content);
111
    }
112
113
    /**
114
     * @param string $composerJson
115
     * @param stdClass $composerSettings
116
     * @return bool|int
117
     */
118
    protected function writeComposerJson(string $composerJson, stdClass $composerSettings)
119
    {
120
        return file_put_contents(
121
            $composerJson,
122
            json_encode(
123
                $composerSettings,
124
                JSON_PRETTY_PRINT |
125
                JSON_UNESCAPED_LINE_TERMINATORS |
126
                JSON_UNESCAPED_SLASHES |
127
                JSON_UNESCAPED_UNICODE
128
            )
129
        );
130
    }
131
132
    private function createDestinationIfNotExist($destination)
133
    {
134
        if (is_null($destination)) {
135
            throw new Exception('Failed to create required folders!');
136
        }
137
        if (!file_exists($destination)) {
138
            if (!mkdir($destination, 0777, true)) {
139
                throw new Exception('Failed to create required folders! Please check your write permission.');
140
            }
141
        }
142
    }
143
144
    private function copyFile($filename, $destination)
145
    {
146
        if (!file_exists(__DIR__ . "/../$filename") || !copy(__DIR__ . "/../$filename", $destination . "/$filename")) {
147
            throw new Exception(sprintf("File %s cannot be created! Please check your write permission.", $filename));
148
        }
149
    }
150
}
151