Passed
Push — master ( 18c4cb...39ec9f )
by Aleksandr
01:36
created

Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sanchescom\WiFi\System\Windows\Profile;
4
5
use Sanchescom\WiFi\System\Windows\Network;
6
7
/**
8
 * Class Service.
9
 */
10
class Service
11
{
12
    /**
13
     * @var string
14
     */
15
    protected static $tmpFolderName = 'tmp';
16
17
    /**
18
     * @var string
19
     */
20
    protected static $templateFolderName = 'templates';
21
22
    /**
23
     * @var string
24
     */
25
    protected static $fileNamePostfix = 'PersonalProfileTemplate.xml';
26
27
    /**
28
     * @var Network
29
     */
30
    protected $network;
31
32
    /**
33
     * Service constructor.
34
     *
35
     * @param Network $network
36
     */
37
    public function __construct(Network $network)
38
    {
39
        $this->network = $network;
40
    }
41
42
    /**
43
     * @param string $password
44
     */
45
    public function create($password): void
46
    {
47
        file_put_contents(
48
            $this->getTmpProfileFileName(),
49
            str_replace(
50
                ['{ssid}', '{hex}', '{key}'],
51
                [$this->network->ssid, $this->ssidToHex(), $password],
52
                (file_get_contents($this->getTemplateProfileFileName()) ?: '')
53
            )
54
        );
55
    }
56
57
    /**
58
     * Delete tmp profile file created for chosen network.
59
     */
60
    public function delete(): void
61
    {
62
        $tmpProfile = $this->getTmpProfileFileName();
63
64
        if (file_exists($tmpProfile)) {
65
            unlink($tmpProfile);
66
        }
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getTmpProfileFileName(): string
73
    {
74
        return __DIR__
75
            . DIRECTORY_SEPARATOR
76
            . static::$tmpFolderName
77
            . DIRECTORY_SEPARATOR
78
            . $this->network->ssid
79
            . '.xml';
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    protected function getTemplateProfileFileName(): string
86
    {
87
        if ($this->network->isWpa2()) {
88
            $security = 'WPA2';
89
        } elseif ($this->network->isWpa()) {
90
            $security = 'WPA';
91
        } elseif ($this->network->isWep()) {
92
            $security = 'WEP';
93
        } else {
94
            $security = 'Unknown';
95
        }
96
97
        return __DIR__
98
            . DIRECTORY_SEPARATOR
99
            . static::$templateFolderName
100
            . DIRECTORY_SEPARATOR
101
            . $security
102
            . '-'
103
            . static::$fileNamePostfix;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    protected function ssidToHex(): string
110
    {
111
        $ssid = $this->network->ssid;
112
        $len = strlen($ssid);
113
        $hex = '';
114
115
        for ($i = 0; $i < $len; $i++) {
116
            $hex .= substr('0' . dechex(ord($ssid[$i])), -2);
117
        }
118
119
        return strtoupper($hex);
120
    }
121
}
122