Test Setup Failed
Push — test ( 8f35ef...256cd8 )
by Tom
02:35
created

File::getContentHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
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
     * @var false|string
44
     */
45
    private $contentHash = false;
46
47
    /**
48
     * @param string $path
49 12
     *
50
     * @throws ParseException
51 12
     *
52 12
     * @return File
53 1
     */
54
    public static function createFromFile($path)
55
    {
56 11
        $result = Yaml::file($path);
57 11
        if (null === $result) {
58
            throw new ParseException(sprintf('YAML error: %s; verify the file contains valid YAML', $path));
59 11
        }
60
61
        $file = new self($result);
62
        $file->path = $path;
63
        $file->contentHash = ($buffer = json_encode($file->array)) ? hash('sha256', $buffer) : false;
64
65
        return $file;
66
    }
67
68
    /**
69 18
     * File constructor.
70
     *
71
     * @param array $array
72 18
     *
73 1
     * @throws ParseException
74
     */
75
    public function __construct(array $array)
76
    {
77 17
        // quick validation: pipelines require
78
        if (!isset($array['pipelines']) || !is_array($array['pipelines'])) {
79 15
            throw new ParseException("Missing required property 'pipelines'");
80
        }
81 15
82 15
        // quick validation: image name
83
        Image::validate($array);
84
85
        $this->pipelines = $this->parsePipelines($array['pipelines']);
86
87
        $this->array = $array;
88
    }
89 3
90
    /**
91 3
     * @throws ParseException
92 1
     *
93 3
     * @return Image
94
     */
95 3
    public function getImage()
96
    {
97
        $imageData = isset($this->array['image'])
98
            ? $this->array['image']
99
            : self::DEFAULT_IMAGE;
100
101 2
        return new Image($imageData);
102
    }
103 2
104 1
    /**
105 2
     * @return array|int
106
     */
107
    public function getClone()
108
    {
109
        return isset($this->array['clone'])
110
            ? $this->array['clone']
111 2
            : self::DEFAULT_CLONE;
112
    }
113 2
114
    /**
115
     * @return null|Pipeline
116
     */
117
    public function getDefault()
118
    {
119
        return $this->pipelines->getDefault();
120
    }
121
122
    /**
123
     * Searches the pipeline that matches the reference
124
     *
125
     * @param Reference $reference
126 1
     *
127
     * @throws \UnexpectedValueException
128 1
     * @throws InvalidArgumentException
129
     *
130
     * @return null|string id if found, null otherwise
131
     */
132
    public function searchIdByReference(Reference $reference)
133
    {
134 1
        return $this->pipelines->searchIdByReference($reference);
135
    }
136 1
137
    /**
138
     * @return Pipelines
139
     */
140
    public function getPipelines()
141
    {
142
        return $this->pipelines;
143
    }
144
145
    /**
146
     * @param string $id
147 1
     *
148
     * @throws InvalidArgumentException
149 1
     * @throws ParseException
150
     *
151
     * @return null|Pipeline
152
     */
153
    public function getById($id)
154
    {
155 1
        return $this->pipelines->getById($id);
156
    }
157 1
158 1
    /**
159 1
     * @return Definitions
160
     */
161
    public function getDefinitions()
162
    {
163
        return new Definitions(
164
            isset($this->array['definitions'])
165
                ? $this->array['definitions'] : array()
166 1
        );
167
    }
168 1
169
    /**
170
     * @return array
171
     */
172
    public function getArray()
173
    {
174
        return $this->array;
175
    }
176
177
    /**
178
     * path to the file, null if created from array, string if created from file
179
     *
180 9
     * in its original form, e.g. '-' could represent from stdin
181
     *
182 9
     * @return null|string
183
     *
184
     * @see File::createFromFile()
185
     */
186
    public function getPath()
187
    {
188
        return $this->path;
189
    }
190 15
191
    /**
192 15
     * get hash for the identity of the file
193
     *
194
     * the string return is uppercase hexits but this is implementation specific and can change any time.
195
     *
196
     * @return null|string null if there is no identity, non-zero-length binary string if there is
197
     */
198
    public function getContentHash()
199
    {
200
        return $this->contentHash ? strtoupper($this->contentHash): null;
0 ignored issues
show
Bug introduced by
It seems like $this->contentHash can also be of type true; however, parameter $string of strtoupper() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

200
        return $this->contentHash ? strtoupper(/** @scrutinizer ignore-type */ $this->contentHash): null;
Loading history...
201
    }
202
203
    /**
204
     * @param array $array
205
     *
206
     * @return Pipelines
207
     */
208
    private function parsePipelines(array $array)
209
    {
210
        return new Pipelines($array, $this);
211
    }
212
}
213