Test Failed
Pull Request — master (#9)
by Yo
01:55
created

TemplateListFactory::create()   C

Complexity

Conditions 7
Paths 36

Size

Total Lines 71
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 71
ccs 0
cts 62
cp 0
rs 6.7968
cc 7
eloc 49
nc 36
nop 1
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
10
/**
11
 * Class TemplateListFactory
12
 */
13
class TemplateListFactory
14
{
15
    /**
16
     * @param string $repositoryType
17
     *
18
     * @return Template[]
19
     */
20
    public function create($repositoryType)
21
    {
22
        $fileTemplateList = [
23
            'git.readme' => 'README.md.twig',
24
            'git.license' => 'LICENSE.twig',
25
            'git.contributing' => 'CONTRIBUTING.md.twig',
26
            'git.gitignore' => '.gitignore.twig',
27
            'composer.config' => 'composer.json.twig',
28
            'phpcs.config' => 'phpcs.xml.dist.twig',
29
            'phpunit.config' => 'phpunit.xml.dist.twig',
30
            'behat.config' => 'behat.yml.twig',
31
            'ci.scrutinizer' => '.scrutinizer.yml.twig',
32
        ];
33
34
        if (RepositoryType::LIBRARY === $repositoryType) {
35
            $fileTemplateList['ci.travis'] = '.travis.yml.twig';
36
        }
37
38
        $list = [];
39
        foreach ($fileTemplateList as $templateId => $templateName) {
40
            $list[$templateId] = $this->createTemplate($templateId, $templateName);
41
        }
42
43
        $folderTemplateList = [
44
            'phpunit.folders' => 'tests',
45
            'behat.folders' => 'features',
46
        ];
47
48
        $basePath = TemplateHelper::getTemplateBasePath();
49
        foreach ($folderTemplateList as $templateId => $templateFolder) {
50
            $folderTemplate = $list[$templateId] = new FolderTemplate($templateId, $templateFolder);
51
            // Iterate over files
52
            $count = 0;
53
            $path = realpath(sprintf('%s/base/%s', $basePath, $templateFolder));
54
            $toRemove = sprintf('%s/base/', $basePath);
55
            foreach ((new Finder())->files()->in($path) as $file) {
56
                $folderTemplate->addFile(
57
                    $this->createTemplate(
58
                        sprintf('%s.%s', $templateId, $count),
59
                        str_replace($toRemove, '', $file->getPathname())
60
                    )
61
                );
62
                $count++;
63
            }
64
        }
65
66
        // Reorder final list
67
        $orderedList =  [
68
            'git.readme',
69
            'git.license',
70
            'git.contributing',
71
            'git.gitignore',
72
            'composer.config',
73
            'phpcs.config',
74
            'phpunit.config',
75
            'phpunit.folders',
76
            'behat.config',
77
            'behat.folders',
78
            'ci.scrutinizer',
79
            'ci.travis',
80
        ];
81
82
        $finalList = [];
83
        foreach ($orderedList as $key) {
84
            if (isset($list[$key])) {
85
                $finalList[$key] = $list[$key];
86
            }
87
        }
88
89
        return $finalList;
90
    }
91
92
    /**
93
     * @param string $id
94
     * @param string $templateName
95
     *
96
     * @return Template
97
     */
98
    protected function createTemplate($id, $templateName)
99
    {
100
        return new Template($id, $templateName, $this->getOutputFilePath($templateName));
101
    }
102
103
104
    /**
105
     * @param string $templateName
106
     *
107
     * @return string
108
     */
109
    protected function getOutputFilePath($templateName)
110
    {
111
        return str_replace('.twig', '', $templateName);
112
    }
113
}
114