summaryrefslogtreecommitdiff
path: root/src/Module/AptModule.php
blob: ff4cb951edc0677bc1da17f7da0a6ba8c159c50c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php

namespace PHPIAC\Module;

use phpseclib3\Net\SSH2;

class AptModule implements ModuleInterface
{
    public function __construct(
        private string $package,
        private string $state = State::PRESENT
    ) {}

    public function checkState(): bool
    {
        global $ssh;
        /**@var SSH2 $ssh*/
        $ssh->enablePTY();
        $ssh->exec("which $this->package");

        if ($this->state === State::PRESENT) {
            $state = ! empty($ssh->read());
        }
        else if ($this->state === State::ABSENT) {
            $state = empty($ssh->read());
        }

        $ssh->disablePTY();

        return $state;
    }

    public function getCommands(): array
    {
        if ($this->state === State::PRESENT) {
            return [
                "sudo apt install -y $this->package",
            ];
        }
        else if ($this->state === State::ABSENT) {
            return [
                "sudo apt remove -y $this->package",
            ];
        }

        return [];
    }
}