Completed
Push — master ( db10ca...f1bcff )
by Blackred
01:55
created

PhinxMigrateTask::getPhinxPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
3
/*
4
 * Wolnościowiec / WebDeploy
5
 * ------------------------
6
 *
7
 *   Framework for creation of post-install scripts dedicated
8
 *   for applications hosted on shared hosting (without access to the shell).
9
 *
10
 *   A part of an anarchist portal - wolnosciowiec.net
11
 *
12
 *   Wolnościowiec is a project to integrate the movement
13
 *   of people who strive to build a society based on
14
 *   solidarity, freedom, equality with a respect for
15
 *   individual and cooperation of each other.
16
 *
17
 *   We support human rights, animal rights, feminism,
18
 *   anti-capitalism (taking over the production by workers),
19
 *   anti-racism, and internationalism. We negate
20
 *   the political fight and politicians at all.
21
 *
22
 *   http://wolnosciowiec.net/en
23
 *
24
 *   License: LGPLv3
25
 */
26
27
namespace Wolnosciowiec\WebDeploy\Tasks;
28
29
use Phinx\Wrapper\TextWrapper;
30
use \Psr\Http\Message\RequestInterface;
31
use Wolnosciowiec\WebDeploy\Exceptions\DeploymentFailureException;
32
33
/**
34
 * @package Wolnosciowiec\WebDeploy\Tasks
35
 */
36
class PhinxMigrateTask implements TaskInterface
37
{
38
    /**
39
     * @var string $configurationPath
40
     */
41
    protected $configurationPath = null;
42
43
    protected function getEnvironment(): string
44
    {
45
        return getenv('WL_PHINX_ENV') ? getenv('WL_PHINX_ENV') : null;
46
    }
47
48
    protected function getTarget(): string
49
    {
50
        return getenv('WL_PHINX_TARGET') ? getenv('WL_PHINX_TARGET') : null;
51
    }
52
53
    protected function getParser(): string
54
    {
55
        return getenv('WL_PHINX_PARSER') ? getenv('WL_PHINX_PARSER') : 'Yaml';
56
    }
57
58
    protected function getConfigurationPath(): string
59
    {
60
        if ($this->configurationPath === null) {
61
            foreach (array_filter([
62
                __DIR__ . '/../../../../../phinx.yml',
63
                __DIR__ . '/../../../../../phinx.php',
64
                __DIR__ . '/../../phinx.yml',
65
                __DIR__ . '/../../phinx.php',
66
                getenv('WL_PHINX_PATH')
67
            ]) as $path)
68
            {
69
                if (is_file($path)) {
70
                    $this->configurationPath = $path;
71
                    return $path;
72
                }
73
            }
74
75
            throw new \Exception('Cannot find a phinx.php configuration file');
76
        }
77
78
        return $this->configurationPath;
79
    }
80
81
    private function getPhinxPath(): string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
82
    {
83
        $paths = [
84
            __DIR__ . '/../../vendor/robmorgan/phinx/app/phinx.php',
85
            __DIR__ . '/../../../../robmorgan/phinx/app/phinx.php',
86
            getenv('WL_PHINX_APP_PATH')
87
        ];
88
89
        foreach (array_filter($paths) as $path) {
90
            if (is_file($path)) {
91
                return $path;
92
            }
93
        }
94
95
        throw new \Exception('Cannot find phinx path!');
96
    }
97
98
    /**
99
     * @param RequestInterface $request
100
     *
101
     * @throws DeploymentFailureException
102
     * @throws \Exception
103
     *
104
     * @return string
105
     */
106
    public function execute(RequestInterface $request): string
107
    {
108
        $app = require $this->getPhinxPath();
109
        $wrap = new TextWrapper($app);
110
111
        $wrap->setOption('configuration', $this->getConfigurationPath());
112
        $wrap->setOption('parser',        $this->getParser());
113
114
        $action = $wrap->getMigrate($this->getEnvironment(), $this->getTarget());
115
116
        if ($wrap->getExitCode() !== 0) {
117
            throw new DeploymentFailureException('Phinx failed with a non-zero exit code, details: "' . $action . '"');
118
        }
119
120
        return $action;
121
    }
122
123
    /**
124
     * @param string $configurationPath
125
     * @return $this
126
     */
127
    public function setConfigurationPath(string $configurationPath)
128
    {
129
        $this->configurationPath = $configurationPath;
130
        return $this;
131
    }
132
}