Passed
Pull Request — master (#5)
by Aleksandr
02:53
created

Profile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sanchescom\WiFi\System\Windows;
6
7
/**
8
 * Class Service.
9
 */
10
class Profile
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $ssid;
16
17
    /**
18
     * @var string
19
     */
20
    protected $securityType;
21
22
    /**
23
     * Service constructor.
24
     *
25
     * @param string $ssid
26
     * @param string $securityType
27
     */
28
    public function __construct(string $ssid, string $securityType)
29
    {
30
        $this->ssid = $ssid;
31
        $this->securityType = $securityType;
32
    }
33
34
    /**
35
     * @param string $password
36
     *
37
     * @return string
38
     */
39
    public function create(string $password): string
40
    {
41
        file_put_contents($this->getTmpFileName(), $this->renderTemplate($password));
42
43
        return $this->getTmpFileName();
44
    }
45
46
    /**
47
     * Delete tmp profile file created for chosen network.
48
     */
49
    public function delete(): bool
50
    {
51
        $tmpProfile = $this->getTmpFileName();
52
53
        if (file_exists($tmpProfile)) {
54
            return unlink($tmpProfile);
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    protected function getTmpFileName(): string
64
    {
65
        return __DIR__.'/../../../tmp/'.$this->ssid.'.xml';
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    protected function getTemplateFileName(): string
72
    {
73
        return __DIR__.'/../../../templates/'.$this->securityType.'.xml';
74
    }
75
76
    /**
77
     * @param string $password
78
     *
79
     * @return string
80
     */
81
    protected function renderTemplate(string $password): string
82
    {
83
        $content = file_get_contents($this->getTemplateFileName()) ?: '';
84
85
        return str_replace(
86
            [
87
                '{ssid}',
88
                '{hex}',
89
                '{key}',
90
            ],
91
            [
92
                $this->ssid,
93
                to_hex($this->ssid),
94
                $password,
95
            ],
96
            $content
97
        );
98
    }
99
}
100