Passed
Push — master ( 1f6f72...a5203b )
by Tom
02:42
created

File   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 147
ccs 35
cts 35
cp 1
rs 10
wmc 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefault() 0 3 1
A getClone() 0 5 2
A getImage() 0 7 2
A __construct() 0 13 3
A searchIdByReference() 0 3 1
A getById() 0 3 1
A createFromFile() 0 8 2
A getPipelines() 0 3 1
A parsePipelines() 0 3 1
A getDefinitions() 0 5 2
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
     * @param string $path
37
     *
38
     * @throws ParseException
39
     *
40
     * @return File
41
     */
42 11
    public static function createFromFile($path)
43
    {
44 11
        $result = Yaml::file($path);
45 11
        if (null === $result) {
46 1
            throw new ParseException(sprintf('YAML error: %s; verify the file contains valid YAML', $path));
47
        }
48
49 10
        return new self($result);
50
    }
51
52
    /**
53
     * File constructor.
54
     *
55
     * @param array $array
56
     *
57
     * @throws ParseException
58
     */
59 17
    public function __construct(array $array)
60
    {
61
        // quick validation: pipelines require
62 17
        if (!isset($array['pipelines']) || !is_array($array['pipelines'])) {
63 1
            throw new ParseException("Missing required property 'pipelines'");
64
        }
65
66
        // quick validation: image name
67 16
        Image::validate($array);
68
69 14
        $this->pipelines = $this->parsePipelines($array['pipelines']);
70
71 14
        $this->array = $array;
72 14
    }
73
74
    /**
75
     * @throws ParseException
76
     *
77
     * @return Image
78
     */
79 3
    public function getImage()
80
    {
81 3
        $imageData = isset($this->array['image'])
82 1
            ? $this->array['image']
83 3
            : self::DEFAULT_IMAGE;
84
85 3
        return new Image($imageData);
86
    }
87
88
    /**
89
     * @return array|int
90
     */
91 2
    public function getClone()
92
    {
93 2
        return isset($this->array['clone'])
94 1
            ? $this->array['clone']
95 2
            : self::DEFAULT_CLONE;
96
    }
97
98
    /**
99
     * @return null|Pipeline
100
     */
101 2
    public function getDefault()
102
    {
103 2
        return $this->pipelines->getDefault();
104
    }
105
106
    /**
107
     * Searches the pipeline that matches the reference
108
     *
109
     * @param Reference $reference
110
     *
111
     * @throws \UnexpectedValueException
112
     * @throws InvalidArgumentException
113
     *
114
     * @return null|string id if found, null otherwise
115
     */
116 1
    public function searchIdByReference(Reference $reference)
117
    {
118 1
        return $this->pipelines->searchIdByReference($reference);
119
    }
120
121
    /**
122
     * @return Pipelines
123
     */
124 1
    public function getPipelines()
125
    {
126 1
        return $this->pipelines;
127
    }
128
129
    /**
130
     * @param string $id
131
     *
132
     * @throws InvalidArgumentException
133
     * @throws ParseException
134
     *
135
     * @return null|Pipeline
136
     */
137 1
    public function getById($id)
138
    {
139 1
        return $this->pipelines->getById($id);
140
    }
141
142
    /**
143
     * @return Definitions
144
     */
145 1
    public function getDefinitions()
146
    {
147 1
        return new Definitions(
148 1
            isset($this->array['definitions'])
149 1
                ? $this->array['definitions'] : array()
150
        );
151
    }
152
153
    /**
154
     * @param array $array
155
     *
156
     * @return Pipelines
157
     */
158 14
    private function parsePipelines(array $array)
159
    {
160 14
        return new Pipelines($array, $this);
161
    }
162
}
163