GitHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 55
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 7 2
A getFilesFromGitRepository() 0 5 1
A cloneGitRepository() 0 14 2
1
<?php
2
3
namespace SnifferReport\Service;
4
5
use Gitonomy\Git\Admin;
6
use Gitonomy\Git\Repository;
7
8
abstract class GitHandler
9
{
10
11
    /**
12
     * Handles git URLs.
13
     *
14
     * @param $git_url
15
     *
16
     * @return array
17
     */
18
    public static function handle($git_url)
19
    {
20
        $parts = explode(' ', $git_url);
21
        $url = $parts[0];
22
        $branch = isset($parts[1]) ? $parts[1] : '';
23
        return self::getFilesFromGitRepository($url, $branch);
24
    }
25
26
    /**
27
     * Get files from a given git repository and branch (optional).
28
     *
29
     * @param $url
30
     * @param string $branch
31
     *
32
     * @return array
33
     */
34
    private static function getFilesFromGitRepository($url, $branch = '')
35
    {
36
        $repo = self::cloneGitRepository($url, $branch);
37
        return FilesHandler::scanFolder($repo->getPath());
38
    }
39
40
    /**
41
     * Clones a git repository.
42
     *
43
     * @param $url
44
     * @param string $branch
45
     *
46
     * @return Repository
47
     */
48
    private static function cloneGitRepository($url, $branch = '')
49
    {
50
        // Use shallow clone to save time.
51
        $args = ['--depth', '1'];
52
53
        if (!empty($branch)) {
54
            // Clone from specific branch.
55
            $args[] = '--branch';
56
            $args[] = $branch;
57
        }
58
59
        $repository = Admin::cloneRepository(FILES_DIRECTORY_ROOT, $url, $args);
60
        return $repository;
61
    }
62
}
63