Passed
Push — master ( c1b6e2...f87aee )
by Elminson
40s
created

PHPProjectGen::generatePHPUnitTestCases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: elminsondeoleobaez
5
 * Date: 10/3/18
6
 * Time: 6:00 PM
7
 */
8
9
namespace Elminson\PHPProjectGen;
10
11
/** @scrutinizer ignore-type */
12
13
use Alchemy\Zippy\Zippy;
0 ignored issues
show
Bug introduced by
The type Alchemy\Zippy\Zippy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/** @scrutinizer ignore-type */
16
17
use PclZip;
0 ignored issues
show
Bug introduced by
The type PclZip was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class PHPProjectGen
20
{
21
    private $composer_config;
22
23
    /**
24
     * PHPProjectGen constructor.
25
     * @param $composer_config
26
     */
27
    public function __construct()
28
    {
29
        $this->getConfig();
30
    }
31
32
    /**
33
     *
34
     */
35
    public function GenerateProject()
36
    {
37
        $this->generateComposer();
38
        $this->generateClass();
39
        $this->generateTestCases();
40
        $this->generatePHPUnitTestCases();
41
        $this->generateZipFile();
42
        $this->cleanData();
43
    }
44
45
    private function getConfig()
46
    {
47
        $string = file_get_contents("config.json");
48
        $this->composer_config = json_decode($string, true);
49
    }
50
51
    /**
52
     *
53
     */
54
    private function generateComposer()
55
    {
56
57
        $composer_data = [];
58
        $composer_data['name'] = strtolower($this->composer_config['name'] . "/" . $this->composer_config['projectname']);
59
        $composer_data['description'] = $this->composer_config['description'];
60
        $composer_data['type'] = $this->composer_config['type'];
61
        if ($this->composer_config['phpunit']) {
62
            $composer_data['require']['phpunit/phpunit'] = '^7.4@dev';
63
            $composer_data['require-dev']['phpunit/phpunit'] = '^7.4@dev';
64
        }
65
        $composer_data['license'] = $this->composer_config['license'];
66
        $composer_data['authors'][0]['name'] = $this->composer_config['developer'];
67
        $composer_data['authors'][0]['email'] = $this->composer_config['email'];
68
        $src = $this->composer_config['name'] . "\\\\" . $this->composer_config['projectname'] . "\\\\";
69
        $tests = $this->composer_config['name'] . "\\\\" . $this->composer_config['projectname'] . "\\\\Test\\\\";
70
        $composer_data['autoload']['psr-0'] = [
71
          $src => "src/",
72
          $tests => "tests/"
73
        ];
74
        $composer_data['autoload']['psr-4'] = [
75
          $src => "src/",
76
          $tests => "tests/"
77
        ];
78
        $composer_data['autoload-dev']['psr-4'] = [
79
          $src => "src/",
80
          $tests => "tests/"
81
        ];
82
        $composer_data['minimum-stability'] = $this->composer_config['minimum-stability'];
83
        $string = stripslashes(json_encode($composer_data, JSON_PRETTY_PRINT));
84
        $this->writeFile('composer', $string, "", "json");
85
    }
86
87
    private function generateClass()
88
    {
89
        $string = file_get_contents("src/ProjectTemplate.php.raw");
90
        $name = $this->composer_config['name'] . '\\' . $this->composer_config['projectname'];
91
        $string = str_replace('{!namespace!}', $name, $string);
92
        $string = str_replace('{!class!}', $this->composer_config['projectname'], $string);
93
        $string = str_replace('{!email!}', $this->composer_config['email'], $string);
94
        $string = str_replace('{!date!}', date('m/d/Y'), $string);
95
        $string = str_replace('{!time!}', date('h:i A'), $string);
96
        $this->writeFile($this->composer_config['projectname'], $string);
97
    }
98
99
    private function generateTestCases()
100
    {
101
        $string = file_get_contents("src/testProjectTemplate.php.raw");
102
        $name = $this->composer_config['name'] . '\\' . $this->composer_config['projectname'];
103
        $string = str_replace('{!namespace!}', $name, $string);
104
        $string = str_replace('{!projectname!}', $this->composer_config['projectname'], $string);
105
        $string = str_replace('{!loweclass!}', strtolower($this->composer_config['projectname']), $string);
106
        $this->writeFile($this->composer_config['projectname'], $string, 'test');
107
108
    }
109
110
    private function generatePHPUnitTestCases()
111
    {
112
        $string = file_get_contents("src/phpunit.xml.dist.raw");
113
        $string = str_replace('{!projectname!}', $this->composer_config['projectname'], $string);
114
        $this->writeFile('phpunit.xml', $string, '', 'dist');
115
116
    }
117
118
    private function writeFile($name, $data, $prefix = "", $ext = "php")
119
    {
120
        $file = fopen('src/temp/' . $prefix . $name . '.' . $ext, 'w');
121
        fwrite(/** @scrutinizer ignore-type */
122
          $file, $data);
123
        fclose(/** @scrutinizer ignore-type */
124
          $file);
125
    }
126
127
    private function generateZipFile()
128
    {
129
        $zipFile = new \PhpZip\ZipFile();
130
        $mainFile = "src/" . $this->composer_config['projectname'] . ".php";
131
        $zipFile
132
          ->addFile(__DIR__ . "/temp/" . $this->composer_config['projectname'] . ".php", $mainFile)
133
          ->addFile(__DIR__ . "/temp/composer.json", "composer.json")
134
          ->addFile(__DIR__ . "/temp/.gitignore", ".gitignore")
135
          ->addFromString("README.md", "#" . $this->composer_config['projectname']);
136
137
        if ($this->composer_config['phpunit']) {
138
            $testFile = "tests/test" . $this->composer_config['projectname'] . ".php";
139
            $zipFile
140
              ->addFile(__DIR__ . "/temp/phpunit.xml.dist", "test/phpunit.xml.dist")
141
              ->addFile(__DIR__ . "/temp/test" . $this->composer_config['projectname'] . ".php", $testFile);
142
        }
143
144
        $zipFile
145
          ->saveAsFile($this->composer_config['projectname'] . '.zip')
146
          ->close();
147
148
    }
149
150
    private function cleanData()
151
    {
152
        unlink(__DIR__ . "/temp/" . $this->composer_config['projectname'] . ".php");
153
        if ($this->composer_config['phpunit']) {
154
            unlink(__DIR__ . "/temp/test" . $this->composer_config['projectname'] . ".php");
155
            unlink(__DIR__ . "/temp/phpunit.xml.dist");
156
        }
157
        unlink(__DIR__ . "/temp/composer.json");
158
    }
159
160
}