Git::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
namespace Fabrica\Tools\Plugin;
4
5
use Fabrica\Tools\Plugin;
6
7
/**
8
 * Git plugin.
9
 *
10
 * @author Ricardo Sierra <[email protected]>
11
 */
12
class Git extends Plugin
13
{
14
    protected $actions = [];
15
16
    /**
17
     * @return string
18
     */
19
    public static function pluginName()
20
    {
21
        return 'git';
22
    }
23
    
24
    /**
25
     * Run the Git plugin.
26
     *
27
     * @return bool
28
     */
29
    public function execute()
30
    {
31
        // Check if there are any actions to be run for the branch we're running on:
32
        if (!array_key_exists($this->build->getBranch(), $this->actions)) {
33
            return true;
34
        }
35
36
        $success = true;
37
        foreach ($this->actions[$this->build->getBranch()] as $action => $options) {
38
            if (!$this->runAction($action, $options)) {
39
                $success = false;
40
                break;
41
            }
42
        }
43
44
        return $success;
45
    }
46
47
    /**
48
     * Determine which action to run, and run it.
49
     *
50
     * @param  $action
51
     * @param  array $options
52
     * @return bool
53
     */
54
    protected function runAction($action, array $options = [])
55
    {
56
        switch ($action) {
57
        case 'merge':
58
            return $this->runMergeAction($options);
59
60
        case 'tag':
61
            return $this->runTagAction($options);
62
63
        case 'pull':
64
            return $this->runPullAction($options);
65
66
        case 'push':
67
            return $this->runPushAction($options);
68
        }
69
70
71
        return false;
72
    }
73
74
    /**
75
     * Handle a merge action.
76
     *
77
     * @param  $options
78
     * @return bool
79
     */
80
    protected function runMergeAction($options)
81
    {
82
        if (array_key_exists('branch', $options)) {
83
            $cmd = 'cd "%s" && git checkout %s && git merge "%s"';
84
            $path = $this->builder->buildPath;
85
            return $this->builder->executeCommand($cmd, $path, $options['branch'], $this->build->getBranch());
86
        }
87
    }
88
89
    /**
90
     * Handle a tag action.
91
     *
92
     * @param  $options
93
     * @return bool
94
     */
95
    protected function runTagAction($options)
96
    {
97
        $tagName = date('Ymd-His');
98
        $message = sprintf('Tag created by PHP Censor: %s', date('Y-m-d H:i:s'));
99
100
        if (array_key_exists('name', $options)) {
101
            $tagName = $this->builder->interpolate($options['name']);
102
        }
103
104
        if (array_key_exists('message', $options)) {
105
            $message = $this->builder->interpolate($options['message']);
106
        }
107
108
        $cmd = 'git tag %s -m "%s"';
109
        return $this->builder->executeCommand($cmd, $tagName, $message);
110
    }
111
112
    /**
113
     * Handle a pull action.
114
     *
115
     * @param  $options
116
     * @return bool
117
     */
118 View Code Duplication
    protected function runPullAction($options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        $branch = $this->build->getBranch();
121
        $remote = 'origin';
122
123
        if (array_key_exists('branch', $options)) {
124
            $branch = $this->builder->interpolate($options['branch']);
125
        }
126
127
        if (array_key_exists('remote', $options)) {
128
            $remote = $this->builder->interpolate($options['remote']);
129
        }
130
131
        return $this->builder->executeCommand('git pull %s %s', $remote, $branch);
132
    }
133
134
    /**
135
     * Handle a push action.
136
     *
137
     * @param  $options
138
     * @return bool
139
     */
140 View Code Duplication
    protected function runPushAction($options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $branch = $this->build->getBranch();
143
        $remote = 'origin';
144
145
        if (array_key_exists('branch', $options)) {
146
            $branch = $this->builder->interpolate($options['branch']);
147
        }
148
149
        if (array_key_exists('remote', $options)) {
150
            $remote = $this->builder->interpolate($options['remote']);
151
        }
152
153
        return $this->builder->executeCommand('git push %s %s', $remote, $branch);
154
    }
155
}
156