Passed
Push — master ( 5b8585...da319f )
by Petr
07:39
created

Clipr::run()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 35
ccs 20
cts 20
cp 1
rs 8.8333
cc 7
nc 17
nop 0
crap 7
1
<?php
2
3
namespace kalanis\kw_clipr;
4
5
6
use kalanis\kw_input\Interfaces\IFiltered;
7
8
9
/**
10
 * Class Clipr
11
 * @package kalanis\kw_clipr
12
 * Main class which runs the whole task system
13
 */
14
class Clipr
15
{
16
    /** @var Interfaces\ILoader */
17
    protected $loader = null;
18
    /** @var IFiltered */
19
    protected $variables = null;
20
    /** @var Clipr\Sources */
21
    protected $sources = null;
22
23 2
    public function __construct(Interfaces\ILoader $loader, Clipr\Sources $sources, IFiltered $variables)
24
    {
25 2
        $this->loader = $loader;
26 2
        $this->sources = $sources;
27 2
        $this->variables = $variables;
28 2
    }
29
30
    /**
31
     * @throws CliprException
32
     * @return int
33
     */
34 2
    public function run(): int
35
    {
36
        // for parsing default params it's necessary to load another task
37 2
        $dummy = new Tasks\DummyTask();
38 2
        $dummy->initTask(new Output\Clear(), $this->variables, $this->loader);
39 2
        $this->sources->determineInput((bool) $dummy->webOutput, (bool) $dummy->noColor);
40
41
        // now we know necessary input data, so we can initialize real task
42 2
        $inputs = $this->variables->getInArray(null, $this->sources->getEntryTypes());
43 2
        $taskName = Clipr\Useful::getNthParam($inputs) ?? Interfaces\ILoader::DEFAULT_TASK;
44 2
        $task = $this->loader->getTask($taskName);
45 2
        if (!$task) {
46 1
            throw new CliprException(sprintf('Unknown task *%s* - check name, interface or your config paths.', $taskName), Interfaces\IStatuses::STATUS_CLI_USAGE);
47
        }
48 1
        $task->initTask($this->sources->getOutput(), $this->variables, $this->loader);
49
50 1
        if (Interfaces\ISources::OUTPUT_STD != $task->outputFile) {
51 1
            ob_start();
52
        }
53
54 1
        if (false === $task->noHeaders) {
55 1
            $task->writeHeader();
56
        }
57
58 1
        $result = $task->process();
59
60 1
        if (false === $task->noHeaders) {
61 1
            $task->writeFooter();
62
        }
63
64 1
        if (Interfaces\ISources::OUTPUT_STD != $task->outputFile) {
65 1
            file_put_contents($task->outputFile, ob_get_clean(), (false === $task->noAppend ? FILE_APPEND : 0));
66
        }
67
68 1
        return $result;
69
    }
70
}
71