Passed
Push — master ( 26e86a...9b47e1 )
by Tom
03:10
created

File::getOptions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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