Passed
Push — test ( ebb7cd...3aa903 )
by Tom
03:02
created

ArtifactSource::getFindPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 1
rs 10
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner\Docker;
6
7
use Ktomk\Pipelines\Cli\Exec;
8
use Ktomk\Pipelines\Glob;
9
10
class ArtifactSource
11
{
12
    /**
13
     * @var Exec
14
     */
15
    private $exec;
16
17
    /**
18
     * @var string container id
19
     */
20
    private $id;
21
22
    /**
23
     * @var string in-container build directory
24
     */
25
    private $dir;
26
27
    /**
28
     * @var null|array all in-container build directory file paths
29
     */
30
    private $allFiles;
31
32
    /**
33
     * ArtifactSource constructor.
34
     * @param Exec $exec
35
     * @param string $id container id
36
     * @param string $dir in-container build directory ($BITBUCKET_CLONE_DIR)
37
     */
38 4
    public function __construct(Exec $exec, $id, $dir)
39
    {
40 4
        $this->exec = $exec;
41 4
        $this->id = $id;
42 4
        $this->dir = $dir;
43 4
    }
44
45
    /**
46
     * @throws \RuntimeException
47
     * @return array|string[]
48
     */
49 3
    public function getAllFiles()
50
    {
51 3
        if (null === $this->allFiles) {
52 3
            $this->allFiles = $this->getFindPaths();
53
        }
54
55 3
        return $this->allFiles;
56
    }
57
58
    /**
59
     * @param string $pattern
60
     * @throws \RuntimeException
61
     * @return array
62
     */
63
    public function findByPattern($pattern)
64
    {
65 1
        $matcher = function ($subject) use ($pattern) {
66 1
            return Glob::match($pattern, $subject);
67 1
        };
68
69 1
        $paths = $this->getAllFiles();
70
71 1
        $found = array_filter($paths, $matcher);
72
73 1
        return array_values($found);
74
    }
75
76
    /**
77
     * @return string container id
78
     */
79 1
    public function getId()
80
    {
81 1
        return $this->id;
82
    }
83
84
    /**
85
     * get an array of paths obtained via docker exec & find
86
     *
87
     * @throws \RuntimeException
88
     * @return array
89
     */
90 3
    private function getFindPaths()
91
    {
92 3
        $buffer = $this->getFindBuffer();
93
94 3
        $lines = explode("\n", trim($buffer, "\n"));
95 3
        $pattern = '~^\\./~';
96 3
        $existing = preg_grep($pattern, $lines);
97 3
        $paths = preg_replace($pattern, '', $existing);
98
99 3
        return array_values($paths);
100
    }
101
102
    /**
103
     * @throws \RuntimeException
104
     * @return string
105
     */
106 3
    private function getFindBuffer()
107
    {
108
        $command = array(
109 3
            'find', '(', '-name', '.git', '-o', '-name', '.idea', ')',
110
            '-prune', '-o', '(', '-type', 'f', '-o', '-type', 'l', ')',
111
        );
112
113 3
        $status = $this->exec->capture('docker', array(
114 3
            'exec', '-w', '/app', $this->id, $command
115 3
        ), $out);
116
117 3
        if (0 === $status) {
118 2
            return $out;
119
        }
120
121 1
        return '';
122
    }
123
}
124