Issues (11)

src/Output/DualOutputCommand.php (2 issues)

1
<?php
2
3
namespace Tequilarapido\Consolify\Output;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Tequilarapido\Consolify\Trace\TraceReserved;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
abstract class DualOutputCommand extends Command
12
{
13
    public function run(InputInterface $input, OutputInterface $output)
14
    {
15
        $this->beforeRun($input);
16
17
        // If we run the manually with -vvv option
18
        // we will execute the command without exception handling
19
        // to be able to debug as usual.
20
        if ($this->isVerbose($input)) {
21
            return $this->effectiveRun($input, $output);
22
        }
23
24
        try {
25
            return $this->effectiveRun($input, $output);
26
        } catch (Exception $e) {
27
            $this->handeException($e);
28
        }
29
    }
30
31
    /**
32
     * Override this method to be able to do some wide system setup
33
     * before the command runs.
34
     *
35
     *  ie. Disable query loging for performance response (DB::disableQueryLog())
36
     *
37
     * @param InputInterface $input
38
     *
39
     * @return
40
     */
41
    abstract protected function beforeRun(InputInterface $input);
42
43
    /**
44
     * Must return the full path for the output file where the console
45
     * output will be streamed.
46
     *
47
     * @param InputInterface $input
48
     *
49
     * @return mixed
50
     */
51
    abstract protected function outputFilePath(InputInterface $input);
52
53
    /**
54
     * Run the command.
55
     *
56
     * @param $input
57
     * @param $output
58
     *
59
     * @return int
60
     */
61
    protected function effectiveRun($input, $output)
62
    {
63
        $outputFile = $this->outputFilePath($input);
64
65
        $dualOutput = (new DualOutput(fopen($outputFile, 'a', false)))
0 ignored issues
show
It seems like fopen($outputFile, 'a', false) can also be of type false; however, parameter $stream of Tequilarapido\Consolify\...alOutput::__construct() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        $dualOutput = (new DualOutput(/** @scrutinizer ignore-type */ fopen($outputFile, 'a', false)))
Loading history...
66
            ->setFile($outputFile)
67
            ->setConsoleOutput($output);
68
69
        return parent::run(
70
            $this->input = $input, $this->output = $dualOutput
0 ignored issues
show
Documentation Bug introduced by
It seems like $dualOutput of type Tequilarapido\Consolify\Output\DualOutput is incompatible with the declared type Illuminate\Console\OutputStyle of property $output.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
        );
72
    }
73
74
    /**
75
     * Handles exceptions that occurs while running the command.
76
     *
77
     * This give us a way to customize the actions that must be taken when something
78
     * went sideways. (ie. flag something as failed in database).
79
     *
80
     * @param Exception $e
81
     */
82
    protected function handeException(Exception $e)
83
    {
84
        $this->error(TraceReserved::FAILED." {$e->getMessage()}");
85
86
        app()->log->error($e);
87
    }
88
89
    /**
90
     * For some operations we need to get the value of an argument passed to the command.
91
     * The issue here is that parent::__constructor() is not called yet, so the symfony/console
92
     * has not parsed yet the arguments, so we need to to hack it manually.
93
     *
94
     * @param $input
95
     * @param $index
96
     *
97
     * @return string
98
     */
99
    protected function getNotParsedArgument(InputInterface $input, $index)
100
    {
101
        return explode(' ', (string) $input)[$index];
102
    }
103
104
    /**
105
     * Do `-vvv` was used when calling the command.
106
     *
107
     * @param InputInterface $input
108
     *
109
     * @return bool
110
     */
111
    protected function isVerbose(InputInterface $input)
112
    {
113
        return (bool) substr_count((string) $input, '-vvv');
114
    }
115
}
116