Passed
Push — master ( 49fb01...6703f1 )
by Tom
03:03
created

File::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File;
6
7
use InvalidArgumentException;
8
use Ktomk\Pipelines\Runner\Reference;
9
use Ktomk\Pipelines\Yaml\Yaml;
10
11
/**
12
 * Bitbucket Pipelines file
13
 */
14
class File
15
{
16
    const FILE_NAME = 'bitbucket-pipelines.yml';
17
18
    const DEFAULT_IMAGE = 'atlassian/default-image:latest';
19
20
    /**
21
     * default clone depth
22
     */
23
    const DEFAULT_CLONE = 50;
24
25
    /**
26
     * @var array
27
     */
28
    private $array;
29
30
    /**
31
     * @var Pipelines
32
     */
33
    private $pipelines;
34
35
    /**
36
     * @var null|string path to file (the visual one to re-present it back to the user)
37
     *
38
     * @see File::createFromFile()
39
     */
40
    private $path;
41
42
    /**
43
     * @param string $path
44
     *
45
     * @throws ParseException
46
     *
47
     * @return File
48
     */
49 12
    public static function createFromFile($path)
50
    {
51 12
        $result = Yaml::file($path);
52 12
        if (null === $result) {
53 1
            throw new ParseException(sprintf('YAML error: %s; verify the file contains valid YAML', $path));
54
        }
55
56 11
        $file = new self($result);
57 11
        $file->path = $path;
58
59 11
        return $file;
60
    }
61
62
    /**
63
     * File constructor.
64
     *
65
     * @param array $array
66
     *
67
     * @throws ParseException
68
     */
69 18
    public function __construct(array $array)
70
    {
71
        // quick validation: pipelines require
72 18
        if (!isset($array['pipelines']) || !is_array($array['pipelines'])) {
73 1
            throw new ParseException("Missing required property 'pipelines'");
74
        }
75
76
        // quick validation: image name
77 17
        Image::validate($array);
78
79 15
        $this->pipelines = $this->parsePipelines($array['pipelines']);
80
81 15
        $this->array = $array;
82 15
    }
83
84
    /**
85
     * @throws ParseException
86
     *
87
     * @return Image
88
     */
89 3
    public function getImage()
90
    {
91 3
        $imageData = isset($this->array['image'])
92 1
            ? $this->array['image']
93 3
            : self::DEFAULT_IMAGE;
94
95 3
        return new Image($imageData);
96
    }
97
98
    /**
99
     * @return array|int
100
     */
101 2
    public function getClone()
102
    {
103 2
        return isset($this->array['clone'])
104 1
            ? $this->array['clone']
105 2
            : self::DEFAULT_CLONE;
106
    }
107
108
    /**
109
     * @return null|Pipeline
110
     */
111 2
    public function getDefault()
112
    {
113 2
        return $this->pipelines->getDefault();
114
    }
115
116
    /**
117
     * Searches the pipeline that matches the reference
118
     *
119
     * @param Reference $reference
120
     *
121
     * @throws \UnexpectedValueException
122
     * @throws InvalidArgumentException
123
     *
124
     * @return null|string id if found, null otherwise
125
     */
126 1
    public function searchIdByReference(Reference $reference)
127
    {
128 1
        return $this->pipelines->searchIdByReference($reference);
129
    }
130
131
    /**
132
     * @return Pipelines
133
     */
134 1
    public function getPipelines()
135
    {
136 1
        return $this->pipelines;
137
    }
138
139
    /**
140
     * @param string $id
141
     *
142
     * @throws InvalidArgumentException
143
     * @throws ParseException
144
     *
145
     * @return null|Pipeline
146
     */
147 1
    public function getById($id)
148
    {
149 1
        return $this->pipelines->getById($id);
150
    }
151
152
    /**
153
     * @return Definitions
154
     */
155 1
    public function getDefinitions()
156
    {
157 1
        return new Definitions(
158 1
            isset($this->array['definitions'])
159 1
                ? $this->array['definitions'] : array()
160
        );
161
    }
162
163
    /**
164
     * @return array
165
     */
166 1
    public function getArray()
167
    {
168 1
        return $this->array;
169
    }
170
171
    /**
172
     * path to the file, null if created from array, string if created from file
173
     *
174
     * in its original form, e.g. '-' could represent from stdin
175
     *
176
     * @return null|string
177
     *
178
     * @see File::createFromFile()
179
     */
180 9
    public function getPath()
181
    {
182 9
        return $this->path;
183
    }
184
185
    /**
186
     * @param array $array
187
     *
188
     * @return Pipelines
189
     */
190 15
    private function parsePipelines(array $array)
191
    {
192 15
        return new Pipelines($array, $this);
193
    }
194
}
195