Test Failed
Pull Request — master (#9)
by Yo
03:11 queued 01:13
created

VarFactory::setGitVariables()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 22
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
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
8
/**
9
 * Class VarFactory
10
 */
11
class VarFactory
12
{
13
    /**
14
     * @return []
0 ignored issues
show
Documentation introduced by
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
15
     *
16
     * @throws \Exception
17
     */
18
    public function create($repositoryType)
19
    {
20
        $bag = new ParameterBag();
21
22
        // - Git variables
23
        $this->setGitVariables($bag);
24
25
        $id = str_replace(
26
            '_',
27
            '-',
28
            ContainerBuilder::underscore($bag->get('github.id'))
29
        );
30
        $bag->set('id', $id);
31
        $bag->set('name', ucwords(str_replace('-', ' ', $id)));
32
33
        // - Composer variables
34
        $this->setComposerVariables($bag, $repositoryType);
35
        // - Autoloading
36
        $this->setAutoloadVariables($bag);
37
38
        $bag->resolve();
39
40
        return $bag->all();
41
    }
42
43
    /**
44
     * @param ParameterBag $bag
45
     *
46
     * @return array
47
     *
48
     * @throws \Exception
49
     */
50
    protected function setGitVariables(ParameterBag $bag)
51
    {
52
        $gitUsername = trim(shell_exec('git config --global user.name'));
53
        if ('' === $gitUsername) {
54
            throw new \Exception("Git username cannot be empty ! Use git config user.name 'NAME' to define it");
55
        }
56
57
        $remoteListOutput = shell_exec('git remote -v show -n origin');
58
        // Try with ssh mode else fallback to http mode
59
        if (0 === preg_match('#github\.com:(.*)(?:\.git)$#m', $remoteListOutput, $matches)) {
60
            preg_match('#github\.com\/([^\/]+\/[^\/]+)#m', $remoteListOutput, $matches);
61
        }
62
        $gitId = trim($matches[1]);
63
        if ('' === $gitId) {
64
            throw new \Exception("Unabled to define github id !");
65
        }
66
67
        $bag->set('git.username', $gitUsername);
68
        $bag->set('git.id', $gitId);
69
        $bag->set('git.url', 'github.com/%git.id%');
70
71
        list($vendor, $id) = explode('/', $gitId);
72
        $bag->set('github.vendor', $vendor);
73
        $bag->set('github.id', $id);
74
        $bag->set('github.url', 'https://%git.url%');
75
    }
76
77
    /**
78
     * @param ParameterBag $bag
79
     * @param $repositoryType
80
     */
81
    protected function setComposerVariables(ParameterBag $bag, $repositoryType)
82
    {
83
        $bag->set(
84
            'composer.package.name',
85
            str_replace(
86
                '_',
87
                '-',
88
                ContainerBuilder::underscore($bag->get('git.id'))
89
            )
90
        );
91
        $bag->set('composer.config.type', $repositoryType);
92
    }
93
94
    /**
95
     * @param ParameterBag $bag
96
     */
97
    protected function setAutoloadVariables(ParameterBag $bag)
98
    {
99
        $bag->set(
100
            'autoload.namespace',
101
            sprintf(
102
                '%s\\%s',
103
                ContainerBuilder::camelize($bag->get('github.vendor')),
104
                ContainerBuilder::camelize($bag->get('github.id'))
105
            )
106
        );
107
    }
108
109
}
110