Passed
Push — master ( f7fd98...6717a3 )
by Aleksandr
01:32
created

Service::renderTemplate()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 1
nop 1
dl 0
loc 16
rs 9.9666
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
     * @return bool
46
     */
47
    public function create(string $password): bool
48
    {
49
        return file_put_contents(
0 ignored issues
show
Bug Best Practice introduced by
The expression return file_put_contents...derTemplate($password)) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
50
            $this->getTmpProfileFileName(),
51
            $this->renderTemplate($password)
52
        );
53
    }
54
55
    /**
56
     * Delete tmp profile file created for chosen network.
57
     */
58
    public function delete(): bool
59
    {
60
        $tmpProfile = $this->getTmpProfileFileName();
61
62
        if (file_exists($tmpProfile)) {
63
            return unlink($tmpProfile);
64
        }
65
66
        return false;
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
     * @param string $password
84
     *
85
     * @return mixed
86
     */
87
    protected function renderTemplate(string $password): string
88
    {
89
        $content = file_get_contents($this->getTemplateProfileFileName()) ?: '';
90
91
        return str_replace(
92
            [
93
                '{ssid}',
94
                '{hex}',
95
                '{key}',
96
            ],
97
            [
98
                $this->network->ssid,
99
                $this->ssidToHex(),
100
                $password,
101
            ],
102
            $content
103
        );
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    protected function getTemplateProfileFileName(): string
110
    {
111
        if ($this->network->isWpa2()) {
112
            $security = 'WPA2';
113
        } elseif ($this->network->isWpa()) {
114
            $security = 'WPA';
115
        } elseif ($this->network->isWep()) {
116
            $security = 'WEP';
117
        } else {
118
            $security = 'Unknown';
119
        }
120
121
        return __DIR__
122
            .DIRECTORY_SEPARATOR
123
            .static::$templateFolderName
124
            .DIRECTORY_SEPARATOR
125
            .$security
126
            .'-'
127
            .static::$fileNamePostfix;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    protected function ssidToHex(): string
134
    {
135
        $ssid = $this->network->ssid;
136
        $len = strlen($ssid);
137
        $hex = '';
138
139
        for ($i = 0; $i < $len; $i++) {
140
            $hex .= substr('0'.dechex(ord($ssid[$i])), -2);
141
        }
142
143
        return strtoupper($hex);
144
    }
145
}
146