Failed Conditions
Push — master ( 07197c...02c092 )
by Yo
02:21
created

TemplateVarFactory::setComposerVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Yoanm\InitPhpRepository\Factory;
3
4
use Symfony\Component\DependencyInjection\ContainerBuilder;
5
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
6
7
/**
8
 * Class TemplateVarFactory
9
 */
10
class TemplateVarFactory
11
{
12
    /**
13
     * @return array
14
     *
15
     * @throws \Exception
16
     */
17
    public function create()
18
    {
19
        $bag = new ParameterBag();
20
21
        // - Git
22
        $this->setGitVariables($bag);
23
        // - Global
24
        $this->setGlobalVariables($bag);
25
        // - Composer
26
        $this->setComposerVariables($bag);
27
        // - Autoload
28
        $this->setAutoloadVariables($bag);
29
30
        $bag->resolve();
31
32
        return $bag->all();
33
    }
34
35
    /**
36
     * @param ParameterBag $bag
37
     *
38
     * @return array
39
     *
40
     * @throws \Exception
41
     */
42
    protected function setGitVariables(ParameterBag $bag)
43
    {
44
        $gitUsername = trim(shell_exec('git config --global user.name'));
45
        if ('' === $gitUsername) {
46
            throw new \Exception("Git username cannot be empty ! Use git config user.name 'NAME' to define it");
47
        }
48
49
        preg_match(
50
            '#github\.com(?:(?:(.*)\/)|(?::(.*)\.git))$#m',
51
            shell_exec('git remote -v show -n origin'),
52
            $matches
53
        );
54
        $gitId = trim($matches[1]);
55
        $gitId = '' === $gitId
56
            ? trim($matches[2])
57
            : $gitId
58
        ;
59
        if ('' === $gitId) {
60
            throw new \Exception("Unabled to define github id !");
61
        }
62
63
        $bag->set('git.username', $gitUsername);
64
        $bag->set('git.id', $gitId);
65
        $bag->set('git.url', 'github.com/%git.id%');
66
67
        list($vendor, $id) = explode('/', $gitId);
68
        $bag->set('github.vendor', $vendor);
69
        $bag->set('github.id', $id);
70
        $bag->set('github.url', 'https://%git.url%');
71
    }
72
73
    /**
74
     * @param ParameterBag $bag
75
     */
76
    protected function setGlobalVariables(ParameterBag $bag)
77
    {
78
        $bag->set('id', str_replace('_', '-', ContainerBuilder::underscore($bag->get('github.id'))));
79
        $bag->set('name', ucwords(str_replace('_', ' ', ContainerBuilder::underscore($bag->get('github.id')))));
80
    }
81
82
    /**
83
     * @param ParameterBag $bag
84
     */
85
    protected function setComposerVariables(ParameterBag $bag)
86
    {
87
        $bag->set('composer.package.name', str_replace('_', '-', ContainerBuilder::underscore($bag->get('git.id'))));
88
    }
89
90
    /**
91
     * @param ParameterBag $bag
92
     */
93
    protected function setAutoloadVariables(ParameterBag $bag)
94
    {
95
        // Namespaces
96
        $bag->set(
97
            'autoload.namespace.base',
98
            sprintf(
99
                '%s\\%s',
100
                ContainerBuilder::camelize($bag->get('github.vendor')),
101
                ContainerBuilder::camelize($bag->get('github.id'))
102
            )
103
        );
104
        $bag->set('autoload.namespace.tests.technical.unit', 'Technical\Unit\%autoload.namespace.base%');
105
        $bag->set('autoload.namespace.tests.technical.integration', 'Technical\Integration\%autoload.namespace.base%');
106
        $bag->set('autoload.namespace.tests.functional.base', 'Functional\%autoload.namespace.base%');
107
        $bag->set(
108
            'autoload.namespace.tests.functional.behat_context',
109
            '%autoload.namespace.tests.functional.base%\BehatContext'
110
        );
111
        // Folders
112
        $bag->set('autoload.folders.source', 'src');
113
        $bag->set(
114
            'autoload.folders.source_psr0',
115
            sprintf(
116
                '%s/%s/%s',
117
                '%autoload.folders.source%',
118
                ContainerBuilder::camelize($bag->get('github.vendor')),
119
                ContainerBuilder::camelize($bag->get('github.id'))
120
            )
121
        );
122
123
        $bag->set('autoload.folders.test.phpunit', 'tests');
124
        $bag->set('autoload.folders.test.behat', 'features');
125
126
        $bag->set('autoload.folders.test.technical.base', '%autoload.folders.test.phpunit%/Technical');
127
128
        $bag->set('autoload.folders.test.technical.unit', '%autoload.folders.test.technical.base%/Unit');
129
        $bag->set('autoload.folders.test.technical.integration', '%autoload.folders.test.technical.base%/Integration');
130
131
        $bag->set('autoload.folders.test.functional.phpunit', '%autoload.folders.test.phpunit%/Functional');
132
        $bag->set('autoload.folders.test.functional.behat_context', '%autoload.folders.test.behat%/bootstrap');
133
    }
134
}
135