Test Failed
Pull Request — master (#9)
by Yo
02:18
created

VarFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 87
ccs 0
cts 50
cp 0
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 19 1
B setGitVariables() 0 26 4
A setComposerVariables() 0 5 2
A setAutoloadVariables() 0 11 1
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\RepositoryType;
7
8
/**
9
 * Class VarFactory
10
 */
11
class VarFactory
12
{
13
    /**
14
     * @param bool $isProject
15
     * @return array
16
     * @throws \Exception
17
     */
18
    public function create($isProject = true)
19
    {
20
        $bag = new ParameterBag();
21
22
        // - Git variables
23
        $this->setGitVariables($bag);
24
25
        $bag->set('id', str_replace('_', '-', ContainerBuilder::underscore($bag->get('github.id'))));
26
        $bag->set('name', ucwords(str_replace('_', ' ', ContainerBuilder::underscore($bag->get('github.id')))));
27
28
        // - Composer variables
29
        $this->setComposerVariables($bag, $isProject);
30
        // - Autoloading
31
        $this->setAutoloadVariables($bag);
32
33
        $bag->resolve();
34
35
        return $bag->all();
36
    }
37
38
    /**
39
     * @param ParameterBag $bag
40
     *
41
     * @return array
42
     *
43
     * @throws \Exception
44
     */
45
    protected function setGitVariables(ParameterBag $bag)
46
    {
47
        $gitUsername = trim(shell_exec('git config --global user.name'));
48
        if ('' === $gitUsername) {
49
            throw new \Exception("Git username cannot be empty ! Use git config user.name 'NAME' to define it");
50
        }
51
52
        preg_match('#github\.com(?:(?:(.*)\/)|(?::(.*)\.git))$#m', shell_exec('git remote -v show -n origin'), $matches);
53
        $gitId = trim($matches[1]);
54
        $gitId = '' === $gitId
55
            ? trim($matches[2])
56
            : $gitId
57
        ;
58
        if ('' === $gitId) {
59
            throw new \Exception("Unabled to define github id !");
60
        }
61
62
        $bag->set('git.username', $gitUsername);
63
        $bag->set('git.id', $gitId);
64
        $bag->set('git.url', 'github.com/%git.id%');
65
66
        list($vendor, $id) = explode('/', $gitId);
67
        $bag->set('github.vendor', $vendor);
68
        $bag->set('github.id', $id);
69
        $bag->set('github.url', 'https://%git.url%');
70
    }
71
72
    /**
73
     * @param ParameterBag $bag
74
     * @param bool         $isProject
75
     */
76
    protected function setComposerVariables(ParameterBag $bag, $isProject)
77
    {
78
        $bag->set('composer.package.name', str_replace('_', '-', ContainerBuilder::underscore($bag->get('git.id'))));
79
        $bag->set('composer.config.type', true === $isProject ? RepositoryType::PROJECT : RepositoryType::LIBRARY);
80
    }
81
82
    /**
83
     * @param ParameterBag $bag
84
     */
85
    protected function setAutoloadVariables(ParameterBag $bag)
86
    {
87
        $bag->set(
88
            'autoload.namespace',
89
            sprintf(
90
                '%s\\%s',
91
                ContainerBuilder::camelize($bag->get('github.vendor')),
92
                ContainerBuilder::camelize($bag->get('github.id'))
93
            )
94
        );
95
    }
96
97
}
98