Completed
Push — master ( 59ebde...3174b5 )
by ReliQ
01:08
created

Publisher::tell()   B

Complexity

Conditions 8
Paths 48

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.4444
c 0
b 0
f 0
cc 8
nc 48
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Docweaver\Service;
6
7
use Illuminate\Console\Command;
8
use ReliqArts\Docweaver\Contract\Filesystem as FilesystemContract;
9
use ReliqArts\Docweaver\Contract\Logger as LoggerContract;
10
use ReliqArts\Docweaver\Contract\Publisher as PublisherContract;
11
12
abstract class Publisher implements PublisherContract
13
{
14
    protected const TELL_DIRECTION_OUT = 'out';
15
    protected const TELL_DIRECTION_IN = 'in';
16
    protected const TELL_DIRECTION_FLAT = 'flat';
17
    protected const TELL_DIRECTION_NONE = 'none';
18
19
    private const DIRECTORY_READY_MODE = 0777;
20
21
    /**
22
     * Calling command if running in console.
23
     *
24
     * @var Command|null
25
     */
26
    protected ?Command $callingCommand;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '?', expecting T_FUNCTION or T_CONST
Loading history...
27
28
    /**
29
     * @var FilesystemContract
30
     */
31
    protected FilesystemContract $filesystem;
32
33
    /**
34
     * @var LoggerContract
35
     */
36
    protected LoggerContract $logger;
37
38
    /**
39
     * @var float
40
     */
41
    private float $startTime;
42
43
    /**
44
     * Publisher constructor.
45
     */
46
    public function __construct(FilesystemContract $filesystem, LoggerContract $logger)
47
    {
48
        $this->filesystem = $filesystem;
49
        $this->logger = $logger;
50
        $this->startTime = microtime(true);
51
        $this->callingCommand = null;
52
    }
53
54
    protected function getExecutionTime(): string
55
    {
56
        return sprintf('%ss', $this->secondsSince($this->startTime));
57
    }
58
59
    /**
60
     * Print to console or screen.
61
     *
62
     * @param string $text
63
     * @param string $direction in|out
64
     *
65
     * @return string
66
     */
67
    protected function tell($text, $direction = self::TELL_DIRECTION_OUT): string
68
    {
69
        $direction = strtolower($direction);
70
        $nl = app()->runningInConsole() ? "\n" : '<br/>';
71
        $dirSymbol = ($direction === self::TELL_DIRECTION_IN
72
            ? '>> '
73
            : ($direction === self::TELL_DIRECTION_FLAT ? '-- ' : '<< '));
74
        if ($direction === self::TELL_DIRECTION_NONE) {
75
            $dirSymbol = '';
76
        }
77
78
        if ($this->callingCommand && app()->runningInConsole()) {
79
            $line = sprintf('%s%s', $dirSymbol, $text);
80
81
            if ($direction === self::TELL_DIRECTION_OUT) {
82
                $line = sprintf('<info>%s</info>', $line);
83
            }
84
85
            $this->callingCommand->line($line);
86
        } else {
87
            echo "{$nl}{$dirSymbol}{$text}";
88
        }
89
90
        return $text;
91
    }
92
93
    /**
94
     * Ensure documentation resource directory exists and is writable.
95
     */
96
    protected function readyResourceDirectory(string $directory): bool
97
    {
98
        if (!$this->filesystem->isDirectory($directory)) {
99
            $this->filesystem->makeDirectory($directory, self::DIRECTORY_READY_MODE, true);
100
        }
101
102
        return $this->filesystem->isWritable($directory);
103
    }
104
105
    protected function setExecutionStartTime(): void
106
    {
107
        $this->startTime = microtime(true);
108
    }
109
110
    /**
111
     * Get seconds since a micro-time start-time.
112
     *
113
     * @param float $startTime start time in microseconds
114
     *
115
     * @return string seconds since, to 2 decimal places
116
     */
117
    private function secondsSince(float $startTime): string
118
    {
119
        $duration = microtime(true) - $startTime;
120
        $hours = (int)($duration / 60 / 60);
121
        $minutes = (int)($duration / 60) - $hours * 60;
122
        $seconds = $duration - $hours * 60 * 60 - $minutes * 60;
123
124
        return number_format((float)$seconds, 2, '.', '');
125
    }
126
}
127