Completed
Push — v2.1.0 ( dbf4e1...a3716a )
by Noritaka
03:06
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
use Exception;
22
23
24
/**
25
 * Class ReportTransferCommand
26
 * @package coverallskit
27
 */
28
class ReportTransferCommand implements ReportTransferAware
29
{
30
31
    use ReportTransferAwareTrait;
32
33
    /**
34
     * @var Context
35
     */
36
    private $context;
37
38
    /**
39
     * @var Stdio
40
     */
41
    private $stdio;
42
43
    /**
44
     * @var \Eloquent\Pathogen\AbsolutePath
45
     */
46
    private $configFilePath;
47
48
    /**
49
     * @var array
50
     */
51
    private $optionRules = [
52
        'c::',
53
        'd::',
54
        'config::',
55
        'debug::'
56
    ];
57
58
59
    /**
60
     * @param Context $context
61
     * @param Stdio $stdio
62
     */
63
    public function __construct(Context $context, Stdio $stdio)
64
    {
65
        $this->context = $context;
66
        $this->stdio = $stdio;
67
    }
68
69
    /**
70
     * @return int
71
     */
72
    public function __invoke()
73
    {
74
        try {
75
            $this->prepare();
76
        } catch (ConfigFileNotFoundException $exception) {
77
            return $this->failed($exception);
0 ignored issues
show
Documentation introduced by
$exception is of type object<coverallskit\ConfigFileNotFoundException>, but the function expects a object<coverallskit\PrintableExceptionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        }
79
80
        return $this->performAction();
81
    }
82
83
    /**
84
     * @throws \Eloquent\Pathogen\Exception\NonRelativePathException
85
     */
86
    private function prepare()
87
    {
88
        $configFilePath = $this->configurationFile();
89
        $workDirectory = PathFactory::instance()->create(getcwd());
90
91
        $relativeConfigFilePath = RelativePath::fromString($configFilePath);
92
        $absoluteConfigFilePath = $workDirectory->resolve($relativeConfigFilePath);
93
94
        $this->configFilePath = $absoluteConfigFilePath;
95
96
        if (file_exists((string) $this->configFilePath)) {
97
            return;
98
        }
99
100
        throw new ConfigFileNotFoundException("File $this->configFilePath is not found");
101
    }
102
103
    /**
104
     * @return int
105
     */
106
    private function performAction()
107
    {
108
        $options = $this->context->getopt($this->optionRules);
109
110
        if ($options->get('-d') || $options->get('--debug')) {
111
            return $this->makeReport();
112
        } else {
113
            return $this->sendReport();
114
        }
115
    }
116
117
    /**
118
     * @param PrintableExceptionInterface $exception
119
     * @return int
120
     */
121
    private function failed(PrintableExceptionInterface $exception)
122
    {
123
        $exception->printMessage($this->stdio);
124
        return Status::FAILURE;
125
    }
126
127
    private function configurationFile()
128
    {
129
        $path = null;
130
        $names = [ '-c', '--config' ];
131
        $options = $this->context->getopt($this->optionRules);
132
133
        foreach ($names as $name) {
134
            $path = $options->get($name);
135
136
            if ($path === null) {
137
                continue;
138
            }
139
            break;
140
        }
141
142
        return ($path !== null) ? $path : '.coveralls.toml';
143
    }
144
145
    /**
146
     * @return ReportEntity
147
     */
148
    private function createReport()
149
    {
150
        $configFilePath = (string) $this->configFilePath;
151
        $configuration = BuilderConfiguration::loadFromFile($configFilePath);
152
        $reportBuilder = CoverallsReportBuilder::fromConfiguration($configuration);
153
154
        $report = $reportBuilder->build();
155
156
        return $report;
157
    }
158
159
    /**
160
     * @return int
161
     */
162
    private function makeReport()
163
    {
164
        $report = $this->createReport();
165
        $report->save();
166
167
        return Status::SUCCESS;
168
    }
169
170
    /**
171
     * @return int
172
     */
173
    private function sendReport()
174
    {
175
        $report = $this->createReport();
176
        $report->setReportTransfer($this->getReportTransfer());
177
        $report->save();
178
        $report->upload();
179
180
        return Status::SUCCESS;
181
    }
182
183
}
184