Failed Conditions
Push — master ( c3b9f6...b14904 )
by Yo
02:03
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\DefaultPhpRepository\Factory;
3
4
use Symfony\Component\Finder\Finder;
5
use Yoanm\DefaultPhpRepository\Command\RepositoryType;
6
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper;
7
use Yoanm\DefaultPhpRepository\Model\FolderTemplate;
8
use Yoanm\DefaultPhpRepository\Model\Template;
9
use Yoanm\DefaultPhpRepository\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
     * @return array
79
     */
80
    protected function getTemplateList($repositoryType)
81
    {
82
        $fileTemplateList = [
83
            'git.readme' => 'README.md.twig',
84
            'git.license' => 'LICENSE.twig',
85
            'git.contributing' => 'CONTRIBUTING.md.twig',
86
            'git.gitignore' => '.gitignore.twig',
87
            'composer.config' => 'composer.json.twig',
88
            'phpcs.config' => 'phpcs.xml.dist.twig',
89
            'phpunit.config' => 'phpunit.xml.dist.twig',
90
            'behat.config' => 'behat.yml.twig',
91
            'ci.scrutinizer' => '.scrutinizer.yml.twig',
92
        ];
93
94
        if (RepositoryType::LIBRARY === $repositoryType) {
95
            $fileTemplateList['ci.travis'] = '.travis.yml.twig';
96
        }
97
98
        $templateList = [];
99
        foreach ($fileTemplateList as $templateId => $templateName) {
100
            $templateList[$templateId] = $this->createTemplate($templateId, $templateName);
101
        }
102
103
        $folderTemplateList = [
104
            'phpunit.folders' => 'tests',
105
            'behat.folders' => 'features',
106
        ];
107
108
        $basePath = TemplateHelper::getTemplateBasePath();
109
        foreach ($folderTemplateList as $templateId => $templateFolder) {
110
            $folderTemplate = $templateList[$templateId] = new FolderTemplate($templateId, $templateFolder);
111
            // Iterate over files
112
            $count = 0;
113
            $path = realpath(sprintf('%s/base/%s', $basePath, $templateFolder));
114
            $toRemove = sprintf('%s/base/', $basePath);
115
            foreach ((new Finder())->files()->in($path) as $file) {
116
                $folderTemplate->addFile(
117
                    $this->createTemplate(
118
                        sprintf('%s.%s', $templateId, $count),
119
                        str_replace($toRemove, '', $file->getPathname())
120
                    )
121
                );
122
                $count++;
123
            }
124
        }
125
        return $templateList;
126
    }
127
}
128