Completed
Branch master (6407fd)
by Gordon
04:43 queued 02:45
created

TravisYMLHelper::saveTravis()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\PHPTravisEnhancer\Helper;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
class TravisYMLHelper
8
{
9
10
    /** @var string */
11
    private $travisFileName = '.travis.yml';
12
13
    /**
14
     * TravisYMLHelper constructor.
15
     *
16
     * @param string $travisFileName The name of the travis config file, by default .travis.yml, but injectable for
17
     * testing purposes
18
     */
19
    public function __construct(string $travisFileName = '.travis.yml')
20
    {
21
        $this->travisFileName = $travisFileName;
22
    }
23
24
25
    /**
26
     * Load the Travis file, or default to an empty array if it does not exist
27
     *
28
     * @todo Change this behavior?
29
     * @return mixed The Travis YAML file as an array (note Symfony returns mixed)
30
     */
31
    public function loadTravis()
32
    {
33
        $result = [];
34
        $path = \getcwd() . '/' . $this->travisFileName;
35
        if (\file_exists($this->travisFileName)) {
36
            $result = Yaml::parseFile($path);
37
        }
38
39
        return $result;
40
    }
41
42
43
    /**
44
     * Save a travis file, default .travis.yml, in the root of a project
45
     *
46
     * @todo How does one specifiy this as an associative array?
47
     * @param array<string> $yamlArray an array that ought to have been formed from a YAML file
48
     */
49
    public function saveTravis(array $yamlArray): void
50
    {
51
        $yaml = Yaml::dump($yamlArray, Yaml::DUMP_OBJECT_AS_MAP);
52
        \file_put_contents($this->travisFileName, $yaml);
53
    }
54
}
55