Completed
Push — master ( aa5756...106423 )
by Andrii
11:44
created

TravisYaml::getBeforeInstall()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 15
nc 4
nop 0
1
<?php
2
3
/*
4
 * Travis CI plugin for HiDev
5
 *
6
 * @link      https://github.com/hiqdev/hidev-travis
7
 * @package   hidev-travis
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\travis\components;
13
14
/**
15
 * `.travis.yml` config file.
16
 */
17
class TravisYaml extends \hidev\base\ConfigFile
18
{
19
    protected $_file = '.travis.yml';
20
21
    protected $_bin;
22
23
    public $sudo = false;
24
25
    public function getBin()
26
    {
27
        if ($this->_bin === null) {
28
            $this->_bin = $this->detectBin();
29
        }
30
31
        return $this->_bin;
32
    }
33
34
    public function detectBin()
35
    {
36
        if ($this->take('package')->fullName === 'hiqdev/hidev') {
37
            return './bin/hidev';
38
        }
39
        if ($this->take('package')->hasRequireAny('hiqdev/hidev')) {
40
            return './vendor/bin/hidev';
41
        }
42
43
        return './hidev.phar';
44
    }
45
46
    public function getBeforeInstall()
47
    {
48
        $commands = $this->get('before_install');
49
        if ($this->getItem('language') != 'php') {
50
            $this->sudo = true;
51
            $commands[] = 'sudo add-apt-repository --yes ppa:ondrej/php';
52
            $commands[] = 'sudo apt-get update';
53
            $commands[] = 'sudo apt-get install php5.6-cli php5.6-mbstring';
54
            $commands[] = 'env php -v';
55
        }
56
        if ($this->bin === './hidev.phar') {
57
            $commands[] = 'wget http://hiqdev.com/hidev/hidev.phar -O hidev.phar && chmod a+x hidev.phar';
58
        } else {
59
            $commands[] = 'composer install --no-interaction';
60
        }
61
        $commands[] = $this->getBin() . ' --version';
62
        $commands[] = $this->getBin() . ' travis/before_install';
63
64
        return array_values(array_unique($commands));
65
    }
66
67
    /**
68
     * Reorders config elements.
69
     */
70
    public function save()
71
    {
72
        $this->addActionItems();
73
        $this->prependLanguageOptions();
74
75
        return parent::save();
76
    }
77
78
    public function addActionItems()
79
    {
80
        $add_items = [
81
            'sudo'           => false,
82
            'before_install' => $this->getBeforeInstall(),
83
        ];
84
        foreach (['install', 'before_script', 'script', 'after_success', 'after_failure', 'after_script'] as $event) {
85
            if ($this->take('travis')->{$event}) {
86
                $add_items[$event] = [$this->getBin() . ' travis/' . $event];
87
            }
88
        }
89
        $this->setItems($add_items);
90
    }
91
92
    public function prependLanguageOptions()
93
    {
94
        $items = $this->_items;
95
        $language = $items['language'] ?: $this->take('package')->getLanguage();
96
        $lang_ops = $items[$language];
97
        unset($items['language'], $items[$language]);
98
        $this->_items = [
99
            'language' => $language,
100
            $language  => $lang_ops,
101
        ] + $items;
102
        $this->setItem('sudo', $this->sudo);
103
    }
104
}
105