Completed
Push — master ( 3b4375...7245aa )
by Noritaka
01:56
created

ReportTransferCommand::configurationFile()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.2
cc 4
eloc 10
nc 6
nop 0
1
<?php
2
3
/**
4
 * This file is part of CoverallsKit.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace coverallskit;
13
14
use coverallskit\Configuration;
15
use coverallskit\entity\ReportEntity;
16
use Aura\Cli\Stdio;
17
use Aura\Cli\Context;
18
use Aura\Cli\Status;
19
use Eloquent\Pathogen\Factory\PathFactory;
20
use Eloquent\Pathogen\RelativePath;
21
22
23
/**
24
 * Class ReportTransferCommand
25
 * @package coverallskit
26
 */
27
class ReportTransferCommand implements ReportTransferAware
28
{
29
30
    use ReportTransferAwareTrait;
31
32
    /**
33
     * @var Context
34
     */
35
    private $context;
36
37
    /**
38
     * @var Stdio
39
     */
40
    private $stdio;
41
42
    /**
43
     * @var \Eloquent\Pathogen\AbsolutePath
44
     */
45
    private $configFilePath;
46
47
    /**
48
     * @var array
49
     */
50
    private $optionRules = [
51
        'c::',
52
        'd::',
53
        'config::',
54
        'debug::'
55
    ];
56
57
58
    /**
59
     * @param Context $context
60
     * @param Stdio $stdio
61
     */
62
    public function __construct(Context $context, Stdio $stdio)
63
    {
64
        $this->context = $context;
65
        $this->stdio = $stdio;
66
    }
67
68
    /**
69
     * @return int
70
     */
71
    public function __invoke()
72
    {
73
        try {
74
            $this->prepare();
75
        } catch (ConfigFileNotFoundException $exception) {
76
            return $this->failed($exception);
77
        }
78
79
        return $this->performAction();
80
    }
81
82
    /**
83
     * @throws \Eloquent\Pathogen\Exception\NonRelativePathException
84
     */
85
    private function prepare()
86
    {
87
        $configFilePath = $this->configurationFile();
88
        $workDirectory = PathFactory::instance()->create(getcwd());
89
90
        $relativeConfigFilePath = RelativePath::fromString($configFilePath);
91
        $absoluteConfigFilePath = $workDirectory->resolve($relativeConfigFilePath);
92
93
        $this->configFilePath = $absoluteConfigFilePath;
94
95
        if (file_exists((string) $this->configFilePath)) {
96
            return;
97
        }
98
99
        throw new ConfigFileNotFoundException("File $this->configFilePath is not found");
100
    }
101
102
    /**
103
     * @return int
104
     */
105
    private function performAction()
106
    {
107
        $options = $this->context->getopt($this->optionRules);
108
109
        if ($options->get('-d') || $options->get('--debug')) {
110
            return $this->makeReport();
111
        } else {
112
            return $this->sendReport();
113
        }
114
    }
115
116
    /**
117
     * @param PrintableException $exception
118
     * @return int
119
     */
120
    private function failed(PrintableException $exception)
121
    {
122
        $exception->printMessage($this->stdio);
123
        return Status::FAILURE;
124
    }
125
126
    private function configurationFile()
127
    {
128
        $path = null;
129
        $names = [ '-c', '--config' ];
130
        $options = $this->context->getopt($this->optionRules);
131
132
        foreach ($names as $name) {
133
            $path = $options->get($name);
134
135
            if ($path === null) {
136
                continue;
137
            }
138
            break;
139
        }
140
141
        return ($path !== null) ? $path : '.coveralls.toml';
142
    }
143
144
    /**
145
     * @return ReportEntity
146
     */
147
    private function createReport()
148
    {
149
        $configFilePath = (string) $this->configFilePath;
150
        $configuration = BuilderConfiguration::loadFromFile($configFilePath);
151
        $reportBuilder = CoverallsReportBuilder::fromConfiguration($configuration);
152
153
        $report = $reportBuilder->build();
154
155
        return $report;
156
    }
157
158
    /**
159
     * @return int
160
     */
161
    private function makeReport()
162
    {
163
        $report = $this->createReport();
164
        $report->save();
165
166
        return Status::SUCCESS;
167
    }
168
169
    /**
170
     * @return int
171
     */
172
    private function sendReport()
173
    {
174
        $report = $this->createReport();
175
        $report->setReportTransfer($this->getReportTransfer());
176
        $report->save();
177
        $report->upload();
178
179
        return Status::SUCCESS;
180
    }
181
182
}
183