Test Failed
Push — main ( 9f888f...38d23e )
by Michiel
09:44
created

OpenTask   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 34
c 1
b 0
f 0
dl 0
loc 88
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A openPath() 0 9 1
A setPath() 0 3 1
A retrieveOpenerTool() 0 18 5
A main() 0 12 3
A init() 0 5 1
1
<?php
2
3
namespace Phing\Task\Optional;
4
5
use Exception;
6
use Phing\Exception\BuildException;
7
use Phing\Io\{FileSystem, UnixFileSystem, WindowsFileSystem};
8
use Phing\{Project, Task};
9
use Phing\Task\System\ExecTask;
10
use Throwable;
11
12
/**
13
 * Opens a file or URL in the user's preferred application.
14
 *
15
 * @author Jawira Portugal <[email protected]>
16
 */
17
class OpenTask extends Task
18
{
19
    /**
20
     * @var string Can be a file, directory or URL
21
     */
22
    protected $path;
23
24
    /**
25
     * @var UnixFileSystem|WindowsFileSystem
26
     */
27
    protected $fileSystem;
28
29
    /**
30
     * @var ExecTask
31
     */
32
    protected $execTask;
33
34
    /**
35
     * Path to be opened later
36
     */
37
    public function setPath(string $path): void
38
    {
39
        $this->path = $path;
40
    }
41
42
    /**
43
     * Initialize dependencies
44
     */
45
    public function init(): void
46
    {
47
        $this->path       = '';
48
        $this->fileSystem = FileSystem::getFileSystem();
49
        $this->execTask   = new ExecTask();
50
    }
51
52
    /**
53
     * Main method
54
     */
55
    public function main(): void
56
    {
57
        try {
58
            $this->log("Path: $this->path", Project::MSG_VERBOSE);
59
            if (empty($this->path)) {
60
                throw new Exception('"path" is required');
61
            }
62
            $executable = $this->retrieveOpenerTool();
63
            $this->openPath($executable, $this->path);
64
        } catch (Throwable $th) {
65
            $this->log($th->getMessage(), Project::MSG_ERR);
66
            throw new BuildException("Error while opening $this->path");
67
        }
68
    }
69
70
    /**
71
     * Retrieves right opener tool to call according to current OS
72
     */
73
    public function retrieveOpenerTool(): string
74
    {
75
        $executables = ($this->fileSystem instanceof UnixFileSystem) ? ['xdg-open', 'wslview', 'open'] : ['start'];
76
        $which       = null;
77
78
        foreach ($executables as $executable) {
79
            $which = $this->fileSystem->which($executable, null);
80
            if ($which) {
81
                $this->log("Opener tool found: $which", Project::MSG_VERBOSE);
82
                break;
83
            }
84
        }
85
86
        if (empty($which)) {
87
            throw new Exception('Cannot retrieve opener tool');
88
        }
89
90
        return $which;
91
    }
92
93
    /**
94
     * Run executable with path as argument
95
     */
96
    protected function openPath(string $executable, string $path): void
97
    {
98
        $this->log("Opening $path");
99
        $this->execTask->setProject($this->getProject());
100
        $this->execTask->setLocation($this->getLocation());
101
        $this->execTask->setExecutable($executable);
102
        $this->execTask->setCheckreturn(true);
103
        $this->execTask->createArg()->setValue($path);
104
        $this->execTask->main();
105
    }
106
}
107