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

TemplateListFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 115
ccs 0
cts 76
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 31 3
B getTemplateList() 0 47 5
A createTemplate() 0 4 1
A getOutputFilePath() 0 4 1
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 $repositoryType
56
     *
57
     * @return array
58
     */
59
    protected function getTemplateList($repositoryType)
60
    {
61
        $fileTemplateList = [
62
            'git.readme' => 'README.md.twig',
63
            'git.license' => 'LICENSE.twig',
64
            'git.contributing' => 'CONTRIBUTING.md.twig',
65
            'git.gitignore' => '.gitignore.twig',
66
            'composer.config' => 'composer.json.twig',
67
            'phpcs.config' => 'phpcs.xml.dist.twig',
68
            'phpunit.config' => 'phpunit.xml.dist.twig',
69
            'behat.config' => 'behat.yml.twig',
70
            'ci.scrutinizer' => '.scrutinizer.yml.twig',
71
        ];
72
73
        if (RepositoryType::LIBRARY === $repositoryType) {
74
            $fileTemplateList['ci.travis'] = '.travis.yml.twig';
75
        }
76
77
        $templateList = [];
78
        foreach ($fileTemplateList as $templateId => $templateName) {
79
            $templateList[$templateId] = $this->createTemplate($templateId, $templateName);
80
        }
81
82
        $folderTemplateList = [
83
            'phpunit.folders' => 'tests',
84
            'behat.folders' => 'features',
85
        ];
86
87
        $basePath = TemplateHelper::getTemplateBasePath();
88
        foreach ($folderTemplateList as $templateId => $templateFolder) {
89
            $folderTemplate = $templateList[$templateId] = new FolderTemplate($templateId, $templateFolder);
90
            // Iterate over files
91
            $count = 0;
92
            $path = realpath(sprintf('%s/base/%s', $basePath, $templateFolder));
93
            $toRemove = sprintf('%s/base/', $basePath);
94
            foreach ((new Finder())->files()->in($path) as $file) {
95
                $folderTemplate->addFile(
96
                    $this->createTemplate(
97
                        sprintf('%s.%s', $templateId, $count),
98
                        str_replace($toRemove, '', $file->getPathname())
99
                    )
100
                );
101
                $count++;
102
            }
103
        }
104
        return $templateList;
105
    }
106
107
    /**
108
     * @param string $id
109
     * @param string $templateName
110
     *
111
     * @return Template
112
     */
113
    protected function createTemplate($id, $templateName)
114
    {
115
        return new Template($id, $templateName, $this->getOutputFilePath($templateName));
116
    }
117
118
119
    /**
120
     * @param string $templateName
121
     *
122
     * @return string
123
     */
124
    protected function getOutputFilePath($templateName)
125
    {
126
        return str_replace('.twig', '', $templateName);
127
    }
128
}
129