Failed Conditions
Pull Request — release/0.2.0 (#12)
by Yo
02:09
created

TemplateListFactory::getTemplateList()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 0
cts 42
cp 0
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 32
nc 12
nop 1
crap 30
1
<?php
2
namespace Yoanm\InitPhpRepository\Factory;
3
4
use Symfony\Component\Finder\Finder;
5
use Yoanm\InitPhpRepository\Command\RepositoryType;
6
use Yoanm\InitPhpRepository\Helper\TemplateHelper;
7
use Yoanm\InitPhpRepository\Model\FolderTemplate;
8
use Yoanm\InitPhpRepository\Model\Template;
9
use Yoanm\InitPhpRepository\Resolver\NamespaceResolver;
10
11
/**
12
 * Class TemplateListFactory
13
 */
14
class TemplateListFactory
15
{
16
    /**
17
     * @param string $repositoryType
18
     * @param string $repositorySubType
19
     *
20
     * @return Template[]
21
     */
22
    public function create($repositoryType, $repositorySubType)
23
    {
24
        $templateList = $this->getTemplateList($repositoryType);
25
26
        (new NamespaceResolver())->resolve($templateList, $repositoryType, $repositorySubType);
27
28
        // Reorder final list
29
        $orderedList = [
30
            'git.readme',
31
            'git.license',
32
            'git.contributing',
33
            'git.gitignore',
34
            'composer.config',
35
            'phpcs.config',
36
            'phpunit.config',
37
            'phpunit.folders',
38
            'behat.config',
39
            'behat.folders',
40
            'ci.scrutinizer',
41
            'ci.travis',
42
        ];
43
44
        $finalList = [];
45
        foreach ($orderedList as $key) {
46
            if (isset($templateList[$key])) {
47
                $finalList[$key] = $templateList[$key];
48
            }
49
        }
50
51
        return $finalList;
52
    }
53
54
    /**
55
     * @param string $id
56
     * @param string $templateName
57
     *
58
     * @return Template
59
     */
60
    protected function createTemplate($id, $templateName)
61
    {
62
        return new Template($id, $templateName, $this->getOutputFilePath($templateName));
63
    }
64
65
66
    /**
67
     * @param string $templateName
68
     *
69
     * @return string
70
     */
71
    protected function getOutputFilePath($templateName)
72
    {
73
        return str_replace('.twig', '', $templateName);
74
    }
75
76
    /**
77
     * @param $repositoryType
78
     *
79
     * @return array
80
     */
81
    protected function getTemplateList($repositoryType)
82
    {
83
        $fileTemplateList = [
84
            'git.readme' => 'README.md.twig',
85
            'git.license' => 'LICENSE.twig',
86
            'git.contributing' => 'CONTRIBUTING.md.twig',
87
            'git.gitignore' => '.gitignore.twig',
88
            'composer.config' => 'composer.json.twig',
89
            'phpcs.config' => 'phpcs.xml.dist.twig',
90
            'phpunit.config' => 'phpunit.xml.dist.twig',
91
            'behat.config' => 'behat.yml.twig',
92
            'ci.scrutinizer' => '.scrutinizer.yml.twig',
93
        ];
94
95
        if (RepositoryType::LIBRARY === $repositoryType) {
96
            $fileTemplateList['ci.travis'] = '.travis.yml.twig';
97
        }
98
99
        $templateList = [];
100
        foreach ($fileTemplateList as $templateId => $templateName) {
101
            $templateList[$templateId] = $this->createTemplate($templateId, $templateName);
102
        }
103
104
        $folderTemplateList = [
105
            'phpunit.folders' => 'tests',
106
            'behat.folders' => 'features',
107
        ];
108
109
        $basePath = TemplateHelper::getTemplateBasePath();
110
        foreach ($folderTemplateList as $templateId => $templateFolder) {
111
            $folderTemplate = $templateList[$templateId] = new FolderTemplate($templateId, $templateFolder);
112
            // Iterate over files
113
            $count = 0;
114
            $path = realpath(sprintf('%s/base/%s', $basePath, $templateFolder));
115
            $toRemove = sprintf('%s/base/', $basePath);
116
            foreach ((new Finder())->files()->in($path) as $file) {
117
                $folderTemplate->addFile(
118
                    $this->createTemplate(
119
                        sprintf('%s.%s', $templateId, $count),
120
                        str_replace($toRemove, '', $file->getPathname())
121
                    )
122
                );
123
                $count++;
124
            }
125
        }
126
        return $templateList;
127
    }
128
}
129