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

VarFactory::setGitVariables()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 26
ccs 0
cts 23
cp 0
rs 8.5806
c 1
b 0
f 0
cc 4
eloc 18
nc 5
nop 1
crap 20
1
<?php
2
namespace Yoanm\DefaultPhpRepository\Factory;
3
4
use Symfony\Component\DependencyInjection\ContainerBuilder;
5
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
6
use Yoanm\DefaultPhpRepository\Command\Mode;
7
use Yoanm\DefaultPhpRepository\Command\RepositoryType;
8
9
/**
10
 * Class VarFactory
11
 */
12
class VarFactory
13
{
14
    /**
15
     * @param bool $isProject
16
     * @return array
17
     * @throws \Exception
18
     */
19
    public function create($isProject = true)
20
    {
21
        $bag = new ParameterBag();
22
23
        // - Git variables
24
        $this->setGitVariables($bag);
25
26
        $bag->set('id', str_replace('_', '-', ContainerBuilder::underscore($bag->get('github.id'))));
27
        $bag->set('name', ucwords(str_replace('_', ' ', ContainerBuilder::underscore($bag->get('github.id')))));
28
29
        // - Composer variables
30
        $this->setComposerVariables($bag, $isProject);
31
        // - Autoloading
32
        $this->setAutoloadVariables($bag);
33
34
        $bag->resolve();
35
36
        return $bag->all();
37
    }
38
39
    /**
40
     * @param ParameterBag $bag
41
     *
42
     * @return array
43
     *
44
     * @throws \Exception
45
     */
46
    protected function setGitVariables(ParameterBag $bag)
47
    {
48
        $gitUsername = trim(shell_exec('git config --global user.name'));
49
        if ('' === $gitUsername) {
50
            throw new \Exception("Git username cannot be empty ! Use git config user.name 'NAME' to define it");
51
        }
52
53
        preg_match('#github\.com(?:(?:(.*)\/)|(?::(.*)\.git))$#m', shell_exec('git remote -v show -n origin'), $matches);
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
     * @param bool         $isProject
76
     */
77
    protected function setComposerVariables(ParameterBag $bag, $isProject)
78
    {
79
        $bag->set('composer.package.name', str_replace('_', '-', ContainerBuilder::underscore($bag->get('git.id'))));
80
        $bag->set('composer.config.type', true === $isProject ? RepositoryType::PROJECT : RepositoryType::LIBRARY);
81
    }
82
83
    /**
84
     * @param ParameterBag $bag
85
     */
86
    protected function setAutoloadVariables(ParameterBag $bag)
87
    {
88
        $bag->set(
89
            'autoload.namespace',
90
            sprintf(
91
                '%s\\%s',
92
                ContainerBuilder::camelize($bag->get('github.vendor')),
93
                ContainerBuilder::camelize($bag->get('github.id'))
94
            )
95
        );
96
    }
97
98
}
99