Passed
Push — master ( 99cab5...2782fc )
by Caen
04:06 queued 13s
created

BuildTask::then()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\BuildTasks;
6
7
use Hyde\Framework\Concerns\TracksExecutionTime;
8
use Hyde\Hyde;
9
use Illuminate\Console\Concerns\InteractsWithIO;
10
use Illuminate\Console\OutputStyle;
11
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Throwable;
13
14
/**
15
 * @see \Hyde\Framework\Testing\Feature\Services\BuildTaskServiceTest
16
 */
17
abstract class BuildTask
18
{
19
    use InteractsWithIO;
20
    use TracksExecutionTime;
21
22
    /** @var string The message that will be displayed when the task is run. */
23
    protected static string $message = 'Running generic build task';
24
25
    protected int $exitCode = Command::SUCCESS;
26
27
    /** @var \Illuminate\Console\OutputStyle|null */
28
    protected $output;
29
30
    abstract public function handle(): void;
31
32
    /**
33
     * This method is called by the BuildTaskService. It will run the task using the handle method,
34
     * as well as write output to the console, and handle any exceptions that may occur.
35
     *
36
     * @return int The exit code of the task. This can be used when calling a task directly from a command.
37
     */
38
    public function run(?OutputStyle $output = null): int
39
    {
40
        $this->startClock();
41
42
        if ($output && ! $this->output) {
43
            $this->setOutput($output);
44
        }
45
46
        $this->printStartMessage();
47
48
        try {
49
            $this->handle();
50
            $this->printFinishMessage();
51
        } catch (Throwable $exception) {
52
            $this->writeln('<error>Failed</error>');
53
            $this->writeln("<error>{$exception->getMessage()}</error>");
54
            $this->exitCode = $exception->getCode();
55
        }
56
57
        $this->write("\n");
58
59
        return $this->exitCode;
60
    }
61
62
    public function printStartMessage(): void
63
    {
64
        $this->write("<comment>{$this->getMessage()}...</comment> ");
65
    }
66
67
    public function printFinishMessage(): void
68
    {
69
        $this->writeln('<fg=gray>Done in '.$this->getExecutionTimeString().'</>');
70
    }
71
72
    public function getMessage(): string
73
    {
74
        return static::$message;
75
    }
76
77
    public function write(string $message): void
78
    {
79
        $this->output?->write($message);
80
    }
81
82
    public function writeln(string $message): void
83
    {
84
        $this->output?->writeln($message);
85
    }
86
87
    /** Write a fluent message to the output that the task created the specified file. */
88
    public function createdSiteFile(string $path): static
89
    {
90
        $this->write(sprintf(
91
            "\n > Created <info>%s</info>",
92
            str_replace('\\', '/', Hyde::pathToRelative($path))
93
        ));
94
95
        return $this;
96
    }
97
98
    /** Write a fluent message to the output with the execution time of the task. */
99
    public function withExecutionTime(): static
100
    {
101
        $this->write(" in {$this->getExecutionTimeString()}");
102
103
        return $this;
104
    }
105
}
106